我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
当前回答
你可以使用下面的代码:
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(
AlertDialogActivity.this);
// Setting Dialog Title
alertDialog2.setTitle("Confirm Delete...");
// Setting Dialog Message
alertDialog2.setMessage("Are you sure you want delete this file?");
// Setting Icon to Dialog
alertDialog2.setIcon(R.drawable.delete);
// Setting Positive "Yes" Btn
alertDialog2.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(),
"You clicked on YES", Toast.LENGTH_SHORT)
.show();
}
});
// Setting Negative "NO" Btn
alertDialog2.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(),
"You clicked on NO", Toast.LENGTH_SHORT)
.show();
dialog.cancel();
}
});
// Showing Alert Dialog
alertDialog2.show();
其他回答
我在onClick方法中使用这个AlertDialog:
button.setOnClickListener(v -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(this);
View view = layoutInflaterAndroid.inflate(R.layout.cancel_dialog, null);
builder.setView(view);
builder.setCancelable(false);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
view.findViewById(R.id.yesButton).setOnClickListener(v -> onBackPressed());
view.findViewById(R.id.nobutton).setOnClickListener(v -> alertDialog.dismiss());
});
dialog.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textmain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:padding="5dp"
android:text="@string/warning"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textpart2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:lines="2"
android:maxLines="2"
android:padding="5dp"
android:singleLine="false"
android:text="@string/dialog_cancel"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textmain" />
<TextView
android:id="@+id/yesButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="5dp"
android:background="#87cefa"
android:gravity="center"
android:padding="10dp"
android:text="@string/yes"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textpart2" />
<TextView
android:id="@+id/nobutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="40dp"
android:background="#87cefa"
android:gravity="center"
android:padding="10dp"
android:text="@string/no"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/yesButton" />
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_margin="5dp"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nobutton" />
</androidx.constraintlayout.widget.ConstraintLayout>
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This is Title");
builder.setMessage("This is message for Alert Dialog");
builder.setPositiveButton("Positive Button", (dialog, which) -> onBackPressed());
builder.setNegativeButton("Negative Button", (dialog, which) -> dialog.cancel());
builder.show();
这是一种类似于用一行代码创建Alert对话框的方法。
这是一个创建Alert对话框的基本示例:
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setCancelable(false);
dialog.setTitle("Dialog on Android");
dialog.setMessage("Are you sure you want to delete this entry?" );
dialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Action for "Delete".
}
})
.setNegativeButton("Cancel ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Action for "Cancel".
}
});
final AlertDialog alert = dialog.create();
alert.show();
只是当你想解散对话框时要小心——使用dialog.dismiss()。在我的第一次尝试中,我使用了遣散dialog(0)(我可能从某个地方复制),有时它可以工作。使用系统提供的对象听起来是更安全的选择。
您可以为此使用AlertDialog,并使用它的Builder类构造一个AlertDialog。下面的例子使用默认的构造函数,它只接受Context,因为对话框将从传入的Context继承适当的主题,但是如果你想这样做,还有一个构造函数允许你指定一个特定的主题资源作为第二个参数。
new AlertDialog.Builder(context)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Continue with delete operation
}
})
// A null listener allows the button to dismiss the dialog and take no further action.
.setNegativeButton(android.R.string.no, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();