单击按钮不会关闭自定义警报对话框,并在自定义对话框下方显示白色背景
Custom Alert dialog is not getting dismissed on click of a button and showing a white background below the custom dialog
我创建了一个自定义警报对话框以显示在我的应用程序中。它有一个 imageView、两个 textView 和一个按钮。一旦我的主要 activity launches.I 根据我的 requirement.The 自定义警报对话框以单独的方法调用此自定义警报 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>
对于额外的白色背景,您必须使用此代码将 Window 背景颜色应用到 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();
}
});
这样试试...
//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();
我创建了一个自定义警报对话框以显示在我的应用程序中。它有一个 imageView、两个 textView 和一个按钮。一旦我的主要 activity launches.I 根据我的 requirement.The 自定义警报对话框以单独的方法调用此自定义警报 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>
对于额外的白色背景,您必须使用此代码将 Window 背景颜色应用到 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()
调用了吗?我还建议您在设置
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();
}
});
这样试试...
//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();