警报对话框的默认按钮在Android中不起作用

警报对话框的默认按钮在Android中不起作用,android,android-alertdialog,Android,Android Alertdialog,我正在使用AlertDialog使用xml文件显示一些数据。但当我在AlertDialog中膨胀xml时,默认按钮不会显示。 请帮助只需将新按钮添加到xml文件中,并获取该按钮的id,然后将click listener置于该按钮上。 这对我有用。这会解决你的问题! LayoutInflater factory = LayoutInflater.from(Splash.this); final View textEntryView = factory.inflate(R.layou

我正在使用AlertDialog使用xml文件显示一些数据。但当我在AlertDialog中膨胀xml时,默认按钮不会显示。
请帮助

只需将新按钮添加到xml文件中,并获取该按钮的id,然后将click listener置于该按钮上。
这对我有用。

这会解决你的问题!
    LayoutInflater factory = LayoutInflater.from(Splash.this);
    final View textEntryView = factory.inflate(R.layout.text_entry, null);

    //text_entry is an Layout XML file containing two text field to display in alert dialog

     final EditText input1 = (EditText) textEntryView.findViewById(R.id.editText1);
    final EditText input2 = (EditText) textEntryView.findViewById(R.id.editText2);             
                     input1.setHint("roomnumber");
                     input2.setHint("hotelname");
                     AlertDialog.Builder alert = new AlertDialog.Builder(Splash.this);

                     alert.setIcon(android.R.drawable.ic_menu_info_details)
                          .setTitle("Enter the Text:")
                          .setView(textEntryView)
                          .setPositiveButton("Your Default button text", 
                              new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface dialog, int whichButton) {
                                         Log.i("AlertDialog","TextEntry 1 Entered "+input1.getText().toString());
                                         Log.i("AlertDialog","TextEntry 2 Entered "+input2.getText().toString());
                                         if(isOnline())
                                        {       
                                        /* User clicked OK so do some stuff */

                                  }else{


                                  }
                                  }
                              });

                     alert.show();
您需要将onclick listener设置为对话框中的所有按钮。遵循下面的代码

  public void showDialogWithIcons() {
            final Dialog dialog = new Dialog(this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.icons_dialog);

            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

            Button yesButton = (Button) dialog.findViewById(R.id.yesanswer_icons);
            yesButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    TextView tvForIconToChange = (TextView) findViewById(R.id.textInThePicture);
                    tvForIconToChange.setTypeface(tf);
                    tvForIconToChange.setText(getMyTextForIcon());
                    dialog.dismiss();

                }

            });
            dialog.show();
        }

因为它是你的自定义布局。 要在自定义对话框中使用按钮,请在自定义布局xml中定义按钮,然后在代码中执行以下操作:

Button xyz = (Button)dialog.findViewbyId(R.id.abc)
xyz.setOnClickListener(this or new View.OnClickListener)

你的密码在哪里?我们能看看你的密码吗?你的回答可能对他有帮助。因此,只需添加一部分代码作为答案。为什么这个和那个代码都不适用于我