我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
当前回答
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>-->
其他回答
public void showSimpleDialog(View view) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(false);
builder.setTitle("AlertDialog Title");
builder.setMessage("Simple Dialog Message");
builder.setPositiveButton("OK!!!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//
}
})
.setNegativeButton("Cancel ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// Create the AlertDialog object and return it
builder.create().show();
}
也可以看看我的关于Android对话的博客,你会在这里找到所有的细节:http://www.fahmapps.com/2016/09/26/dialogs-in-android-part1/。
new AlertDialog.Builder(context)
.setTitle("title")
.setMessage("message")
.setPositiveButton(android.R.string.ok, null)
.show();
您可以使用AlertDialog创建对话框。构建器
试试这个:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to delete this entry?");
builder.setPositiveButton("Yes, please", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//perform any action
Toast.makeText(getApplicationContext(), "Yes clicked", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//perform any action
Toast.makeText(getApplicationContext(), "No clicked", Toast.LENGTH_SHORT).show();
}
});
//creating alert dialog
AlertDialog alertDialog = builder.create();
alertDialog.show();
要改变Alert对话框的正反按钮的颜色,你可以在alertDialog.show()后面写下面两行;
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimaryDark));
在材质组件库中,你可以使用MaterialAlertDialogBuilder
MaterialAlertDialogBuilder(context)
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton("Delete") { dialog, which ->
// Respond to positive button press
}
.setNegativeButton("Cancel") { dialog, which ->
// Respond to positive button press
}
.show()
使用Compose 1.0。X你可以使用:
val openDialog = remember { mutableStateOf(true) }
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
// Dismiss the dialog when the user clicks outside the dialog or on the back
// button. If you want to disable that functionality, simply use an empty
// onCloseRequest.
openDialog.value = false
},
title = null,
text = {
Text(
"Are you sure you want to delete this entry?"
)
},
confirmButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text("Delete")
}
},
dismissButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text("Cancel")
}
}
)
}
这是一个创建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();