是的,我知道有AlertDialog.Builder,但我才知道自己有多难(好吧,至少不是程序员友好的),以显示Android的对话框。
Yes I know there's AlertDialog.Builder, but I'm shocked to know how difficult (well, at least not programmer-friendly) to display a dialog in Android.
我曾经是一个.NET开发人员,我想知道是否有任何的Android当量以下的?
I used to be a .Net developer, and I'm wondering is there any Android-equivalent of the following?
if (MessageBox.Show("Are You Sure?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//Do something...
}
感谢您的输入。
Thanks for your input.
推荐答案
AlertDialog.Builder真的不是那么难使用。这是一个有点吓人首先肯定的,但一旦你使用它一下它的结构简单,功能强大。我知道你说你知道如何使用它,但这里只是一个简单的例子呢:
AlertDialog.Builder really isn't that hard to use. It's a bit intimidating at first for sure, but once you've used it a bit it's both simple and powerful. I know you've said you know how to use it, but here's just a simple example anyway:
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
如果你有其他的是/否框,应该做同样的事情。
You can also reuse that DialogInterface.OnClickListener
if you have other yes/no boxes that should do the same thing.
如果你从一个 View.OnClickListener
,您可以使用 view.getContext()获取上下文。或者您可以使用
yourFragmentName.getActivity()
。
If you're creating the Dialog from within a View.OnClickListener
, you can use view.getContext()
to get the Context. Alternatively you can use yourFragmentName.getActivity()
.
相关推荐
最新文章