【问题标题】:How to use Dialog Fragment? (showDialog deprecated) Android如何使用对话框片段? (不推荐使用 showDialog)Android
【发布时间】:2013-12-22 16:37:45
【问题描述】:

我知道有这个文档

http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog

但作为一个新的 Android/Java 学习者,要理解编写一个弹出两个选项(是/否)消息的简单警报对话框所涉及的代码量并不容易。

这是我目前在 MainActivity 文件中的代码:

final private int RESET_DIALOG = 0;

    private OnClickListener resetButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog(RESET_DIALOG);

        }
    };

    protected android.app.Dialog onCreateDialog(int id) {
        switch(id) {
        case RESET_DIALOG: 
            AlertDialog.Builder builder = new Builder(this);
            return builder
                    .setMessage("Are you sure you want to reset the count?")
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {    

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did not reset!", 5).show();

                        }
                    })

                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {


                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did Reset!", 5).show();

                        }
                    })
                    .create();
        }
        return null;
    };

这是我尝试按照 android 网站上的说明进行操作:Main Activity 文件:

final private int RESET_DIALOG = 0;

    private OnClickListener resetButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, MainDialog.class);
            startActivity(intent);

        }
    };

    protected android.app.Dialog onCreateDialog(int id) {
        switch(id) {
        case RESET_DIALOG: 
            AlertDialog.Builder builder = new Builder(this);
            return builder
                    .setMessage("Are you sure you want to reset the count?")
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {    

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did not reset!", 5).show();

                        }
                    })

                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {


                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did Reset!", 5).show();

                        }
                    })
                    .create();
        }
        return null;
    };

然后创建了一个 MainDialog 类:(我实际上迷失了如何正确执行此操作或应用它)

package com.proteintracker;

import android.support.v4.app.DialogFragment;

public class MainDialog extends DialogFragment {
    public static MyAlertDialogFragment newInstance(int title) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }
}

我不确定我是否应该为片段创建一个新类以及如何将它应用到活动屏幕中的当前对话框。

【问题讨论】:

    标签: android android-fragments android-alertdialog


    【解决方案1】:

    您可以像这样显示您的DialogFragment:

    void showDialog() {
        DialogFragment newFragment = MyAlertDialogFragment.newInstance(
                R.string.alert_dialog_two_buttons_title);
        newFragment.show(getFragmentManager(), "dialog");
    }
    

    在您的片段对话框中,您应该覆盖onCreateDialog 并返回简单Dialog 的实例,例如AlertDialog。

    public static class MyAlertDialogFragment extends DialogFragment {
    
    public static MyAlertDialogFragment newInstance(int title) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");
    
        AlertDialog.Builder builder = new Builder(this);
        return builder
                    .setMessage("Are you sure you want to reset the count?")
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {    
    
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did not reset!", 5).show();
    
                        }
                    })
    
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    
    
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did Reset!", 5).show();
    
                        }
                    })
                    .create();
    }
    }
    

    【讨论】:

    • 我正在尝试了解 showDialog 的去向 - 它是否假设在主 Activity 文件中,并且只获取其他所有内容并为 MyAlertDialogFragment 类创建一个新文件......我正在尝试通过放置在 resetbuttonListener 的 onClick 中显示对话框,但它一直出错...
    【解决方案2】:

    带有自定义视图的警报

    public class MyAlertDialogFragment extends DialogFragment {
    
        public static final String TITLE = "dataKey";
    
        public static MyAlertDialogFragment newInstance(String dataToShow) {
            MyAlertDialogFragment frag = new MyAlertDialogFragment();
            Bundle args = new Bundle();
            args.putString(TITLE, dataToShow);
            frag.setArguments(args);
            return frag;
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            String mDataRecieved = getArguments().getString(TITLE,"defaultTitle");
    
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            LayoutInflater inflater = getActivity().getLayoutInflater();
            View view = inflater.inflate(R.layout.alert_layout, null);
    
            TextView mTextView = (TextView) view.findViewById(R.id.textview);
            mTextView.setText(mDataRecieved);
            setCancelable(false);
    
            builder.setView(view);
            Dialog dialog = builder.create();
    
            dialog.getWindow().setBackgroundDrawable(
                    new ColorDrawable(Color.TRANSPARENT));
    
            return dialog;
    
        }
    }
    

    并通过 YesNoDialog 界面发出警报

    public class MyAlertDialogFragment extends DialogFragment {
    
        public static final String TITLE = "dataKey";
        private OnYesNoClick yesNoClick;
    
        public static MyAlertDialogFragment newInstance(String dataToShow ) {
            MyAlertDialogFragment frag = new MyAlertDialogFragment();
            Bundle args = new Bundle();
            args.putString(TITLE, dataToShow);
            frag.setArguments(args);
            return frag;
        }
    
        public void setOnYesNoClick(OnYesNoClick yesNoClick) {
            this.yesNoClick = yesNoClick;
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            String mDataRecieved = getArguments().getString(TITLE,"defaultTitle");
    
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
            builder
                    .setMessage("Message to Show")
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            if(yesNoClick != null)
                                yesNoClick.onNoClicked();
                        }
                    })
    
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            if(yesNoClick != null)
                                yesNoClick.onYesClicked();
                        }
                    });
            Dialog dialog = builder.create();
    
            dialog.getWindow().setBackgroundDrawable(
                    new ColorDrawable(Color.TRANSPARENT));
    
            return dialog;
    
        }
    
        public interface OnYesNoClick{
            void onYesClicked();
            void onNoClicked();
        }
    }
    

    像这样使用它

    private void showYesNoDialog(){
            MyAlertDialogFragment yesNoAlert = MyAlertDialogFragment.newInstance(
                    "Data to Send");
            yesNoAlert.show(getFragmentManager(), "yesNoAlert");
    
            yesNoAlert.setOnYesNoClick(new MyAlertDialogFragment.OnYesNoClick() {
                @Override
                public void onYesClicked() {
                    //yes or ok clicked
                }
    
                @Override
                public void onNoClicked() {
                    //no or cancel clicked
                }
            });
        }
    

    【讨论】:

    • 活动轮换后不要忘记调用 set* 方法
    • 也适用于片段
    【解决方案3】:

    使用 Sherlock 的 DialogFragment 示例

    FragmentManager fm = getSherlockActivity().getSupportFragmentManager();
    DialogFragment dialog = new DialogFragment(){
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the Builder class for convenient dialog construction
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder
                .setTitle(getString(R.string.delete)+"?")
                .setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    // do something
                    }
                })
                .setNegativeButton(getString(android.R.string.cancel),  new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dismiss();
                    }
                });
            // Create the AlertDialog object and return it
            return builder.create();
        }
    };
    dialog.setCancelable(true);
    dialog.show(fm, "DELETE_DIALOG_FRAGMENT");
    

    【讨论】:

    • 什么是夏洛克,我不想用其他库来做这么简单的事情......
    【解决方案4】:

    你可以这样显示对话框:

    new AlertDialog.Builder(this)
    .setMessage("Are you sure you want to reset the count?")
    .setNegativeButton("No", new DialogInterface.OnClickListener() {    
    
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            Toast.makeText(MainActivity.this, "Did not reset!", 5).show();
        }
    })
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            Toast.makeText(MainActivity.this, "Did Reset!", 5).show();
    
        }
    })
    .create().show();
    

    【讨论】:

    • 我不需要开关盒来选择什么?或者你只是表明我只需要在最后添加 .show() 就足够了......
    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2018-03-08
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 2010-11-03
    相关资源
    最近更新 更多