我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
当前回答
现在最好使用DialogFragment而不是直接创建AlertDialog。
怎么做?参见:https://stackoverflow.com/a/21032871/1390874 为什么?参见:https://stackoverflow.com/a/13765411/1390874
其他回答
在过去的几天里,我的同事一直在问我如何在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;
// ...
你可以使用下面的代码:
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();
试试下面的代码:
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
现在与Jetpack撰写引入android,你可以简单地创建警报对话框使用下面的代码
if (viewModel.shouldDialogOpen.value) {
AlertDialog(onDismissRequest = { viewModel.shouldDialogOpen.value = false },
title = { Text("Delete Entry?") },
text = {
Text("Are you sure you want to delete this entry?")
},
dismissButton = {
Button(modifier = Modifier.fillMaxWidth(), onClick = {
viewModel.shouldDialogOpen.value = false
}) {
Text(text = "Cancel")
}
}, confirmButton = {
Button(modifier = Modifier.fillMaxWidth(), onClick = {
viewModel.shouldDialogOpen.value = false
viewModel.beginDelete(recipe)
}) {
Text(text = "Okay")
}
})
}
在viewModel中。shouldDialogOpen, shouldDialogOpen是viewmodel中的一个mutablestate字段,当我们需要显示或解散对话框时,我们会改变它的值。
更多的代码样本Jetpack撰写:- https://androidlearnersite.wordpress.com/2021/08/03/jetpack-compose-1-0-0-sample-codes/
Kotlin定制对话框: 如果你想创建自定义对话框
Dialog(activity!!, R.style.LoadingIndicatorDialogStyle)
.apply {
// requestWindowFeature(Window.FEATURE_NO_TITLE)
setCancelable(true)
setContentView(R.layout.define_your_custom_view_id_here)
//access your custom view buttons/editText like below.z
val createBt = findViewById<TextView>(R.id.clipboard_create_project)
val cancelBt = findViewById<TextView>(R.id.clipboard_cancel_project)
val clipboard_et = findViewById<TextView>(R.id.clipboard_et)
val manualOption =
findViewById<TextView>(R.id.clipboard_manual_add_project_option)
//if you want to perform any operation on the button do like this
createBt.setOnClickListener {
//handle your button click here
val enteredData = clipboard_et.text.toString()
if (enteredData.isEmpty()) {
Utils.toast("Enter project details")
} else {
navigateToAddProject(enteredData, true)
dismiss()
}
}
cancelBt.setOnClickListener {
dismiss()
}
manualOption.setOnClickListener {
navigateToAddProject("", false)
dismiss()
}
show()
}
在style.xml中创建LoadingIndicatorDialogStyle
<style name="LoadingIndicatorDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:statusBarColor">@color/black_transperant</item>
<item name="android:layout_gravity">center</item>
<item name="android:background">@android:color/transparent</item>
<!--<item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>-->