失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 打电话android代码 android 拨打电话例子源码(亲测可用)

打电话android代码 android 拨打电话例子源码(亲测可用)

时间:2021-02-06 08:54:29

相关推荐

打电话android代码 android 拨打电话例子源码(亲测可用)

【实例简介】

【实例截图】

【核心代码】

package androidCall.pack;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import android.app.Activity;

import android.content.Intent;

import .Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class androidCall extends Activity {

/** Called when the activity is first created. */

private Button button;

private EditText text;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

text = (EditText)findViewById(R.id.text);

button = (Button)findViewById(R.id.button);

button.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

try {

String inputStr = text.getText().toString();

if(isPhoneNumberValid(inputStr) == true){

Intent myIntentDial = new Intent(

Intent.ACTION_CALL,Uri.parse("tel:" inputStr)

);

startActivity(myIntentDial);

text.setText("");

}else{

text.setText("");

Toast.makeText(getBaseContext(), "电话格式不对", Toast.LENGTH_LONG).show();

}

} catch (Exception e) {

// TODO: handle exception

System.out.println(e.getMessage());

}

}

});

}

public static 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;

Pattern pattern = pile(expression);

Matcher matcher = pattern.matcher(inputStr);

Pattern pattern2 = pile(expression2);

Matcher matcher2 = pattern2.matcher(inputStr);

if(matcher.matches()||matcher2.matches()){

isValid = true;

}

return isValid;

}

}

如果觉得《打电话android代码 android 拨打电话例子源码(亲测可用)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。