更新显示的对话框

时间:2015-06-05 14:05:45

标签: android dialog

我使用自定义布局创建了Dialog

public class WaitDialog extends DialogFragment {
    [..]
    @SuppressLint("InflateParams")
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View viewDlg = inflater.inflate(R.layout.dialog_finish, null);
        tvStatusMain = (TextView) viewDlg.findViewById(R.id.tvStatusMain);
        [..]
    }
    public void setMessage(String message) {
        tvStatusMain.setText(message);
    }
}

这段代码运行正常,但是当我像这样实例化Dialog时:

flexWaitDialog = new WaitDialog();
flexWaitDialog.setCancelable(false);
flexWaitDialog.show(this.getFragmentManager(), "login");

然后尝试更新消息:

flexWaitDialog.setMessage(getString(R.string.initialising));

它显示了布局中的默认字符串。但是,当我使用Handler

new Handler().post(new Runnable() {
    @Override
    public void run() {
        flexWaitDialog.setMessage(getString(R.string.initialising));
    }
});

确实有效。这种箍跳是否有原因?如果没有,有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

我认为onResumeonCreateDialog之前被调用。

您可以修改您的类来处理:

public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            LayoutInflater inflater = getActivity().getLayoutInflater();
            View viewDlg = inflater.inflate(R.layout.dialog_finish, null);
            tvStatusMain = (TextView) viewDlg.findViewById(R.id.tvStatusMain);
            if (mMessage != null) {
                tvStatusMain.seText(mMessage);
            }
            mViewCreated = true;
            [..]
        }

        public void setMessage(String message) {
            if (mViewCreated) {
                tvStatusMain.setText(message);
            } else {
                mMessage = message
            }           
        }