实验目的

1.掌握简单的组件(TextView,EditText,Button)使用

2.掌握点击事件的使用

电话拔号器效果图


界面布局代码:

<RelativeLayoutxmlns: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"><TextViewandroid:id="@+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/title"/><EditTextandroid:id="@+id/phoneNumber"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="phone"android:layout_below="@id/title"/><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/callPhone"android:layout_below="@id/phoneNumber"/></RelativeLayout>

Java代码实现拨打电话:

@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);Buttonbutton=(Button)findViewById(R.id.button);button.setOnClickListener(this);}@OverridepublicvoidonClick(Viewv){//1.获取输入的电话号码EditTextet=(EditText)findViewById(R.id.phoneNumber);Stringnumber=et.getText().toString();//2.创建打电话意图Intentintent=newIntent();intent.setAction(intent.ACTION_CALL);intent.setData(Uri.parse("tel:"+number));startActivity(intent);}


注意 在AndroidManifest.xml添加拨打电话的权限

<uses-permissionandroid:name="android.permission.CALL_PHONE"/>

测试: