是的,我知道有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...
}
当前回答
在芬兰湾的科特林:
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()
其他回答
谢谢尼基,你的回答帮助我改善了现有的简单添加我想要的行动如下
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Do this action");
builder.setMessage("do you want confirm this action?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do do my action here
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// I do not need any action here you might
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.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();
AlertDialog.Builder altBx = new AlertDialog.Builder(this);
altBx.setTitle("My dialog box");
altBx.setMessage("Welcome, Please Enter your name");
altBx.setIcon(R.drawable.logo);
altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
if(edt.getText().toString().length()!=0)
{
// Show any message
}
else
{
}
}
});
altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//show any message
}
});
altBx.show();
试试这个:
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();
史蒂夫的回答是正确的,尽管有些片段过时了。下面是一个FragmentDialog的例子。
类:
public class SomeDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Title")
.setMessage("Sure you wanna do this!")
.setNegativeButton(android.R.string.no, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing (will close dialog)
}
})
.setPositiveButton(android.R.string.yes, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something
}
})
.create();
}
}
开始对话:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Create and show the dialog.
SomeDialog newFragment = new SomeDialog ();
newFragment.show(ft, "dialog");
您还可以让类实现onClickListener,并使用它来代替嵌入式侦听器。