利用隐士Intent拨打电话
package com.luo.dllo.test;
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 MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_1, btn_2, btn_3, btn_call, btn_del;
private EditText ed;
private String phoneNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//绑定布局
setContentView(R.layout.activity_phone);
initView();
}
//自定义initView方法
private void initView() {//代码规范
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_call = (Button) findViewById(R.id.btn_call);
btn_del = (Button) findViewById(R.id.btn_del);
ed = (EditText) findViewById(R.id.ed);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_call.setOnClickListener(this);
btn_del.setOnClickListener(this);
}
//设置监听事件
@Override
public void onClick(View v) {
//获取号码
phoneNumber = ed.getText().toString();
switch (v.getId()) {
case R.id.btn_1:
//利用加等 += 拼接字符串
phoneNumber += "1";
break;
case R.id.btn_2:
phoneNumber += "2";
break;
case R.id.btn_3:
phoneNumber += "3";
break;
case R.id.btn_call:
//利用隐式Intent拨打电话号
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
startActivity(intent);//启动Intent
break;
case R.id.btn_del:
//利用substring截取号码,变相的实现删除
if(phoneNumber.length() > 0)
phoneNumber = phoneNumber.substring(0, phoneNumber.length() - 1);
break;
}
ed.setText(phoneNumber);
}
}
AndroidManifest文件如下:
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
布局文件就简单的给了几个按钮,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ed"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="30sp" />
<Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="30sp" />
<Button
android:id="@+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textSize="30sp" />
<Button
android:id="@+id/btn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨号"
android:textSize="30sp" />
<Button
android:id="@+id/btn_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:textSize="30sp" />
</LinearLayout>