点击后拨打电话,但需确认

Make a call on click but with confirmation

我想在用户确认的情况下单击 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);
        }
    }
});

有人可以帮助我吗?谢谢

那么,在您的 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();
 }    
});
final ArrayList<String> getContacts = new ArrayList<>();

将您的 phone 数字添加到此 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();

                }

            });

单击文本视图,您可以直接将用户带到 phone 调用屏幕,将这些属性添加到您的 TextView(xml 文件):

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

那么您就不需要在代码中处理意图了! :)