功能实现:一个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>
至此可以实现拨号功能。