Android開發系列(一):電話撥號器的實現

來源:互聯網
上載者:User

標籤:android開發   國際化   textview   layout   

目標,實現簡單的電話撥號功能。


首先,可以看到上邊有幾個控制項,上邊有幾個文字。

在我們建立好了工程之後,我們首先可以在res目錄下的values目錄的strings.xml中把這幾個文字添加進去,為了更好地實現國際化。

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, MainActivity!</string>    <string name="app_name">電話拔號器</string>    <string name="mobile">請輸入手機號</string>    <string name="button">拔號</string></resources>
接下來,我們需要實現這個Activity視窗。可以看出,這個介面中有三個組件:第一行的”請輸入手機號“,第二行的手機號文字框,第三行的“撥號”按鈕。

所以,我們在res目錄下的layout目錄的main.xml設定檔中添加這幾個組件:

       <TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/mobile"    /><EditTextandroid:layout_width="fill_parent"     android:layout_height="wrap_content"    android:id="@+id/mobile"    /><Buttonandroid:layout_width="wrap_content"     android:layout_height="wrap_content"    android:text="@string/button"    android:id="@+id/button"    />
做到這裡的時候,介面就是已經做好了。


這個時候,我們需要通過java來實現這個撥號器的撥號功能了:

我們需要編寫MainActivity.java:

package cn.itcast.phone;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {    private EditText mobileText;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mobileText = (EditText) findViewById(R.id.mobile);//獲得文本輸入框:通過main中的id為mobile的屬性        Button button = (Button) this.findViewById(R.id.button); //獲得Button        button.setOnClickListener(new ButtonClickListener());//設定點擊事件的監聽器    }     private final class ButtonClickListener implements View.OnClickListener{public void onClick(View v) { //預設實現方法,當按鈕被點擊就會被調用String number = mobileText.getText().toString(); //通過文本輸入框,獲得文本輸入框的值Intent intent = new Intent(); //得到一個意圖對象intent.setAction("android.intent.action.CALL"); //傳遞動作名稱intent.setData(Uri.parse("tel:"+ number)); //傳遞資料startActivity(intent);//方法內部會自動為Intent添加類別:android.intent.category.DEFAULT}    }}

我們來分析下程式的過程:

首先,調用setContentView(R.layout.main);來獲得main視窗,然後通過findViewById(R.id.mobile)獲得在main.xml中id為mobile的文本輸入框。

接下來,通過findViewById(R.id.button);獲得在main.xml中id為button的按鈕。

繼續,用setOnclickListener(new ButtonClickListener());來設定點擊事件的監聽器。


再下邊,我們需要編寫一個ButtonClickListener()類,用final來標記不能夠被繼承。

在這個類中添加未被實現的方法,當點擊的時候會被調用:Onckick(View v)。

在方法中,調用mobileText.getText().toString()來獲得文字框輸入的手機號碼。

然後,通過intent傳遞撥打到電話的動作名稱:“android.intent.action.CALL"

通過intent傳遞資料:intent.setDate(Uri.parse("tel:"+number));

接下來開啟intent意圖。


因為撥打到電話涉及到資料的隱私,所以我們需要在AndroidMainfest.xml中添加一個安全控制:

<uses-permission android:name="android.permission.CALL_PHONE"/>

到了這個時候,一個電話撥號器基本就完成了。




Android開發系列(一):電話撥號器的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.