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

2023-09-05 05:30:31 作者:心逸

我想作一个对话片段,询问你确定?用是/否的答复。

我已经看了的文档和它的非常啰嗦,去全国各地的地方,解释如何让先进的对话框,但在制作一个简单的世界你好之类的对话框中没有完整的code。大多数教程利用德precated对话框系统。该官方博客似乎是不必要的复杂和难以理解。

那么,什么是创建和显示一个很基本的警告对话框最简单的方法?如果是使用支持库的奖励积分。

解决方案

一个DialogFragment真的只是包装了一个对话的片段。你可以把任何类型的对话框在那里创建和DialogFragment的onCreateDialog()方法返回的对话框。

下面有一个例子DialogFragment:

 类MyDialogFragment扩展DialogFragment {
    语境mContext;
    公共MyDialogFragment(){
        mContext = getActivity();
    }
    @覆盖
    公共对话onCreateDialog(包savedInstanceState){
        AlertDialog.Builder alertDialogBu​​ilder =新AlertDialog.Builder(mContext);
        alertDialogBu​​ilder.setTitle(真的吗?);
        alertDialogBu​​ilder.setMessage(你确定吗?);
        //空应该是你的点击监听器
        alertDialogBu​​ilder.setPositiveButton(OK,NULL);
        alertDialogBu​​ilder.setNegativeButton(取消,新DialogInterface.OnClickListener(){

            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                dialog.dismiss();
            }
        });


        返回alertDialogBu​​ilder.create();
    }
}
 

要创建对话框电话:

 新MyDialogFragment()秀(getFragmentManager(),MyDialog)。
 
父亲与儿子在深夜的一段简单对话

和从什么地方关闭对话框:

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

所有这些code会很好地工作的支持库,通​​过只是改变了进口使用支持库类。

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

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.

解决方案

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.

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();
    }
}

To create the dialog call:

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

And to dismiss the dialog from somewhere:

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