Android拨号器的实现


首先:在string.xml中增加几个全局变量
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, SharpPhoneActivity!</string>
<string name="app_name">Sharp拨号器</string>
<string name="mobile">请输入手机号</string>
<string name="button">拨打此号</string>
</resources>
其次:修改main.xml进行界面的布局
<?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="@string/mobile" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/phoneno" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" />
</LinearLayout>

再次:编写代码
package com.sharpandroid.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 SharpPhoneActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View e) {
// 获取手机号
EditText phonenoText = (EditText) findViewById(R.id.phoneno);
String phoneno = phonenoText.getText().toString();
if ((phoneno != null) && (!"".equals(phoneno.trim()))) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:" + phoneno));
startActivity(intent);
}}});}}

最后在AndroidManifest.xml的配置文件中添加以下粗体字代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sharpandroid.phone" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SharpPhoneActivity" 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="8" />
<uses-permission android:name="android.permission.CALL_PHONE" />
</manifest>

相关内容