Android实现拨号器电话

xml中的代码实现

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
 >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title" />
   <EditText 
       android:id="@+id/phonenumber"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="请输入手机号码"
       android:inputType="phone"
       android:layout_below="@id/title"
       
       />
	<Button 
	    android:id="@+id/but"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:onClick="call"
	    android:text="拨打"
	    android:layout_below="@id/phonenumber"
	    />
</RelativeLayout>

mainActivity中代码的实现

public class MainActivity extends Activity {
	private EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit=(EditText)findViewById(R.id.phonenumber);
    }

	public  void call(View v){
	System.out.println("被点击了");
	String num=edit.getText().toString();
	 
	Intent intent=new Intent();//创建一个意图
	intent.setAction(intent.ACTION_CALL);//指定动作为拨打电话
	intent.setData(Uri.parse("tel:"+num));//指定要播出的电话号码
	startActivity(intent);
	
	}
  
}

同时还需要给我们的应用程序添加一个可以拨打号码的权限

<!-- 添加拨打电话的权限 -->
	<uses-permission 
	    android:name="android.permission.CALL_PHONE"
	    />