运行效果如下图所示:
布局文件activity_main.xml的代码:
<span style="font-size:18px;"><span style="font-size:18px;"><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"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
tools:context="com.gjj.phone.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phoneText" />
<EditText
android:id="@+id/phoneNum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="call"
android:text="@string/button" />
</LinearLayout></span></span>
strings.xml文件中的代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">电话拨号器</string>
<string name="phoneText">请输入手机号</string>
<string name="button">拨号</string>
<string name="action_settings">Settings</string>
</resources>
<span style="font-size:18px;">package com.gjj.phone;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void call(View v) {
EditText phoneText = (EditText) findViewById(R.id.phoneNum);
String num = phoneText.getText().toString();
// 创建意图对象,通过意图过滤器激活系统自带的电话拨号器的Activity
Intent intent = new Intent();
intent.setAction("android.intent.action.CALL");
intent.setData(Uri.parse("tel:" + num));
startActivity(intent);
}
}</span>
最后在AndroidManifest.xml中要添加权限:
<span style="font-size:18px;"><uses-permission android:name="android.permission.CALL_PHONE"/></span>