如何使用setProgressDrawable()是否正确? [英] How to use setProgressDrawable() correctly?

查看:193
本文介绍了如何使用setProgressDrawable()是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,新绘制对象设置为我的进度。

I am having problem with setting a new Drawable to my ProgressBar.

如果我使用setProgressDrawable()内的onCreate()方法,它的伟大工程。但是,当我尝试调用处理程序回调后里面同样的方法它不工作和进度在前看不见。

If I use the setProgressDrawable() inside onCreate() method it works great. But when I try to call the same method inside a Handler post callback it doesn't work and the progressbar disapears.

有人可以解释这种现象?我该如何解决这个问题呢?

Can someone explain this behaviour? How can I solve this problem?

推荐答案

碰到这个问题我自己,我设法得到它的工作:)

Bumped into this problem myself and I managed to get it working :)

我用 的AsyncTask 处理后台任务/线程,但思路应该是一样使用 Runnable接口/处理器(虽然的AsyncTask 确实感觉更好IMO)。

I used the AsyncTask to handle the background tasks/threads, but the idea should be the same as using Runnable/Handler (though AsyncTask does feel nicer imo).

所以,这是我做过什么...把的setContentView(R.layout.my_screen); onPostExecute 的方法! (即不是的onCreate 方法)

So, this is what I did... put setContentView(R.layout.my_screen); in the onPostExecute method! (ie. instead of the onCreate method)

所以,code看起来是这样的:

So the code looks something like this:

public class MyScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.my_screen); !!! Don't setContentView here... (see bottom)

    new MySpecialTask().execute();
}

private int somethingThatTakesALongTime() {
    int result;
    // blah blah blah
    return result;
}

private void updateTheUiWithResult(int result) {
    // Some code that changes the UI
    // For exampe:
    TextView myTextView = (TextView) findViewById(R.id.result_text);
    myTextView.setText("Result is: " + result);

    ProgressBar anyProgressBar = (ProgressBar) findViewById(R.id.custom_progressbar);
    anyProgressBar.setProgressDrawable(res.getDrawable(R.drawable.progressbar_style));
    anyProgressBar.setMax(100);
    anyProgressBar.setProgress(result);       
}

private class MySpecialTask extends AsyncTask<String, Void, Integer> {
    ProgressDialog mProgressDialog;

    @Override
    protected void onPreExecute() {
        mProgressDialog = ProgressDialog.show(MyScreen.this, "", "Calculating...\nPlease wait...", true);
    }
    @Override
    protected Integer doInBackground(String... strings) {
        return somethingThatTakesALongTime();
    }

    @Override
    protected void onPostExecute(Integer result) {
        mProgressDialog.dismiss();
        setContentView(R.layout.my_screen); // setContent view here... then it works...
        updateTheUiWithResult(result);
    }
}
}

说实话,你为什么需要调用的setContentView onPostExecute 我不知道......但这样做这样意味着你可以设置自定义样式的进度条(他们不要你消失了!)

To be honest, why you need to call setContentView in onPostExecute I have no idea... but doing so means you can set custom styles for your progress bars (and they don't disappear on you!)

这篇关于如何使用setProgressDrawable()是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆