Android實現撥號功能


撥號界面:
   
   
   
           
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5. <!-- match_parent: 匹配父元素,父元素有多寬,我就有多寬;父元素有多高,我就有多高 -->
  6. <!-- wrap_content:包裹內容,我的內容有多高,我就有多高,我的內容有多寬,我就有多寬 -->
  7. <EditText
  8. android:id="@+id/et_input_num"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:inputType="phone" >
  12. </EditText>
  13. <!-- android:text: 指定按鈕上的顯示文字 -->
  14. <!-- android:onClick: 定義點擊事件 -->
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:onClick="call"
  19. android:text="打電話" />
  20. </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(thisphone_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.完全自定義顯示方式








免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM