ACTION.CALL

      通过一个Button来实现打电话  为了避免用户输入非电话号码的字符串,再拨打电话之前通过自定义isPhoneNumberVaild()以及Toast信息来提示用户

   定义一个Activity

 

package cn.mw.com.phone;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EX01_02Activity extends Activity {
	EditText ed_phone;
	Button call_phone;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ed_phone = (EditText) findViewById(R.id.phone_call);
		call_phone = (Button) findViewById(R.id.call_btn);
		call_phone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String strInput = ed_phone.getText().toString();
				try {
					if (isPhoneNumberValid(strInput) == true) {

						// 穿件一个新的Intent 运行ACTION.CALL的常数与通过uri将字符串带入

						Intent myIntentDial = new Intent(
								"android.intent.action.CALL", Uri.parse("tel:"
										+ strInput));
						//在StartActivity()方法里面带入自定义的Intent对象以运行拨打电话的工作
						startActivity(myIntentDial);
						ed_phone.setText("");
						
					}else{
						ed_phone.setText("");
						Toast.makeText(getApplicationContext(), "输入的电话号码格式不符", Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	protected boolean isPhoneNumberValid(String phoneNumber) {
		boolean isValid=false;
		String expression="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
		String expression2="^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
		CharSequence inputStr=phoneNumber;
		//创建Patten
		Pattern pattern=Pattern.compile(expression);
		//将Patten以参数传入Matcher作 Regular  expression
		Matcher matcher=pattern.matcher(inputStr);
		//创建Patten2
		Pattern pattern2=Pattern.compile(expression2);
		//将Patten2以参数传入Matcher作 Regular  expression
		Matcher matcher2=pattern.matcher(inputStr);
		if(matcher.matches()||matcher2.matches()){
			isValid=true;
		}
		// TODO Auto-generated method stub
		return isValid;
	}
}

 定义一个XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<EditText 
    android:id="@+id/phone_call"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="请输入电话号码"
    />
<Button 
    
    android:id="@+id/call_btn"
    android:layout_width="140dip"
    android:layout_height="wrap_content"
    android:text="拨打电话"
    />
</LinearLayout> 
 

 

然后再在EX01_02mainfest.xml里边添加

 <uses-permission android:name="android.permission.CALL_PHONE"/>

 

综上所述就完成了打电话的时候防止出现不是电话号码的情况以便于提醒用户

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值