当我尝试在onPreferenceChange监听器中显示ProgressDialog(简单的,微调器类型)时,我遇到了一个非常有趣的问题.
public class SettingsActivity extends PreferenceActivity {
private ProgressDialog dialog;
public void onCreate(Bundle savedInstanceState) {
ListPreference pref= (ListPreference) findPreference("myPreference");
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
dialog = ProgressDialog.show(SettingsActivity.this, "", "Doing stuff...", true);
SystemClock.sleep(2000);
}
return true;
}
}
ProgressDialog显示,但直到方法(在这种情况下为睡眠)完成.我究竟做错了什么?
解决方法:
您正在主UI空间线程上休眠,这会阻止操作系统处理您的应用程序事件.这将阻止您的应用重绘,正如您所发现的那样,可以防止新窗口实际出现.
不要睡觉,试试这个:
final ProgressDialog dialog = ProgressDialog.show(
SettingsActivity.this, "", "Doing stuff...", true);
new Handler().postDelayed(new Runnable() {
public void run() {
dialog.hide();
}
}, 2000);