1.在res/layout/activity_main.xml文件中,编辑界面控件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.call.MainActivity"
tools:ignore="MergeRootFrame"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/phoneLable" />
<EditText
android:id="@+id/phoneText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/phoneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phoneButton" />
</LinearLayout>
2.在res/values/string.xml中,定义需要用到的字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Call</string>
<string name="action_settings">Settings</string>
<string name="phoneLable">请输入号码</string>
<string name="phoneButton">拨号</string>
</resources>
3.在MainActivity.java中,编写电话拨号器的逻辑代码
package com.example.call;
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 phoneText = null;
private Button phoneButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneText = (EditText) this.findViewById(R.id.phoneText);
phoneButton = (Button) this.findViewById(R.id.phoneButton);
phoneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String phoneStr = phoneText.getText().toString();
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phoneStr));
startActivity(intent);
}
});
}
}
4.在AndroidManifest.xml中,声明打电话权限“android.permission.CALL_PHONE”
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.call"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.call.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>
</manifest>
运行效果:
3092

被折叠的 条评论
为什么被折叠?



