Android簡單的打電話程式的實現_學員原創

來源:互聯網
上載者:User
Android簡單的打電話程式的實現

第一步:介面設計

第二步:整體實現:

打電話是手機的主要功能,我的Android第一個程式就是實現打電話功能。

1、首先在values目錄下的strings.xml中添加相應的文字資訊

<string name="input_info">請輸入您要撥打的號碼:</string><string name="dial_caption">撥打</string>

2、然後在layout中實現頁面的布局,在並且引入相應的文本資訊

<!—實現頁面標題的插入--> <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/input_info" />   <!—定義文字框-->   <EditText        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/phone_number"/><!—定義一個按鈕-->   <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/dial_btn"        android:text="@string/dial_caption"        />

3、撰寫java代碼:

import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText; public classPhoneActivity extends Activity {//定義文字框和按鈕   EditText numberEt;   Button dialBtn;   publicvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);               findViews();        dialBtn.setOnClickListener(new OnClickListener(){             public void onClick(View v) {                //調用系統的撥號服務實現撥打功能                Stringphone_number = numberEt.getText().toString();                phone_number= phone_number.trim();                if(phone_number != null &&!phone_number.equals("")){                    //封裝一個撥打到電話的intent,並且將電話號碼封裝成一個Uri對象傳入                Intentintent = newIntent(Intent.ACTION_CALL,Uri.parse("tel:" + phone_number));                PhoneActivity.this.startActivity(intent);                }            }        });      }   public void findViews(){    numberEt = (EditText) this.findViewById(R.id.phone_number);    dialBtn = (Button) this.findViewById(R.id.dial_btn);   }}

4、不要忘了給它許可權哦,在AndroidManifest.xml中寫入

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

5、啟動兩次模擬器,類比5556給5554打電話.

在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.