Android案例之撥打到電話

來源:互聯網
上載者:User

步驟一: 
首先在layout下的main.xml檔案中構建出撥打到電話的介面(建議利用工具去拖動,因為這樣速度比較快,而且不用去別的檔案中去定義一些東西) 
Main.xml 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/tipMsg" /> 
    <EditText 
        android:id="@+id/phone" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="phone" > 
        <requestFocus /> 
    </EditText> 
    <Button 
        android:id="@+id/callBtn" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/num" /> 
步驟二: 
values下的strings.xml檔案 
    <string name="app_name">Phone</string> 
    <string name="tipMsg">請輸入正確的電話號碼</string> 
<string name="num">撥號</string> 
步驟三: 
在PhoneActivity.java檔案中進行必須的配置 
public class PhoneActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        //設定顯示的視圖 www.2cto.com  
        setContentView(R.layout.main); 
        //擷取當前這個視圖的組件 
        Button callBtn=(Button) findViewById(R.id.callBtn); 
        callBtn.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                //觸發按鈕後的一些操作 
                EditText phone = (EditText) findViewById(R.id.phone); 
                //意圖     要幹什麼        //擷取文字框中的內容      phone.getText(); 
                Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone.getText())); 
                //開始處理意圖        執行 
                PhoneActivity.this.startActivity(intent); 
            } 
        }); 
    } 
} 
步驟四: 
在AndroidManifest.xml檔案中添加可以撥打到電話的許可權(建議在可視化介面去添加) 
方式:開啟AndroidManifest.xml檔案,點擊Permissions-——>點擊添加,添加Users Permission——>在name框中選擇android.permission.CALL_PHONE——>完成 
步驟五: 
右擊運行即可 

 


作者 w_l_j

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.