今天我们来实现一个Android模拟器的打电话功能。
首先,我们分析一下这个程序的界面都需要那些控件:
界面做的简单一些,重在实现其功能,一行文本提示,一个文本框,一个Button按钮,如图:
建立项目工程实现功能
new一个Android Project项目(Phone),点开res下values中的string.xml文件,在resources根节点中添加我们需要用到的键值对,如图:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Phone</string><!-- 这个eclipse是自动添加的 -->
<!-- 手动添加所需的键值对 -->
<string name="input_info">请输入要拨打的号码</string>
<string name="dial_caption">拨打</string>
</resources>
在gen中的R.java中的string内部类下将会自动生成这两个键值对的ID:
public static final class string {
public static final int app_name=0x7f040000;
public static final int dial_caption=0x7f040002;
public static final int input_info=0x7f040001;
}
显示视图的布局
打开res下layout中的main.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" >
<!-- 添加文本标签
android:layout_width定义该组件的宽度fill_parent(强制性地使构件扩展,以填充布局单元内尽可能多的空间。)
android:layout_height顶一顶该控件的高度wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。
android:text=“@string/input_Info” 引用R.java中的内部类string的静态成员变量input_info,input_info即我们刚刚在string.xml文件中添加的键值对<string name="input_info">请输入要拨打的号码</string>的ID
-->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/input_info" />
<!-- 添加可编辑的文本框 android:layout_width android:layout_height同上
android:id="@+id/phone_number" 引用R.java中的id内部类下的phone_number成员变量,如果有直接引用如没有则添加
-->
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/phone_number"/>
<!-- 添加Buttron按钮控件
属性android:text="@string/dial_caption" Button按钮的名字引用R.java文件中string内部类的dial_caption成员变量 ,dial_caption即我们刚刚在string.xml文件中添加的键值对<string name="dial_caption">请输入要拨打的号码</string>的ID
android:id="@+id/dial_btn 引用R.java中的id内部类下的dial_btn成员变量,该变量同样是我们刚才在string.xml中添加的键值对的名字,如果有直接引用如没有则添加
-->
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/dial_caption" android:id="@+id/dial_btn"/> </LinearLayout>
现在我们的准备工作已经完善,下面就用java代码来实现我们的功能
打开PhoneActivity.java这个类
package cn.sword.activity;
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 PhoneActivity extends Activity {
//声明EditTest Button控件
EditText numberEt;
Button dialBtn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//设置视图文件
findViews();//调用findViews方法得到EditTest控件与Button控件
//将dialBtn按钮控件添加点击监听事件
dialBtn.setOnClickListener(new OnClickListener() {
@Override
//当点击该Button时触发该监听事件的此方法,执行操作
public void onClick(View v) {
//获取文本框中的内容
String phone_number = numberEt.getText().toString();
//利用trim()方法去除空格
phone_number = phone_number.trim();
if(phone_number!=null && !phone_number.equals("")){
//创建一个意图,作用即触发此监听时要执行的动作
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone_number));
//将意图添加进该Activity
PhoneActivity.this.startActivity(intent);
}
}
});
}
public void findViews(){
numberEt = (EditText) this.findViewById(R.id.phone_number);//获取可编辑文本框的ID
dialBtn = (Button) this.findViewById(R.id.dial_btn);//获取Button按钮的ID
}
}
接下来非常重要的就是为此Activity添加权限---拨打电话的权限
打开AndroidManifest.xml ,添加权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.sword.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<!-- 添加打电话的权限 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<!-- android:icon为应用程序的图标,引用res文件夹drawable中图片在R.java内drawable内部类的ic_launcher静态成员变量
android:label为应用程序的名称,引用res下values文件夹下string.xml文件中键值对在 -->
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".PhoneActivity" >
<intent-filter >
<!-- 程序的入口 -->
<action android:name="android.intent.action.MAIN" />
<!-- android.intent.category.LAUNCHER相当于在手机应用程序菜单中添加一个该程序的快捷方式 -->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
启动两个模拟器,测试拨打电话
OK ,测试成功,如有错误请帮助指出,谢谢。