【实例简介】
【实例截图】
【核心代码】
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 拨打电话例子源码(亲测可用)》对你有帮助,请点赞、收藏,并留下你的观点哦!