Android初學項目——撥號器

來源:互聯網
上載者:User

標籤:

第一次寫這個,還不知道格式呢。

學習android還是有些時間了,沒有什麼具體的步驟,所以路線總是亂的,畢竟自學自己摸索困難不是一點點,所以收錄一下寫過的小項目,用來積累經驗

的效果。

具體步驟:

  1.頁面的xml檔案,代碼如下:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     tools:context=".MainActivity" > 6  7     <TextView 8         android:id="@+id/txt_hello" 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content"11         android:text="@string/hello_world" />12     <EditText 13         android:id="@+id/phone_number"14         android:layout_width="match_parent"15         android:layout_height="wrap_content"16         android:hint="@string/phone_number_tip"17         android:inputType="phone"18         android:layout_below="@id/txt_hello"19         android:paddingTop="10dip"/>20     <Button 21         android:id="@+id/btn_call"22         android:layout_width="wrap_content"23         android:layout_height="wrap_content"24         android:layout_below="@id/phone_number"25         android:layout_alignParentRight="true"26         android:text="@string/call_phone"/>27 28 </RelativeLayout>

  只有一個EditText和一個Button按鈕(那個TextView沒去刪,所以也加在裡面了),所有的文本也放在strings.xml中,慢慢養成一個好習慣嘛

  2.接下來就是java檔案了,代碼不多

public class MainActivity extends Activity implements OnClickListener{    private EditText phoneNumber;    private Button btnCall;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                phoneNumber = (EditText) findViewById(R.id.phone_number);        btnCall = (Button) findViewById(R.id.btn_call);        btnCall.setOnClickListener(this);    }    @Override    public void onClick(View arg0) {        // TODO Auto-generated method stub        switch (arg0.getId()) {        case R.id.btn_call:            Intent intent=new Intent(Intent.ACTION_DIAL);            String number=phoneNumber.getText().toString();            intent.setData(Uri.parse("tel:"+number));            startActivity(intent);            break;        default:            break;        }    }}

  習慣了通過直接繼承OnClickListener介面,實現OnClick方法來實現按鈕的監聽(這要用於一個頁面中的多個按鈕),通過View.getId()的方法,來獲得匹配點擊的按鈕的ID,利用隱式的Intent.ACTION_DIAL,來點用手機內建的撥號服務。將獲得的手機號碼通過Uri.parse("tel:"+number)傳到撥打到電話的介面,,格式一定是“tel:”後面接數字型大小碼。

  3.當然,調用這種系統內部服務需要許可權的,這時就要去資訊清單檔AndroidManifest.xml中加入許可權

這個選中的就是撥打到電話的許可權,當然也可以直接在資訊清單檔中加入如下的代碼

 

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

就這樣,開啟模擬器,運行程式,就可以看到效果了,也可以試試輸入手機號點擊撥打。

內容不多,也是小小的經驗一筆,裡面的用詞可能有誤,本人新手,有誤也請大家指出。同時也希望正在學習android的人們有個更好地交流。

 

 

 

 

  

 

 

 

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.