gpt4 book ai didi

android - DialogFragment 在其 show() 之后调用 onCreateDialog()

转载 作者:行者123 更新时间:2023-11-29 17:54:57 26 4
gpt4 key购买 nike

我有一个带有 ProgressBar 的 DialogFragment:

public class DialogFragmentProgress extends DialogFragment {
final static String DTAG = "FileCmd";

View v;
ProgressBar pb = null;
TextView tv = null;
public void setMsg(String msg){
Log.d(DTAG,"ProgressBar: setMsg("+msg+")");
if (tv != null){
Log.d(DTAG,"ProgressBar: (tv != null)");
tv.setText(msg);
}else{
Log.d(DTAG,"ProgressBar: (tv == null)");
}
}

public void setProgress(int progress){
Log.d(DTAG,"ProgressBar: setProgress("+progress+")");
if (pb != null){
Log.d(DTAG,"ProgressBar: (pb != null)");
pb.setProgress(progress);
Log.d(DTAG,"ProgressBar: getProgress() return "+pb.getProgress());
}else{
Log.d(DTAG,"ProgressBar: (pb == null)");
}
}

public static DialogFragmentProgress newInstance() {

DialogFragmentProgress fragment = new DialogFragmentProgress();

Log.d(DTAG,"ProgressBar: DialogFragmentProgress.newInstance()!");
return fragment;
}
public DialogFragmentProgress() {
// TODO Auto-generated constructor stub
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

LayoutInflater inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.progressbar, null);

builder.setView(v)
.setTitle(R.string.progress_prompt)
.setNegativeButton(R.string.negative, null);

tv = (TextView)v.findViewById(R.id.progressbar_text);
pb = (ProgressBar)v.findViewById(R.id.progressbar);

pb.setMax(100);
pb.setProgress(10);

Log.d(DTAG,"ProgressBar: DialogFragmentProgress.onCreateDialog()!");

return builder.create();
}

}

在主 Activity 中:

DialogFragmentProgress dfp = new DialogFragmentProgress();
dfp.show(getSupportFragmentManager(), "");
dfp.setProgress(50);
dfp.setMsg(srcFile.getName());

日志输出:

11-19 01:42:57.744: D/FileCmd(2095): ProgressBar: setProgress(50)
11-19 01:42:57.744: D/FileCmd(2095): ProgressBar: (pb == null)
11-19 01:42:57.744: D/FileCmd(2095): ProgressBar: setMsg(.qm_guid)
11-19 01:42:57.744: D/FileCmd(2095): ProgressBar: (tv == null)
11-19 01:42:59.494: D/FileCmd(2095): ProgressBar: DialogFragmentProgress.onCreateDialog()!

请看时间,这意味着onCreateDialog()被调用的时间很晚!所以我无法获得电视和铅。有什么问题?我需要立即显示对话框,然后我可以设置 ProgressBar。

最佳答案

What's the problem?

fragment 事务是异步的,因此在您的代码中 fragment 的对话框将不可用,直到您调用 show() 的代码/方法返回。在 show() 调用之后尝试使用 getSupportFragmentManager().executePendingTransaction(),或者,更好的是,更改您当前的代码,以便 setProgress()setMsg() 存储传入的值(如果 View 可用则更新 View ),然后在 onCreateDialog() 回调中创建对话框时使用这些值。

编辑:

public class DialogFragmentProgress extends DialogFragment {
final static String DTAG = "FileCmd";
private String mMsg;
private int mCurProgress = -1;

public void setMsg(String msg) {
mMsg = msg;
// if we also have the view available update it
if (pb != null){
//... current code
}

public void setProgress(int progress){
mCurProgress = progress;
// if we also have the view available update it
if (pb != null){
// ... current code
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// current code...
// if mMsg and mCurProgress are valid it means they were set before the fragment's dialog
// was created so we need to use them at this moment
if (mMsg != null) {
// we have a valid message so use it at this moment to set the text
tv.setText(mMsg);
}
if (mCurProgress != -1) {
// we have a valid progress so use it at this moment to set the progress
pb.setProgress(mCurProgress);
}
return builder.create();
}

关于android - DialogFragment 在其 show() 之后调用 onCreateDialog(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20066401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com