方法一、

1.创建一个callphone项目

2.在string.xml文件中添加text名称的配置

 

3.在activity_phone.xml文件中添加控件

 

4.在PhoneActivity.java中添加拨号相关的代码

 

5.运行代码,发现会出现如下错误

 

错误原因:没有添加拨号权限

在callphone Manifest(AndroidManifest.xml)中添加权限配置

 

6.运行代码,结果如下

 

 

 

7.总结

 

方法二、

 

public class PhoneActivity2 extends Activity {

 

    private EditText etPhone;

    private Button callBtn;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_phone);

        etPhone=(EditText) findViewById(R.id.etphone);

        callBtn = (Button) findViewById(R.id.callBtn);

        callBtn.setOnClickListener(new OnClickListener() {

          

           @Override

           public void onClick(View v) {

              //1.获取拨号的电话号码

              String phone=etPhone.getText().toString();

              //土司效果

              Toast.makeText(PhoneActivity2.this, "拨号"+phone, Toast.LENGTH_LONG).show();

              //2.定义拨号的意图

              Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phone));

              //3.执行拨号的意图

              startActivity(intent);

           }

       });

    }

}