使用对话框内的单选按钮时,对话框中的取消按钮不起作用

时间:2016-04-13 13:36:47

标签: android android-alertdialog

  在对话框中使用单选按钮时,

“对话框中的取消和确定”按钮不起作用。

public void bookappointment()
{
    LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
    final View promptView = layoutInflater.inflate(R.layout.bridaldialogbox, null);
    android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(getActivity());
    alertDialogBuilder.setView(promptView);
    final android.support.v7.app.AlertDialog alert = alertDialogBuilder.create();
    alert.show();
    alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    final EditText userstatus=(EditText) promptView.findViewById(R.id.choosen);
    address = (EditText) promptView.findViewById(R.id.address);
    radioGroup = (RadioGroup) promptView.findViewById(R.id.radioGroup);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            position = radioGroup.indexOfChild(promptView.findViewById(checkedId));
            position1 = radioGroup.indexOfChild(promptView.findViewById(radioGroup.getCheckedRadioButtonId()));
            switch (position) {
                case 0:
                    userstatus.setText("Home");
                    bridal_status = userstatus.getText().toString();
                    break;
                case 1:
                    userstatus.setText("Mahal");
                    bridal_status = userstatus.getText().toString();
                    break;
            }
        }
    });

    Button ok_buy = (Button) promptView.findViewById(R.id.ok_bridal);
    ok_buy.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            alert.dismiss();
        }
    });

    Button no_cancel = (Button) promptView.findViewById(R.id.cancel_bridal);
    no_cancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            alert.dismiss();
        }
    });
}
  

我在其他地方使用了相同的功能对话框,但它的工作正常但现在当使用单选按钮时单击按钮5或6次后效果不佳只有对话框正在解除

4 个答案:

答案 0 :(得分:0)

你能使用alert.setPositiveButton()和alert.setNegativeButton()吗? 试试这些,看看是否有效。

答案 1 :(得分:0)

你有

alert.dismiss();

作为ok_buy和no_cancel按钮中的操作。你的意思是在你的ok_buy做一些肯定的事情吗?

答案 2 :(得分:0)

正如@ Ruben2112所说,请查看此链接(Android alert dialog and set positive button)并设置正面和负面按钮。在负面按钮(取消)中尝试Casey Murray所说的话(dialog.dismiss();dialog.cancel()

或许是因为您在设置按钮

之前设置了alert.show()

答案 3 :(得分:0)

你可以尝试这个简单的android dialog popup library。在您的活动中使用非常简单。

这是您的工作代码

public void bookappointment(){
final AlertDialog dialog = Pop.on(getActivity())
   .with()
   .cancelable(false) //ignore if not needed
   .layout(R.layout.bridaldialogbox)
   .show(new Pop.View() {
       @Override
       public void prepare(View view) {
           final EditText userstatus=(EditText) promptView.findViewById(R.id.choosen);
           address = (EditText) view.findViewById(R.id.address);
           radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
           radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
               @Override
               public void onCheckedChanged(RadioGroup group, int checkedId) {
                   position = radioGroup.indexOfChild(view.findViewById(checkedId));
                   position1 = radioGroup.indexOfChild(view.findViewById(radioGroup.getCheckedRadioButtonId()));
                   switch (position) {
                       case 0:
                           userstatus.setText("Home");
                           bridal_status = userstatus.getText().toString();
                           break;
                       case 1:
                           userstatus.setText("Mahal");
                           bridal_status = userstatus.getText().toString();
                           break;
                   }
               }
               Button ok_buy = (Button) view.findViewById(R.id.ok_bridal);
               ok_buy.setOnClickListener(new View.OnClickListener() {
                   public void onClick(View v) {
                       dialog.dismiss();
                   }
               });

               Button no_cancel = (Button) view.findViewById(R.id.cancel_bridal);
               no_cancel.setOnClickListener(new View.OnClickListener() {
                   public void onClick(View v) {
                       dialog.dismiss();
                   }
               });             
           });
       }
   });
}

要使其工作,请包含一次gradle依赖项

dependencies {
    compile 'com.vistrav:pop:2.0'
}