DialogFragment以及AlertDialogFragment

来源:互联网 发布:淘宝店铺装饰 编辑:程序博客网 时间:2024/05/29 11:26

          作为一个拖延症严重类型的病人,终于开始这篇文章了,如果看到这篇文章的朋友,谁有对付拖延症的好的方法,谢谢推荐一下。代码部分昨天完成,其实是在上一周,在Activity(4.0开发环境下,Fragment学习资料:android之Fragment(官网资料翻译))使用AlertDialog,被告知说showDialog(int id)这个方法Deprecated。被弃用了,建议使用DialogFragment。


         就开始dialogFragment的学习吧,先看下说明:

         A fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

         一个显示一个对话框窗口的Fragment,漂浮在其Activity显示窗口的顶部。这个Fragment包含一个对话框对象,它将基于Fragment的状态适当的选择显示。应该通过API来控制Dialog(决定何时显示、隐藏、销毁),而不是直接调用Dialog。

        Implementations should override this class and implement onCreateView(LayoutInflater, ViewGroup, Bundle) to supply the content of the dialog. Alternatively, they can override onCreateDialog(Bundle) to create an entirely custom dialog, such as an AlertDialog, with its own content.

        实现DialogFragment是通过继承DialogFragment并且实现 onCreateView(LayoutInflater, ViewGroup, Bundle) 这个方法来提供对话框内容。或者也可以重写 onCreateDialog(Bundle)这个方法来创建一个自定义的对话框,像AlertDialog,有它自己的内容。

       下面开始一个简单的例子:

        主Activity:

public class DialogFragmentDemoActivity extends Activity implements OnClickListener{    /** Called when the activity is first created. */private Button mDialogButton;private Button mAlertDialogButton;private int mStackLevel;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                mDialogButton = (Button)findViewById(R.id.dialog_button);        mAlertDialogButton = (Button)findViewById(R.id.alert_dialog_button);        mDialogButton.setOnClickListener(this);        mAlertDialogButton.setOnClickListener(this);    }@Overridepublic void onClick(View v) {switch(v.getId()) {case R.id.dialog_button:showDialog();break;case R.id.alert_dialog_button:showAlertDialog();break;}}void showDialog() {    mStackLevel++;    // DialogFragment.show() will take care of adding the fragment    // in a transaction.  We also want to remove any currently showing    // dialog, so make our own transaction and take care of that here.    FragmentTransaction ft = getFragmentManager().beginTransaction();    Fragment prev = getFragmentManager().findFragmentByTag("dialog");    if (prev != null) {        ft.remove(prev);    }    ft.addToBackStack(null);    // Create and show the dialog.    DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);    newFragment.show(ft, "dialog");}void showAlertDialog() {    DialogFragment newFragment = MyAlertDialogFragment.newInstance(            R.string.alert_dialog_button);    newFragment.show(getFragmentManager(), "dialog");}public void doPositiveClick() {    // Do stuff here.    Log.i("FragmentAlertDialog", "Positive click!");}public void doNegativeClick() {    // Do stuff here.    Log.i("FragmentAlertDialog", "Negative click!");}}

主要看下showDialog的注释说明: DialogFragment.show() will take care of adding the fragment in a transaction.  We also want to remove any currently showing dialog, so make our own transaction and take care of that here.

        DialogFragment.show()会管理添加到事务里面的Fragment。在这里,我们想要删除当前显示的对话框,就必须用自己的事务进行Fragment管理。

       看下MyDialogFragment和MyAlertDialogFragment的定义:

public class MyDialogFragment extends DialogFragment {int mNum;    /**     * Create a new instance of MyDialogFragment, providing "num"     * as an argument.     */    static MyDialogFragment newInstance(int num) {        MyDialogFragment f = new MyDialogFragment();        // Supply num input as an argument.        Bundle args = new Bundle();        args.putInt("num", num);        f.setArguments(args);        return f;    }    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mNum = getArguments().getInt("num");        // Pick a style based on the num.        int style = DialogFragment.STYLE_NORMAL, theme = 0;        switch ((mNum-1)%6) {            case 1: style = DialogFragment.STYLE_NO_TITLE; break;            case 2: style = DialogFragment.STYLE_NO_FRAME; break;            case 3: style = DialogFragment.STYLE_NO_INPUT; break;            case 4: style = DialogFragment.STYLE_NORMAL; break;            case 5: style = DialogFragment.STYLE_NORMAL; break;            case 6: style = DialogFragment.STYLE_NO_TITLE; break;            case 7: style = DialogFragment.STYLE_NO_FRAME; break;            case 8: style = DialogFragment.STYLE_NORMAL; break;        }        switch ((mNum-1)%6) {            case 4: theme = android.R.style.Theme_Holo; break;            case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;            case 6: theme = android.R.style.Theme_Holo_Light; break;            case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;            case 8: theme = android.R.style.Theme_Holo_Light; break;        }        setStyle(style, theme);    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        View v = inflater.inflate(R.layout.fragment_dialog, container, false);        View tv = v.findViewById(R.id.text);        ((TextView)tv).setText("Dialog #" + mNum + ": using style "                + "getNameForNum(mNum)");        // Watch for button clicks.        Button button = (Button)v.findViewById(R.id.dialog_show);        button.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                // When button is clicked, call up to owning activity.                ((DialogFragmentDemoActivity)getActivity()).showDialog();            }        });        return v;    }}

在onCreate()方法里面通过num参数来设定DialogFragment的格式,看一下setArgument()这个方法:

Supply the construction arguments for this fragment.

为Fragment提供构造参数。

setStyle()方法:

Call to customize the basic appearance and behavior of the fragment's dialog. This can be used for some common dialog behaviors, taking care of selecting flags, theme, and other options for you. The same effect can be achieve by manually setting Dialog and Window attributes yourself. Calling this after the fragment's Dialog is created will have no effect.

Parameters
styleSelects a standard style: may be STYLE_NORMALSTYLE_NO_TITLESTYLE_NO_FRAME, or STYLE_NO_INPUT.themeOptional custom theme. If 0, an appropriate theme (based on the style) will be selected for you.

在自定义Fragment对话框的外观和行为时调用,通常被用于设定一些常见对话框的行为,像选择Flag、主题以及其他。同样你也可以通过手动选择来设置对话框的属性,但是在Fragment的Dialog创建以后,调用该方法无效。

       傲慢的上校原创作品,转载请说明出处:http://blog.csdn.net/aomandeshangxiao/article/details/7790520

        再看MyAlertDialogFragment:

public 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");        return new AlertDialog.Builder(getActivity())//                .setIcon(R.drawable.alert_dialog_icon)                .setTitle(title)                .setPositiveButton(R.string.alert_dialog_ok,                    new DialogInterface.OnClickListener() {                        public void onClick(DialogInterface dialog, int whichButton) {                            ((DialogFragmentDemoActivity)getActivity()).doPositiveClick();                        }                    }                )                .setNegativeButton(R.string.alert_dialog_cancel,                    new DialogInterface.OnClickListener() {                        public void onClick(DialogInterface dialog, int whichButton) {                            ((DialogFragmentDemoActivity) getActivity()).doNegativeClick();                        }                    }                )                .create();    }}

比较简单,在onCreateDialog里面返回一个自定义的dialog。

       找了近一个周的工作,也非常感谢CSDN上面好友们的帮助,现在工作不太好找,请换工作朋友三思。

最后,文件下载地址:http://download.csdn.net/detail/aomandeshangxiao/4456176


       大体写道如此,做饭吃饭去了。。。



原创粉丝点击