制作android电话拨号器,需要设计界面、添加事件代码、添加权限和测试几个步骤,具体如下
一、设计拨号界面
android拨号界面如下:
对应的布局文件如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/input_text"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/mobile"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dial_btn"
android:id="@+id/dialBtn"
/>
</LinearLayout>
二、编写单击事件代码
package com.pei.phone;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//为button设置监听器
Button button = (Button) this.findViewById(R.id.dialBtn);
button.setOnClickListener(new ButtonClickListener());
}
//为button按钮设置监听器,监听器使用外部类
public class ButtonClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText text = (EditText)findViewById(R.id.mobile);
String strNum = text.getText().toString();
//新建一个intent对象,进行调用系统拨号功能
Intent intent = new Intent();
//设定intent的动作
intent.setAction("android.intent.action.CALL");
//设定intent的数据
intent.setData(Uri.parse("tel:"+ strNum));
//开始运行intent
startActivity(intent);
}
}
}
三、添加拨打电话号码权限
在AndroidMainfest.xml中添加权限,最好在application标签前面,在后面会有waring,代码如下
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pei.phone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission
android:name="android.permission.CALL_PHONE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
四、软件测试
运行两个模拟器,如下图
拨打效果图如下
测试成功。
虽然很多人写过这样的文章了,我觉得自己还是写下来,加深记忆。。