是的,我知道有AlertDialog。但我很惊讶地知道在Android中显示对话框有多么困难(好吧,至少不是程序员友好的)。
我曾经是一名。net开发人员,我想知道在android上是否有类似的功能?
if (MessageBox.Show("Sure?", "", MessageBoxButtons.YesNo) == DialogResult.Yes){
// Do something...
}
是的,我知道有AlertDialog。但我很惊讶地知道在Android中显示对话框有多么困难(好吧,至少不是程序员友好的)。
我曾经是一名。net开发人员,我想知道在android上是否有类似的功能?
if (MessageBox.Show("Sure?", "", MessageBoxButtons.YesNo) == DialogResult.Yes){
// Do something...
}
当前回答
芬兰湾的科特林的实现。
你可以像这样创建一个简单的函数:
fun dialogYesOrNo(
activity: Activity,
title: String,
message: String,
listener: DialogInterface.OnClickListener
) {
val builder = AlertDialog.Builder(activity)
builder.setPositiveButton("Yes", DialogInterface.OnClickListener { dialog, id ->
dialog.dismiss()
listener.onClick(dialog, id)
})
builder.setNegativeButton("No", null)
val alert = builder.create()
alert.setTitle(title)
alert.setMessage(message)
alert.show()
}
像这样叫它:
dialogYesOrNo(
this,
"Question",
"Would you like to eat?",
DialogInterface.OnClickListener { dialog, id ->
// do whatever you need to do when user presses "Yes"
}
})
其他回答
试试这个:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm");
builder.setMessage("Are you sure?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
AlertDialog。Builder真的没有那么难用。一开始肯定有点吓人,但一旦你使用了一点,它就变得简单而强大了。我知道你说过你知道如何使用它,但这里只是一个简单的例子:
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
你也可以重用那个DialogInterface。OnClickListener如果你有其他是/否框,应该做同样的事情。
如果你是从视图中创建对话框。OnClickListener,你可以使用view.getContext()来获取Context。或者你也可以使用yourFragmentName.getActivity()。
在芬兰湾的科特林:
AlertDialog.Builder(this)
.setTitle(R.string.question_title)
.setMessage(R.string.question_message)
.setPositiveButton(android.R.string.yes) { _, _ -> yesClicked() }
.setNegativeButton(android.R.string.no) { _, _ -> noClicked() }
.show()
显示对话框匿名作为命令链&没有定义另一个对象:
new AlertDialog.Builder(this).setTitle("Confirm Delete?")
.setMessage("Are you sure?")
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Perform Action & Dismiss dialog
dialog.dismiss();
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
})
.create()
.show();
Kotlin在Android::
override fun onBackPressed() {
confirmToCancel()
}
private fun confirmToCancel() {
AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you want to cancel?")
.setCancelable(false)
.setPositiveButton("Yes") {
dialog: DialogInterface, _: Int ->
dialog.dismiss()
// for sending data to previous activity use
// setResult(response code, data)
finish()
}
.setNegativeButton("No") {
dialog: DialogInterface, _: Int ->
dialog.dismiss()
}
.show()
}