我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
当前回答
带有编辑文本的警报对话框
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();
其他回答
你可以创建Activity并扩展AppCompatActivity。然后在清单中加入下一种风格
<activity android:name=".YourCustomDialog"
android:theme="Theme.AppCompat.Light.Dialog">
</activity>
充气它的按钮和TextViews
然后把这个当做一个对话。
例如,在linearLayout中,我填充下一个参数:
android:layout_width="300dp"
android:layout_height="wrap_content"
现在与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/
这是一个创建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();
David Hedlund发布的代码给了我错误:
无法添加窗口-空令牌无效
如果你得到同样的错误,请使用下面的代码。它的工作原理!
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!isFinishing()){
new AlertDialog.Builder(YourActivity.this)
.setTitle("Your Alert")
.setMessage("Your Message")
.setCancelable(false)
.setPositiveButton("ok", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Whatever...
}
}).show();
}
}
});
使用AlertDialog。建造者:
AlertDialog alertDialog = new AlertDialog.Builder(this)
//set icon
.setIcon(android.R.drawable.ic_dialog_alert)
//set title
.setTitle("Are you sure to Exit")
//set message
.setMessage("Exiting will call finish() method")
//set positive button
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what would happen when positive button is clicked
finish();
}
})
//set negative button
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what should happen when negative button is clicked
Toast.makeText(getApplicationContext(),"Nothing Happened",Toast.LENGTH_LONG).show();
}
})
.show();
您将得到以下输出。
要查看警报对话框教程使用下面的链接。
Android警报对话框教程