功能實現:一個EditView 一個撥打按鈕,輸入號碼跳轉到撥號界面
界面布局:activity_call.xml
復制代碼
//線性垂直布局:一個EditView文本、一個Button按鈕
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical"
6 android:paddingBottom="@dimen/activity_vertical_margin"
7 android:paddingLeft="@dimen/activity_horizontal_margin"
8 android:paddingRight="@dimen/activity_horizontal_margin"
9 android:paddingTop="@dimen/activity_vertical_margin"
10 tools:context=".PhoneActivity" >
11
12 <TextView
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:text="請輸入電話" />
16
17 <EditText
18 android:id="@+id/editText1"
19 android:layout_width="fill_parent"
20 android:layout_height="wrap_content"
21 android:inputType="phone" />
22
23 <Button
24 android:id="@+id/callsumbit"
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content"
27 android:layout_gravity="center"
28 android:text="撥打電話" />
29
30 </LinearLayout>
復制代碼
CallActivity的Create方法
復制代碼
1 protected void onCreate(Bundle savedInstanceState) {
2 super.onCreate(savedInstanceState);
3 setContentView(R.layout.activity_call);
4 Button btn = (Button) findViewById(R.id.callsumbit);
5 btn.setOnClickListener(new OnClickListener() {
6
7 @Override
8 public void onClick(View v) {
9 EditText etNumber = (EditText) findViewById(R.id.editText1);
10
11 String number = etNumber.getText().toString();
12 Intent intent = new Intent();
13 intent.setAction(Intent.ACTION_CALL);
14 intent.setData(Uri.parse("tel:" + number));
15 startActivity(intent);
16 }
17 });
18 }
復制代碼
增加撥打電話的權限:AndroidManifest.xml
復制代碼
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.ccec.callphone"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="8"
9 android:targetSdkVersion="18" />
10 <uses-permission android:name="android.permission.CALL_PHONE"/>
11
12 <application
13 android:allowBackup="true"
14 android:icon="@drawable/ic_launcher"
15 android:label="@string/app_name"
16 android:theme="@style/AppTheme" >
17 <activity
18 android:name="com.ccec.callphone.CallActivity"
19 android:label="@string/app_name" >
20 <intent-filter>
21 <action android:name="android.intent.action.MAIN" />
22
23 <category android:name="android.intent.category.LAUNCHER" />
24 </intent-filter>
25 </activity>
26 </application>
27
28 </manifest>