如何在Android上显示“是/否"对话框? [英] How to display a Yes/No dialog box on Android?

查看:161
本文介绍了如何在Android上显示“是/否"对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道有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("Sure?", "", MessageBoxButtons.YesNo) == DialogResult.Yes){
    // Do something...
}

推荐答案

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().

这篇关于如何在Android上显示“是/否"对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆