Activity文件
package com.example.administrator.jackapp;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class telActivity extends AppCompatActivity {
/**
* 该方法在Activity创建时被系统调用
* 可以做一些初始化操作
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置Activity布局文件
setContentView(R.layout.activity_tel);
// 声明一个Button对象button_tel,并通过资源id得到Button实例
Button button_tel = (Button)findViewById(R.id.bt_tel);
// 设置button_tel的点击事件监听
button_tel.setOnClickListener(new mTelClickListener());
}
/**
* 内部类 mTelClickListener
* 监听Button点击事件
* 内部类重写 onClick方法
*/
class mTelClickListener implements View.OnClickListener{
@Override
public void onClick(View view){
//声明一个EditText对象et_telNumber,并通过资源id得到EditText实例
EditText et_telNumber = (EditText)findViewById(R.id.et_telNumber);
//得到电话号码
String telNumber = et_telNumber.getText().toString();
//打电话
//创建意图对象intent
Intent intent = new Intent();
//设置动作
intent.setAction(Intent.ACTION_CALL);
//设置电话号码
//Uri统一资源标识符
intent.setData(Uri.parse("tel:"+telNumber));
//启动打电话应用
startActivity(intent);
}
}
}
layout文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".telActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:textSize="20sp"
android:text="请输入手机号码:"/>
<EditText
android:id="@+id/et_telNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:hint="input tel number "
android:inputType="phone"/>
<Button
android:id="@+id/bt_tel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="拨号"/>
</LinearLayout>
manifest文件中增加拨号权限
<uses-permission android:name="android.permission.CALL_PHONE" />