我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
当前回答
new AlertDialog.Builder(context)
.setTitle("title")
.setMessage("message")
.setPositiveButton(android.R.string.ok, null)
.show();
其他回答
在过去的几天里,我的同事一直在问我如何在Xamarin中使用AlertDialog。Android和几乎所有人都把这个问题作为参考发给了我,他们在问我之前读了这个问题(没有找到答案),所以这里是Xamarin。Android (c#)版本:
var alertDialog = new AlertDialog.Builder(this) // this: Activity
.SetTitle("Hello!")
.SetMessage("Are you sure?")
.SetPositiveButton("Ok", (sender, e) => { /* ok things */ })
.SetNegativeButton("Cancel", (sender, e) => { /* cancel things */ })
.Create();
alertDialog.Show();
// you can customize your AlertDialog, like so
var tvMessage = alertDialog.FindViewById<TextView>(Android.Resource.Id.Message);
tvMessage.TextSize = 13;
// ...
showDialog(MainActivity.this, "title", "message", "OK", "Cancel", {...}, {...});
科特林
fun showDialog(context: Context, title: String, msg: String,
positiveBtnText: String, negativeBtnText: String?,
positiveBtnClickListener: DialogInterface.OnClickListener,
negativeBtnClickListener: DialogInterface.OnClickListener?): AlertDialog {
val builder = AlertDialog.Builder(context)
.setTitle(title)
.setMessage(msg)
.setCancelable(true)
.setPositiveButton(positiveBtnText, positiveBtnClickListener)
if (negativeBtnText != null)
builder.setNegativeButton(negativeBtnText, negativeBtnClickListener)
val alert = builder.create()
alert.show()
return alert
}
Java
public static AlertDialog showDialog(@NonNull Context context, @NonNull String title, @NonNull String msg,
@NonNull String positiveBtnText, @Nullable String negativeBtnText,
@NonNull DialogInterface.OnClickListener positiveBtnClickListener,
@Nullable DialogInterface.OnClickListener negativeBtnClickListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle(title)
.setMessage(msg)
.setCancelable(true)
.setPositiveButton(positiveBtnText, positiveBtnClickListener);
if (negativeBtnText != null)
builder.setNegativeButton(negativeBtnText, negativeBtnClickListener);
AlertDialog alert = builder.create();
alert.show();
return alert;
}
这是在kotlin中完成的
val builder: AlertDialog.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert)
} else {
AlertDialog.Builder(this)
}
builder.setTitle("Delete Alert!")
.setMessage("Are you want to delete this entry?")
.setPositiveButton("YES") { dialog, which ->
}
.setNegativeButton("NO") { dialog, which ->
}
.setIcon(R.drawable.ic_launcher_foreground)
.show()
new AlertDialog.Builder(context)
.setTitle("title")
.setMessage("message")
.setPositiveButton(android.R.string.ok, null)
.show();
// Dialog box
public void dialogBox() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Click on Image for tag");
alertDialogBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertDialogBuilder.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}