正好用到这个小方法,就自己记录下来吧!
1.新建一个Android工程
2.打开res/layout/activity_main.xml
3.编辑activity_main.xml,添加如下代码:
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:text="移动" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="30dp"
android:text="联通" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="34dp"
android:text="电信" />
4.打开MainActivity.java,并编辑,添加如下代码:
public class MainActivity extends Activity implements OnClickListener{
private Button btn1 ;
private Button btn2 ;
private Button btn3 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview(){
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:10086"));
startActivity(intent);
break;
case R.id.button2:
Intent intent1 = new Intent(Intent.ACTION_CALL,Uri.parse("tel:10010"));
startActivity(intent1);
break;
case R.id.button3:
Intent intent2 = new Intent(Intent.ACTION_CALL,Uri.parse("tel:10000"));
startActivity(intent2);
break;
}
}
}
5.打开AndroidManifest.xml,添加拨打电话的权限:
<uses-permission android:name="android.permission.CALL_PHONE"/>
必须添加这条权限,否则无法拨打电话!!!
这样就结束了,就是这么简单