AlertDialog用的onCreate自定义视图() [英] AlertDialog with custom view in onCreate()

查看:470
本文介绍了AlertDialog用的onCreate自定义视图()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:

    public class MainActivity extends Activity {

    private static final int CONFIRMATION_DIALOG = 0;
    private View mLoginConfirmView;
    private TextView mTextViewLoginConfirm;
    private CheckBox mCheckBoxLoginConfirm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mLoginConfirmView = View.inflate(this, R.layout.dialog_login_confirmation, null);
        mTextViewLoginConfirm = (TextView) mLoginConfirmView.findViewById(R.id.textView_DialogTranspConfirm);
        mCheckBoxLoginConfirm = (CheckBox) mLoginConfirmView.findViewById(R.id.checkBox_DialogTranspConfirm);

        showDialog(CONFIRMATION_DIALOG);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case CONFIRMATION_DIALOG: {


            return new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.stat_sys_warning)
            .setTitle(R.string.app_name)
            .setView(mLoginConfirmView)
            .setPositiveButton(R.string.change, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    showDialog(EXIT_PROGRESS_DIALOG);
                }
            })
            .setNegativeButton(R.string.go_ahead, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    if (mCheckBoxLoginConfirm.isChecked())
                        setConfirmationDialogHidden();
                }
            })
            .create();
        }
            break;

        default:
            break;
        }
        return super.onCreateDialog(id);
    }

    @Override
    @Deprecated
    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
        case CONFIRMATION_DIALOG: 

            CheckBox checkBox = (CheckBox)dialog.findViewById(R.id.checkBox_DialogTranspConfirm);
            checkBox.setChecked(false);
            TextView textView = (TextView)dialog.findViewById(R.id.textView_DialogTranspConfirm);
            textView.setText(getString(R.string.you_use_service_for, Utils.getNumber()));


            break;

        default:
            break;
        }
        super.onPrepareDialog(id, dialog);
    }
}

据崩溃只的HTC One S,当从就行了,在这里我把我的自定义视图背景我的应用程序将返回:

It crashes only on HTC One S, when my app returns from background on the line, where I set my custom view:

.setView(mLoginConfirmView)

.setView(mLoginConfirmView)

投掷:java.lang.IllegalStateException:指定的小孩已经有一个父。您必须先对孩子的父母打电话removeView()。
    我已经决定增加以下检查:如果该视图有一个家长,我什么都不做:

Throwing: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. I've decided to add following check: if the view has a parent, I do nothing:

@Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case CONFIRMATION_DIALOG: {

        ViewGroup parent = (ViewGroup) mLoginConfirmView.getParent();
            if (parent!=null)
                return super.onCreateDialog(id);

            return new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.stat_sys_warning)
            .setTitle(R.string.app_name)
            .setView(mLoginConfirmView)
            .setPositiveButton(R.string.change, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    showDialog(EXIT_PROGRESS_DIALOG);
                }
            })
            .setNegativeButton(R.string.go_ahead, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    if (mCheckBoxLoginConfirm.isChecked())
                        setConfirmationDialogHidden();
                }
            })
            .create();
        }
            break;

        default:
            break;
        }
        return super.onCreateDialog(id);
    }

这是正确的方式?我可以显示对话框从的onCreate()?

Is it correct way? Can I show dialogs from onCreate()?

推荐答案

这会崩溃作为活动的用户界面尚未prepared和活动尚未显示给用户,Android的显示之后的屏幕的onResume()执行完成后,尝试显示来自onResume()对话框

It will crash as the UI of the activity is not yet prepared, and activity is not yet shown to the user, android shows the screen after its onResume() execution is completed, try showing the dialog from onResume()

这篇关于AlertDialog用的onCreate自定义视图()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆