【问题标题】:Android: Use DialogFragment as Container for custom Dialog Yes or No?Android:使用 DialogFragment 作为自定义对话框的容器是还是否?
【发布时间】:2014-02-24 13:40:21
【问题描述】:

好的,伙计们,这是我关于 SO 的第一个问题,我想知道是否真的有必要将 DialogFragment 用作我在 Activity 中拥有的简单自定义对话框的容器。

这是我的代码:

public class MainActivity extends Activity
{
    // VARS:
    private Button buttonShowDialog;
    private Dialog dialogSimple;
    private Button buttonOK;
    private Button buttonCancel;

    // ONCREATE:
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);
        buttonShowDialog = (Button) findViewById(R.id.button_showdialog);
        buttonShowDialog.setOnClickListener(new OnClickListener)
        {
            @Override
            public void onClick(View view) 
            {
                if (dialogSimple != null)
                {
                    dialogSimple.show();
                }
                else
                {
                    showDialog();
                }
            }
        });
    }

    // SHOW DIALOG:
    private void showDialog()
    {
        dialogSimple = new Dialog(MainActivity.this);
        dialogSimple.setContentView(R.layout.dialog);
        buttonOK = (Button) dialogSimple.findViewById(R.id.button_ok);
        buttonCanel = (Button) dialogSimple.findViewById(R.id.button_cancel);

        buttonOK.setOnClickListener(new OnClickListener)
        {
            @Override
            public void onClick(View view) 
            {
                doSomeStuff();
            }
        });

        buttonCanel.setOnClickListener(new OnClickListener)
        {
            @Override
            public void onClick(View view) 
            {
                dialogSimple.dismiss();
            }
        });

        dialogSimple.show();
    }
}

我的活动清单条目:

android:configChanges="orientation|keyboardHidden|keyboard|screenSize"

我测试过,它适用于: API 8(设备), 9(设备), 10(鸸鹋), 11(鸸鹋), 16(鸸鹋), 17(设备), 18(设备) 和 19(鸸鹋)

所以我担心的是,通过这个程序,我的应用程序可能会在某些设备上出现异常,因为 Dialog 不在 DialogFragment 中。

我的问题

  1. 我的担心正确吗?
  2. 如果是,我想知道使用此程序会在我的应用程序中发生什么样的坏事?
  3. 也许有一些示例代码可以以简洁且简短的方式正确执行此操作?

【问题讨论】:

    标签: java android dialog android-activity android-dialogfragment


    【解决方案1】:

    不,不需要。只需recommended

    但您可以通过警告对话框来实现:

     AlertDialog.Builder bld = new AlertDialog.Builder();
     bld.setMessage(...);
     bld.setPositiveButton(...);
     bld.setNegativeButton(...);
     bld.create().show();
    

    【讨论】:

    • 哇,尼克,你的回答真快!非常感谢!
    【解决方案2】:

    Dialog 类是显示对话框的基类。您应该使用更具体的子类来显示是/否对话框。例如:AlertDialog。

        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and show it
        builder.create().show();
    

    您可以找到更多详细信息here

    【讨论】:

    • 我花了更长的时间才回复,因为我想提供一个正确的答案。
    • 也感谢 NPE 的回答。这也是我 1 天前在 Android 开发者网站上读到的,你应该避免直接实例化 Dialog。但是没有解释为什么以及如果是的话会发生什么。只有优点(除了更多代码:))。也许您有缺点的答案或链接?我只是好奇。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 2016-07-30
    • 2012-05-11
    • 1970-01-01
    相关资源
    最近更新 更多