最简单的是/否对话框片段

Simplest yes/no dialog fragment(最简单的是/否对话框片段)
本文介绍了最简单的是/否对话框片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个对话片段,询问你确定吗?"回复是/否".

I'd like to make a dialog fragment that asks "Are you sure?" with a "yes/no" reply.

我查看了文档,它真的很冗长,到处都是这个地方,解释了如何制作高级对话框,但没有完整的代码来制作一个简单的hello world"类型的对话框.大多数教程使用已弃用的对话框系统.官方博客似乎过于复杂且难以理解.

I've looked at the documentation and it's really verbose, going all over the place, explaining how to make advanced dialog boxes, but no intact code on making a simple 'hello world' kind of dialog box. Most tutorials utilize the deprecated dialog box system. The official blog seems to be unnecessarily complicated and difficult to understand.

那么,创建和显示一个真正基本的警报对话框的最简单方法是什么?使用支持库的奖励积分.

So, what's the simplest way to create and display a really basic Alert Dialog? Bonus points if it's using the support library.

推荐答案

DialogFragment 其实就是一个包装了对话框的片段.您可以通过在 DialogFragment 的 onCreateDialog() 方法中创建并返回对话框来将任何类型的对话框放入其中.

A DialogFragment is really just a fragment that wraps a dialog. You can put any kind of dialog in there by creating and returning the dialog in the onCreateDialog() method of the DialogFragment.

这是一个示例 DialogFragment:

Heres an example DialogFragment:

class MyDialogFragment extends DialogFragment{
    Context mContext;
    public MyDialogFragment() {
        mContext = getActivity();
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
        alertDialogBuilder.setTitle("Really?");
        alertDialogBuilder.setMessage("Are you sure?");
        //null should be your on click listener
        alertDialogBuilder.setPositiveButton("OK", null);
        alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });


        return alertDialogBuilder.create();
    }
}

创建对话调用:

new MyDialogFragment().show(getFragmentManager(), "MyDialog");

并从某处关闭对话框:

((MyDialogFragment)getFragmentManager().findFragmentByTag("MyDialog")).getDialog().dismiss();

只需更改导入以使用支持库类,所有这些代码都将与支持库完美配合.

All of that code will work perfectly with the support library, by just changing the imports to use the support library classes.

这篇关于最简单的是/否对话框片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)