Android电话拨号器
android电话拨号器的设计
具体的运行界面如上图所示。
具体操作过程:
1、新建一个Android项目。在Eclipse中依次单击“File”->“NEW”->"Android Project"
2、编写string.xml文件。具体代码如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="app_name">Phone拨号器</string> 5 <string name="hello_world">Hello world!</string> 6 <string name="action_settings">Settings</string> 7 <string name="mobile">请输入手机号码:</string> 8 <string name="button">拨打电话</string> 9 10 </resources>
3、编写main.xml文件。具体代码如下:
1 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.phone.MainActivity$PlaceholderFragment" > 10 <LinearLayout android:orientation="vertical" 11 android:layout_width="fill_parent" 12 android:layout_height="fill_parent"> 13 <TextView 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:text="@string/mobile" 17 /> 18 <EditText 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" 21 android:id="@+id/phoneNum"/> 22 <Button 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:text="@string/button" 26 android:id="@+id/btnPhone"/> 27 </LinearLayout> 28 </RelativeLayout>
4、处理按钮的单击事件。具体代码如下:
1 package com.example.phone; 2 3 import android.app.Activity; 4 import android.app.Fragment; 5 import android.content.Intent; 6 import android.net.Uri; 7 import android.os.Bundle; 8 import android.view.LayoutInflater; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.View; 12 import android.view.ViewGroup; 13 import android.widget.Button; 14 import android.widget.EditText; 15 16 public class MainActivity extends Activity { 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 23 if (savedInstanceState == null) { 24 getFragmentManager().beginTransaction() 25 .add(R.id.container, new PlaceholderFragment()).commit(); 26 } 27 } 28 29 @Override 30 public boolean onCreateOptionsMenu(Menu menu) { 31 32 // Inflate the menu; this adds items to the action bar if it is present. 33 getMenuInflater().inflate(R.menu.main, menu); 34 return true; 35 } 36 37 @Override 38 public boolean onOptionsItemSelected(MenuItem item) { 39 // Handle action bar item clicks here. The action bar will 40 // automatically handle clicks on the Home/Up button, so long 41 // as you specify a parent activity in AndroidManifest.xml. 42 int id = item.getItemId(); 43 if (id == R.id.action_settings) { 44 return true; 45 } 46 return super.onOptionsItemSelected(item); 47 } 48 49 /** 50 * A placeholder fragment containing a simple view. 51 */ 52 public static class PlaceholderFragment extends Fragment { 53 54 public PlaceholderFragment() { 55 } 56 57 @Override 58 public View onCreateView(LayoutInflater inflater, ViewGroup container, 59 Bundle savedInstanceState) { 60 View rootView = inflater.inflate(R.layout.fragment_main, container, 61 false); 62 63 final EditText phoneText=(EditText)rootView.findViewById(R.id.phoneNum); 64 Button btnPhone=(Button)rootView.findViewById(R.id.btnPhone); 65 btnPhone.setOnClickListener(new View.OnClickListener() { 66 67 @Override 68 public void onClick(View v) { 69 // TODO Auto-generated method stub 70 String phoneNum=phoneText.getText().toString(); 71 if(phoneNum!=null&&(!"".equals(phoneNum.trim()))) 72 { 73 Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneNum)); 74 startActivity(intent); 75 } 76 } 77 }); 78 return rootView; 79 } 80 } 81 82 }
5、申请拨号权限。我们需要在功能清单文件AndroidManifest.xml中申请权限,在</application>标签后面加上<uses-permission>具体代码如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.phone" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="14" 9 android:targetSdkVersion="19" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.example.phone.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 <uses-permission android:name="android.permission.CALL_PHONE"/> 27 </manifest>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。