相关疑难解决方法(0)

DialogFragment OnCreateView与OnCreateDialog的自定义布局

我正在尝试使用自己的布局创建一个DialogFragment.

我见过几种不同的方法.有时布局在OnCreateDialog中设置如下:(我正在使用Mono,但我已经习惯了Java)

public override Android.App.Dialog OnCreateDialog (Bundle savedInstanceState)
{
    base.OnCreateDialog(savedInstanceState);
    AlertDialog.Builder b = new AlertDialog.Builder(Activity);
        //blah blah blah
    LayoutInflater i = Activity.LayoutInflater;
    b.SetView(i.Inflate(Resource.Layout.frag_SelectCase, null));
    return b.Create();
}
Run Code Online (Sandbox Code Playgroud)

第一种方法对我有用......直到我想findViewByID. 在谷歌搜索后使用,我尝试了第二种方法,涉及覆盖OnCreateView

所以我注释掉了两行OnCreateDialog设置布局然后添加了这个:

public override Android.Views.View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View v = inflater.Inflate(Resource.Layout.frag_SelectCase, container, false);
        //should be able to use FindViewByID here...
    return v;
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个可爱的错误:

11-05 22:00:05.381: E/AndroidRuntime(342): FATAL EXCEPTION: main
11-05 22:00:05.381: E/AndroidRuntime(342): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
Run Code Online (Sandbox Code Playgroud)

我很难过.

java mono android xamarin.android android-inflate

74
推荐指数
6
解决办法
6万
查看次数

Android:禁用DialogFragment确定/取消按钮

如何在使用AlertDialog创建DialogFragment时禁用"确定/取消"按钮?我尝试调用myAlertDialogFragment.getDialog(),但即使显示片段,它也始终返回null

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");

        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) {
                            ((FragmentAlertDialog)getActivity()).doPositiveClick();
                        }
                    }
                )
                .setNegativeButton(R.string.alert_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((FragmentAlertDialog)getActivity()).doNegativeClick();
                        }
                    }
                )
                .create();
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过膨胀包含取消和确定按钮的布局来实现它,但我宁愿使用AlertDialog解决方案,如果可能的话

android button android-alertdialog dialogfragment

11
推荐指数
2
解决办法
1万
查看次数