在该警报对话框中的验证单选按钮中显示Toast时,如何保持警报对话框

时间:2018-05-30 06:57:46

标签: android validation radio-button alertdialog

我在Alert对话框中有一个单选按钮,然后iam验证单选按钮,如果没有选中的单选按钮,它将显示Toast消息,并且必须保持显示警告对话框。但是在我的代码下面,当显示吐司时,警报对话框将关闭。

private void showPopupSubscribe() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final View dialogView = getLayoutInflater().inflate(R.layout.paket_subscribe, null);
    builder.setView(dialogView);
    builder.setCancelable(false);
    builder.setIcon(R.drawable.logo_1);
    builder.setTitle(R.string.subscription_package);
    final RadioGroup radioGroup = dialogView.findViewById(R.id.rgSubscribe);

    builder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
        @SuppressLint("ResourceType")
        @Override
        public void onClick(DialogInterface dialog, int which) {
            int selected = radioGroup.getCheckedRadioButtonId();

            if (selected == R.id.rbA) {
                smsCode = vv0;
            } else if (selected == R.id.rbB) {
                smsCode = vv1;
            } else if (selected == R.id.rbC) {
                smsCode = vv2;
            } else if (selected == R.id.rbD) {
                smsCode = vv3;
            }
            if (selected == -1 ){
                Toast.makeText(CatalogHomeActivity.this, "Pilih paket untuk berlangganan", Toast.LENGTH_SHORT).show();

            }else {
                pdSubs.show();
                doCharging();
                dialog.dismiss();
            }
        }
    });
    dialog = builder.create();
    dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                dialog.dismiss();
                finish();
            }
            return true;
        }
    });
    dialog.show();
}

2 个答案:

答案 0 :(得分:0)

首先删除setPositiveButton(); 然后设置正面按钮 如果你想在setOnShowListener

中那个关闭对话框之后
 ...
            builder.setPositiveButton(android.R.string.ok, null);
            ...
            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
                    button.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View view) {
                            if (selected == -1 ){
                                Toast.makeText(CatalogHomeActivity.this, "Pilih paket untuk berlangganan", Toast.LENGTH_SHORT).show();

                            }else {
                                pdSubs.show();
                                doCharging();
                                dialog.dismiss();
                            }
                        }
                    });
                }
            });
            dialog.show();

答案 1 :(得分:0)

尝试以下代码。

private void showPopupSubscribe() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final View dialogView = getLayoutInflater().inflate(R.layout.paket_subscribe, null);
        builder.setView(dialogView);
        builder.setCancelable(false);
        builder.setIcon(R.drawable.logo_1);
        builder.setTitle(R.string.subscription_package);
        builder.setPositiveButton("Next", null);
        final RadioGroup radioGroup = dialogView.findViewById(R.id.rgSubscribe);


        dialog = builder.create();
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    dialog.dismiss();
                    finish();
                }
                return true;
            }
        });
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
                button.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        int selected = radioGroup.getCheckedRadioButtonId();

                        if (selected == R.id.rbA) {
                            smsCode = vv0;
                        } else if (selected == R.id.rbB) {
                            smsCode = vv1;
                        } else if (selected == R.id.rbC) {
                            smsCode = vv2;
                        } else if (selected == R.id.rbD) {
                            smsCode = vv3;
                        }
                        if (selected == -1 ){
                            Toast.makeText(CatalogHomeActivity.this, "Pilih paket untuk berlangganan", Toast.LENGTH_SHORT).show();

                        }else {
                            pdSubs.show();
                            doCharging();
                            dialog.dismiss();
                        }
                    }
                });
            }
        });
        dialog.show();
    }