本文实例讲述了android电话拨号器实现方法。分享给大家供大家参考。具体如下:
以下案例模拟android电话拨号器的实现
androidmanifest.xml清单列表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?xml version= "1.0" encoding= "utf-8" ?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
package = "com.ljq.phone"
android:versioncode= "1"
android:versionname= "1.0" >
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ".mainactivity"
android:label= "@string/app_name" >
<intent-filter>
<action android:name= "android.intent.action.main" />
<category android:name= "android.intent.category.launcher" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minsdkversion= "7" />
<uses-permission android:name= "android.permission.call_phone" />
</manifest>
|
main.xml布局文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical" android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
<textview android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:text= "请输入电话号码" />
<edittext android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:id= "@+id/phone" />
<button android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "拔打此号码"
android:id= "@+id/button" />
</linearlayout>
|
mainactivity类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.ljq.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 phone= null ;
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
phone=(edittext) this .findviewbyid(r.id.phone);
button button=(button) this .findviewbyid(r.id.button);
button.setonclicklistener( new view.onclicklistener(){
public void onclick(view v) {
string tel=phone.gettext().tostring();
//方法一, 使用intent目的: 激活android组件
//intent intent=new intent();
//intent.setaction("android.intent.action.call");
//intent.setdata(uri.parse("tel:"+tel));
//方法二
intent intent= new intent( "android.intent.action.call" , uri.parse( "tel:" +tel));
//方法的内部会自动为intent对象设置类别:android.intent.category.default
startactivity(intent);
}
});
}
}
|
运行结果:
界面初始化:
电话拨打效果:
希望本文所述对大家的android程序设计有所帮助。