一、定义名称strings.xml
<resources>
<string name="app_name">电话拨号器</string>
<string name="action_settings">Settings</string>
<string name="please_enter_phone_number">请输入电话号码</string>
<string name="call">拨打</string>
</resources>
二、设计拨号界面main.xml
<RelativeLayout 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"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et_phone_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/please_enter_phone_number"
android:inputType="phone" />
<Button
android:id="@+id/btn_dial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_phone_number"
android:text="@string/call" />
</RelativeLayout>
三、编写MyActivity.java
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MyActivity extends Activity {
private Button mDialButton;
private EditText mNumberEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDialButton = (Button) findViewById(R.id.btn_dial);
mDialButton.setOnClickListener(new ButtonClickListener());
}
private class ButtonClickListener implements OnClickListener {
/**
* 当按钮被点击的时候调用的方法
*/
@Override
public void onClick(View v) {
mNumberEditText = (EditText) MyActivity.this
.findViewById(R.id.et_phone_number);
//获取电话号码
String number = mNumberEditText.getText().toString().trim();
// 判断电话号码是否为空
if (!TextUtils.isEmpty(number)) {
//意图想干一件什么事情
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
//数据都是通过Uri进行包装
intent.setData(Uri.parse("tel:" + number));
startActivity(intent);
} else {
Toast.makeText(MyActivity.this,
R.string.please_enter_phone_number, Toast.LENGTH_SHORT)
.show();
}
}
}
}
四、AndroidManifest.xml中添加拨号权限
<uses-permission android:name="android.permission.CALL_PHONE" />
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。