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虛擬機器中測試,測試圖如下:
以上是簡單的撥打到電話的實現,希望你能夠理解!