標籤:
撥號介面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- match_parent: 匹配父元素,父元素有多寬,我就有多寬;父元素有多高,我就有多高 -->
<!-- wrap_content:包裹內容,我的內容有多高,我就有多高,我的內容有多寬,我就有多寬 -->
<EditText
android:id="@+id/et_input_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" >
</EditText>
<!-- android:text: 指定按鈕上的顯示文字 -->
<!-- android:onClick: 定義點擊事件 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="call"
android:text="打電話" />
</LinearLayout>
// v: 代表你點擊的控制項public void call(View view) { EditText editText = (EditText) findViewById(R.id.input_num); // 擷取使用者輸入的手機號 String phone_number = editText.getText().toString(); //刪除字串首部和尾部的空格
phone_number = phone_number.trim(); /** * context : 上下文,環境 <br/> * text : 要顯示的內容 <br/> * duration : 顯示的時常 //期間 * */ Toast.makeText(this, phone_number, Toast.LENGTH_LONG); if(phone_number != null && !phone_number.equals("")) { //調用系統的撥號服務實現電話撥打功能 // 打電話 // Intent : 意圖.我想去做一件事 Intent t = new Intent(); // Action:動作.我具體想做什麼事 // Intent.ACTION_DIAL: 啟用撥號介面 // Intent.ACTION_CALL: 直接撥打到電話 t.setAction(Intent.ACTION_CALL); // Data: 資料.具體的動作所需要的附加資料 //封裝一個撥打到電話的intent,並且將電話號碼封裝成一個Uri對象傳入 t.setData(Uri.parse("tel:" + phone_number)); // 通知系統你去幫我幹活吧 startActivity(t);} }
最後在在AndroidManifest.xml裡面添加<uses-permission android:name="android.permission.CALL_PHONE"/>
備忘:Toast.makeText()使用方法1.預設的顯示方式
2.自訂顯示位置3.帶圖片的4.完全自訂顯示方式
來自為知筆記(Wiz)
Android實現撥號功能