1

从edittext获取号码时如何拨打电话?我试试这个,但它不起作用。我有一个问题,将变量号传递给 Uri.parse() 必须在 gettext 的 void 内?

public String string;
public String number;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


     EditText txtcallnumber;
     txtcallnumber = (EditText)findViewById(R.id.callnumber);
     string = txtcallnumber.getText().toString().trim();//There no work call
       number = "tel:" + string;

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
          call();
        }
      });
}


public void call() {
try {
   Intent callIntent = new Intent(Intent.ACTION_CALL);
    //callIntent.setData(Uri.parse("tel:xxxxxxx")); //This work
    string = txtcallnumber.getText().toString().trim();
       number = "tel:" + string;//There work call
    callIntent.setData(Uri.parse(number));
    startActivity(callIntent);


} catch (ActivityNotFoundException activityException) {
     Log.e("helloandroid dialing example", "Call failed");
}
4

2 回答 2

0

在 onCreate 方法中不需要:

 string = txtcallnumber.getText().toString().trim();
 number = "tel:" + string;

因为 onCreate 几乎用于设置显示,并且在设置屏幕之前用户不会输入,但是在输入文本并按下按钮后的 onCall 方法中,它会......你知道的。

另外你应该有这个来检查没有输入:

if(string!=null){
  try{try {
Intent callIntent = new Intent(Intent.ACTION_CALL);

string = txtcallnumber.getText().toString().trim();
   number = "tel:" + string;
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(number)));


} catch (ActivityNotFoundException activityException) {
 Log.e("helloandroid dialing example", "Call failed");

      }}else{
    Log.d("helloandroid dialing example","nothing entered");}
于 2012-12-29T17:00:00.597 回答
0

您需要在清单文件中添加权限::

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

在您的 call() 方法中尝试使用这些 Intent::

number=edittext.getText().trim();
no="tel:"+number;

startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(no)));
于 2012-12-29T14:59:50.497 回答