【问题标题】:Custom Alert dialog is not getting dismissed on click of a button and showing a white background below the custom dialog单击按钮并在自定义对话框下方显示白色背景时,自定义警报对话框不会被关闭
【发布时间】:2019-07-09 10:27:39
【问题描述】:

我创建了一个自定义警报对话框以显示在我的应用程序中。它有一个 imageView、两个 textView 和一个按钮。我正在尝试在我的主要活动启动后显示此自定义警报对话框。我正在根据我的要求以单独的方法调用此自定义警报 dialog.show()。自定义警报对话框正在显示,但单击按钮即可没有被解雇,自定义警报对话框也显示额外的白色背景。

showCustomAlertDialog() 方法

 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();
 }

reminder_alert_dialog 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="500dp"
    android:background="@drawable/ic_alert" />

<TextView
    android:id="@+id/greetings_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="GOOD MORNING, KEVIN"
    android:textColor="#000000"
    android:textSize="20sp"
    android:textStyle="bold"
    android:layout_marginTop="200dp"
    android:layout_centerHorizontal="true"/>

<TextView
    android:id="@+id/medicines_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Have you taken your medicines today?"
    android:textColor="#000000"
    android:textSize="15sp"
    android:textStyle="normal"
    android:layout_below="@id/greetings_text"
    android:layout_marginTop="50dp"
    android:layout_centerHorizontal="true"/>

<Button
    android:id="@+id/record_button"
    android:layout_width="160dp"
    android:layout_height="40dp"
    android:background="#3BC4B8"
    android:text="Record"
    android:textSize="20sp"
    android:textColor="#ffffff"
    android:layout_below="@id/medicines_text"
    android:layout_marginTop="60dp"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

【问题讨论】:

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


    【解决方案1】:

    来到额外的白色背景,您必须使用此代码将窗口背景颜色应用于 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();
         }
      });
    

    【讨论】:

    • alertDialog 和 builder 是两个不同的变量。我希望在一张卡片中同时使用 alertDilaog 和 builder,但我没有看到您将 alertDilaog 作为参数传递给 builder 的任何地方。
    • 在代码中,我提到builder 实际上是AlertDialog 类型。生成器用于构建。如果你想要一个AlertDialog.Builder 类型,不要调用create() 方法,所以代码将是new AlertDialog.Builder(this);
    【解决方案2】:

    试试这样...

    //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();
    

    【讨论】:

      猜你喜欢
      • 2011-08-08
      • 2011-06-17
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多