单击按钮并在自定义对话框下方显示白色背景时,“自定义警报”对话框不会消失

时间:2019-07-09 10:27:39

标签: android android-layout alertdialog android-alertdialog android-dialogfragment

我创建了一个自定义提醒对话框以显示在我的应用中。它具有一个imageView,两个textViews和一个按钮。我试图在我的主要活动启动后显示此自定义警报对话框。我根据我的要求在单独的方法中调用此自定义警报dialog.show()。自定义警报对话框正在显示,但单击按钮时不会被驳回,自定义警报对话框也会显示额外的白色背景。

showCustomAlertDialog()方法

201903    201904
25%       50%

reminder_alert_dialog布局

 public void showCustomAlertDialog() {
 LayoutInflater layoutInflater = LayoutInflater.from(this);
 final View promptView  =layoutInflater.inflate(R.layout.reminder_alert_dialog, null);
 final AlertDialog builder = new AlertDialog.Builder(this).create();

 Button recordButton = (Button)promptView.findViewById(R.id.record_button);
 recordButton.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
        System.out.println("Dismiss the dialog");
        builder.dismiss();
     }
  });
  builder.setView(promptView);
  builder.show();
 }

2 个答案:

答案 0 :(得分:0)

来到额外的白色背景时,您必须使用此代码将AlertDialog的“窗口背景”颜色应用为透明

Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.tabs);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();

没有显示对话框的问题是调用System.out.println()吗?我还建议您在设置

之类的侦听器之前,先设置AlertDialog.Builder的视图
final AlertDialog builder = new AlertDialog.Builder(this).create();

  builder.setView(promptView);

 Button recordButton = (Button)promptView.findViewById(R.id.record_button);
 recordButton.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
        System.out.println("Dismiss the dialog");
        builder.dismiss();
     }
  });

答案 1 :(得分:0)

尝试这样...

//class variables
 AlertDialog dialog;
 Button dismiss;
//Add your other views if required

//in onCreate() activity method
makeDialog();

//makeDialog() method code

void makeDialog(){
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle("Title");
      View root = getLayoutInflater().inflate(R.layout.your_layout,null);
      dismiss = root.findViewById(R.id.your_button_id);
      //another views initialization goes here...
      dismiss.setOnClickListener(new View.OnClickListener(){
                       dialog.dismiss();
                       //your logic or job...
      });
      builder.setView(root);
      dialog = builder.create();
}

随时显示对话框

dialog.show();