Android開發(fā)之電話撥號器實(shí)例詳解
本文實(shí)例分析了Android開發(fā)之電話撥號器的用法。分享給大家供大家參考,具體如下:
1、新建一個名為javacallPhone的安卓項目,并在cn.csdn.hr.activity包下建一個CallPhoneActivity.java類

2.打開res下的Layout下的main.xml進(jìn)行布局,設(shè)置布局方式為水平布局,再從左側(cè)分別拖入textview,text files下的Phone,和button按鈕,通過new String后效果如下:

3.打開CallPhoneActivity.java進(jìn)行編寫,具體代碼如下:
package cn.csdn.hr.activity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CallPhoneActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設(shè)置顯示的視圖界面
setContentView(R.layout.main);
//獲取該布局文件中相應(yīng)的組件
Button callBtn = (Button) findViewById(R.id.callBtn);
//為按鈕注冊點(diǎn)擊事件
callBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 撥號
//獲取輸入框 可編輯對象
EditText editText = (EditText) findViewById(R.id.editCallPhone);
//獲取輸入的電話號碼
Editable editable = editText.getText();
//轉(zhuǎn)換成字符串:editable.toString();
//意圖
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+editable.toString()));
//執(zhí)行
startActivity(intent);
}
});
}
}
4.打開項目清單文件Andriod Manifest.xml,選擇permissions,添加用戶打電話權(quán)限,如下圖:

5、在源代碼中出現(xiàn):
6、最后run as 安卓應(yīng)用,實(shí)現(xiàn)撥號功能
補(bǔ)充:android電話撥號器開發(fā)筆記
AndroidActivity:
package itcast.com;
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 class AndroidActivity extends Activity {
private EditText editex;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 得到按鈕
Button button = (Button) this.findViewById(R.id.bt_number);
editex = (EditText) this.findViewById(R.id.et_number);
// 設(shè)置button監(jiān)聽器
// 方式二:通過匿名內(nèi)部類
//
// button.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// String number = editex.getText().toString(); // 獲取電話號碼
// Intent intent = new Intent(); // 創(chuàng)建意圖
// intent.setAction(Intent.ACTION_CALL); // 設(shè)置打電話的意圖
// intent.setData(Uri.parse("telphone" + number)); // 撥打的具體號碼
// startActivity(intent);// 激活意圖
// }
// });
// button.setOnClickListener(this); // 當(dāng)前的實(shí)現(xiàn)這是AndroidActivity
}
public void btlisten(View v) {
String number = editex.getText().toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse(number));
startActivity(intent);
}
// // 方式三 實(shí)現(xiàn)了,OnClickListener
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// switch (v.getId()) {
// case R.id.bt_number:
// // 相應(yīng)的按鈕時間
// String number = editex.getText().toString();
// Intent intent = new Intent();
// intent.setAction(Intent.ACTION_CALL);
// intent.setData(Uri.parse(number));
// startActivity(intent);
// break;
// }
// }
// 方式一
// class MyOnClickListener implements OnClickListener {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// // 獲取文本域
// EditText editext = (EditText) AndroidActivity.this
// .findViewById(R.id.et_number);
// // 得到電話號碼
// String number = editext.getText().toString();
// Intent intent = new Intent(); // 創(chuàng)建意圖.代表要執(zhí)行的動作
// intent.setAction(Intent.ACTION_CALL); // 撥打電話的意圖
// intent.setData(Uri.parse(number));// 撥打的號碼 例如110
// startActivity(intent);// 激活意圖
// }
// }
}
mian.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/input_number" />
<EditText
android:id="@+id/et_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/print"
android:lines="1"
android:numeric="integer" />
<Button
android:id="@+id/bt_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bt_call"
android:onClick="btlisten"/>
</LinearLayout>
Strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, AndroidActivity!</string> <string name="app_name">AndroidCall</string> <string name="input_number">請輸入電話號碼</string> <string name="print">請輸入號碼</string> <string name="bt_call">撥打電話</string> </resources>
需求:
撥打電話的程序
步驟
1. 配置文件
strings.xml 負(fù)責(zé)的頁面是文字
main.xml 負(fù)責(zé)頁面的布局以及每個空間的ID
2.java的代碼:
通過ID 獲取button
監(jiān)聽button(參數(shù)view)
3.MyOnCliockListenr
處理事件:
得到EdiText,獲取電話號碼
創(chuàng)建意圖的引用:Intent intrnt = new Intent();
設(shè)置意圖對象的動作setAction()
撥打電話號碼setData()
開啟意圖statAchivate(Intent)
修改安全異常:AndroidMainfest.xml中的permissions-->android.permission.CALL_PHONE
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android電話撥號器實(shí)例詳解
- Android簡易電話撥號器實(shí)例詳解
- Android實(shí)現(xiàn)簡單的撥號器功能
- Android開發(fā)之電話撥號器和短信發(fā)送器實(shí)現(xiàn)方法
- Android學(xué)習(xí)筆記(二)之電話撥號器
- Android電話撥號器實(shí)現(xiàn)方法
- Android 2.3 撥號上網(wǎng)流程從源碼角度進(jìn)行分析
- Android Studio Intent隱式啟動,發(fā)短信,撥號,打電話,訪問網(wǎng)頁等實(shí)例代碼
- Android編程簡單實(shí)現(xiàn)撥號器功能的方法
相關(guān)文章
Android編程實(shí)現(xiàn)改變控件背景及形態(tài)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)改變控件背景及形態(tài)的方法,涉及Android控件布局設(shè)置的相關(guān)技巧,需要的朋友可以參考下2016-02-02
Android?實(shí)現(xiàn)APP可切換多語言步驟詳解
如果是單獨(dú)給app加上國際化,其實(shí)很容易,創(chuàng)建對應(yīng)的國家資源文件夾即可,如values-en,values-pt,這篇文章主要介紹了Android?實(shí)現(xiàn)APP可切換多語言,需要的朋友可以參考下2023-11-11
SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能(四)
這篇文章主要介紹了SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
android BitmapFactory.Options使用方法詳解
這篇文章主要為大家詳細(xì)介紹了android BitmapFactory.Options使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
android實(shí)現(xiàn)簡單的畫畫板實(shí)例代碼
畫畫板實(shí)現(xiàn)起來其實(shí)很簡單,我們只需要利用android給我們提供的Canvas類來操作就可以實(shí)現(xiàn)簡單的畫畫功能2014-01-01
Android自定義控件實(shí)現(xiàn)通用驗(yàn)證碼輸入框
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)通用驗(yàn)證碼輸入框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-01-01

