点击即可拨打电话,但需要确认

时间:2016-11-12 11:26:36

标签: android phone-call

我想点击TextView并拨打电话号码但不能直接打电话,并确认用户。我的代码是这样的,但是当我点击

时它会直接调用
txt_ocho.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        try {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + txt_ocho.getText().toString().trim()));
            startActivity(callIntent);
        } catch (ActivityNotFoundException activityException) {
            Log.d("Calling a Phone Number", "Call failed" + activityException);
        }
    }
});

有人可以帮帮我吗?谢谢

3 个答案:

答案 0 :(得分:2)

好吧,在你的onClick块中,显示一个带有2个按钮的alertDialog(取消并确认)。

txt_ocho.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle("Confirm call")
        .setMessage("Are you sure you want to make the phone call?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            try {
                 Intent callIntent = new Intent(Intent.ACTION_CALL);
                 callIntent.setData(Uri.parse("tel:" + txt_ocho.getText().toString().trim()));
                 startActivity(callIntent);
            } catch (ActivityNotFoundException activityException) {
                 Log.d("Calling a Phone Number", "Call failed" + activityException);
            }   
        }

    })
    .setNegativeButton("Cancel", null)
    .show();
 }    
});

答案 1 :(得分:1)

final ArrayList<String> getContacts = new ArrayList<>();

将您的电话号码添加到此ArrayList 像这样:

getContacts.add(125486842);
getContacts.add(48686223174);

txt_ocho.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                   AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                            int numOfContacts = getContacts.size();
                            builder.setItems(getContacts.toArray(new String[numOfContacts]), new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    // for bring up dial screen with phone number filled
                                    Intent intent = new Intent(Intent.ACTION_DIAL);
                                    intent.setData(Uri.parse("tel:" + getContacts.get(which)));
                                    getActivity().startActivity(intent);
                                }
                            })
                                    .setTitle("set a title")
                                    .create()
                                    .show();

                }

            });

答案 2 :(得分:1)

单击文本视图,您可以直接将用户带到电话呼叫屏幕,将这些属性添加到TextView(xml文件)中:

android:autoLink="phone" 
android:linksClickable="true"

然后您不需要在代码中处理意图! :)