English 中文(简体)
显示进展 参考行动中的诊断
原标题:Displaying ProgressDialog within a PreferenceActivity

在我试图在<条形码>内显示“ProgressDial(简单、次要的类型)”时,我便提出一个非常有趣的问题。

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上显示,但直到方法(sleep in this case)完成为止。 我做了什么错误?

最佳回答

http://developer.android.com/resources/articles/painless-threading.html” rel=“nofollow noretinger”http://developer.android.com/resources/articles/painless-threading.html

如果你只想援引<代码> leep方法,那么这很可能是不必要的,但甚至应该为其他方法工作,否则就会阻碍国际交易日志的校对。

你可以这样做:

private class BackgroundTask extends AsyncTask<Integer, Integer, void> {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(SettingsActivity.this, "", "Doing stuff...", true);
    }

    @Override
    protected void doInBackground(Integer... params) {
        sleep(params[0]);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        dialog.dismiss();
    }
 }

除使用以下代码外:

new BackgroundTask().execute(2000);
问题回答

你在主线上睡觉,这阻止了操作系统处理你的申请事件。 这将阻止你们的再造,正如你发现的那样,能够防止新窗口实际上出现。

不要睡觉,而是试图:

final ProgressDialog dialog = ProgressDialog.show(
    SettingsActivity.this, "", "Doing stuff...", true);
new Handler().postDelayed(new Runnable() {
    public void run() {
      dialog.hide();
    }
  }, 2000);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签