我使用自定义布局创建了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));
}
});
确实有效。这种箍跳是否有原因?如果没有,有更好的方法吗?
答案 0 :(得分:0)
我认为onResume
在onCreateDialog
之前被调用。
您可以修改您的类来处理:
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
}
}