天天看點

Android:電話撥号器

Android:電話撥号器
 PhoneActivity.java

  1. public class PhoneActivity extends Activity implements OnClickListener { 
  2.     /** 
  3.      * Button 呼出電話按鈕 
  4.      */ 
  5.     private Button btn_callon; 
  6.     /** 
  7.      * EditText 号碼輸入框 
  8.      */ 
  9.     private EditText et_phoneNum; 
  10.     /** 
  11.      * String 使用者輸入電話号碼字元串 
  12.      */ 
  13.     private String phoneNum; 
  14.     /** Called when the activity is first created. */ 
  15.     @Override 
  16.     public void onCreate(Bundle savedInstanceState) { 
  17.         super.onCreate(savedInstanceState); 
  18.         setContentView(R.layout.main); 
  19.         initView(); 
  20.         btn_callon.setOnClickListener(this); 
  21.     } 
  22.     /** 
  23.      * 初始化界面:擷取元件 
  24.      */ 
  25.     public void initView() { 
  26.         btn_callon = (Button) findViewById(R.id.btn_id_phone_callon); 
  27.         et_phoneNum = (EditText) findViewById(R.id.et_id_phone_phoneNum); 
  28.     } 
  29.     public void onClick(View v) { 
  30.         phoneNum = et_phoneNum.getText().toString(); 
  31.         if (phoneNum != null && !"".equals(phoneNum.trim())) { // 判斷手機号是否為空 
  32.             Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"// 把tel字首傳給系統之後,作業系統會自動調用撥号服務來撥打該号碼 
  33.                     + phoneNum)); 
  34.             startActivity(intent); 
  35.         } 
  36.     } 

main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" > 
  6.     <TextView 
  7.         style="@style/wrap_content" 
  8.         android:text="@string/tv_text_phone_phonetext" /> 
  9.     <EditText 
  10.         android:id="@+id/et_id_phone_phoneNum" 
  11.         style="@style/fill_parent" 
  12.         android:inputType="number" 
  13.         android:singleLine="true" /> 
  14.     <Button 
  15.         android:id="@+id/btn_id_phone_callon" 
  16.         style="@style/wrap_content" 
  17.         android:text="@string/btn_text_phone_callon" /> 
  18. </LinearLayout> 

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="tv_text_phone_phonetext">請輸入電話号碼:</string> 
  4.     <string name="btn_text_phone_callon">呼出電話</string> 
  5.     <string name="app_name">Phone</string> 
  6. </resources> 

style.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <style name="wrap_content"> 
  4.         <item name="android:layout_width">wrap_content</item> 
  5.         <item name="android:layout_height">wrap_content</item> 
  6.     </style> 
  7.     <style name="fill_parent"> 
  8.         <item name="android:layout_width">fill_parent</item> 
  9.         <item name="android:layout_height">wrap_content</item> 
  10.     </style> 
  11. </resources> 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     package="com.fisnail" 
  4.     android:versionCode="1" 
  5.     android:versionName="1.0" > 
  6.     <uses-sdk android:minSdkVersion="8" /> 
  7.     <!-- 撥打電話權限 --> 
  8.     <uses-permission android:name="android.permission.CALL_PHONE" /> 
  9.     <application 
  10.         android:icon="@drawable/ic_launcher" 
  11.         android:label="@string/app_name" > 
  12.         <activity 
  13.             android:name=".PhoneActivity" 
  14.             android:label="@string/app_name" > 
  15.             <intent-filter> 
  16.                 <action android:name="android.intent.action.MAIN" /> 
  17.                 <category android:name="android.intent.category.LAUNCHER" /> 
  18.             </intent-filter> 
  19.         </activity> 
  20.     </application> 
  21. </manifest>