我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
当前回答
你也可以尝试这种方式,它将为你提供材料样式的对话框
private void showDialog()
{
String text2 = "<font color=#212121>Medi Notification</font>";//for custom title color
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle(Html.fromHtml(text2));
String text3 = "<font color=#A4A4A4>You can complete your profile now or start using the app and come back later</font>";//for custom message
builder.setMessage(Html.fromHtml(text3));
builder.setPositiveButton("DELETE", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
toast = Toast.makeText(getApplicationContext(), "DELETE", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
toast = Toast.makeText(getApplicationContext(), "CANCEL", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
builder.show();
}
其他回答
new AlertDialog.Builder(loginregister.this)
.setTitle("messege")
.setPositiveButton("ok", null)
.setMessage( "user name : " + username + "/n" +
"password :" + password + "/n" )
.show();
您可以为此使用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();
现在与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/
带有编辑文本的警报对话框
AlertDialog.Builder builder = new AlertDialog.Builder(context);//Context is activity context
final EditText input = new EditText(context);
builder.setTitle(getString(R.string.remove_item_dialog_title));
builder.setMessage(getString(R.string.dialog_message_remove_item));
builder.setTitle(getString(R.string.update_qty));
builder.setMessage("");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
input.setHint(getString(R.string.enter_qty));
input.setTextColor(ContextCompat.getColor(context, R.color.textColor));
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setText("String in edit text you want");
builder.setView(input);
builder.setPositiveButton(getString(android.R.string.ok),
(dialog, which) -> {
//Positive button click event
});
builder.setNegativeButton(getString(android.R.string.cancel),
(dialog, which) -> {
//Negative button click event
});
AlertDialog dialog = builder.create();
dialog.show();
new AlertDialog.Builder(context)
.setTitle("title")
.setMessage("message")
.setPositiveButton(android.R.string.ok, null)
.show();