如何让进度条显示的同时OnClickListener运行? [英] How to get ProgressBar to show while OnClickListener is running?

查看:226
本文介绍了如何让进度条显示的同时OnClickListener运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Andr​​oid程序,计算需要多长时间的电话计算一定的数学问题。我想开始的数学问题,当我打的按钮,而它正在运行我想要要显示的旋转进度条,我想这道数学题完成后消失。这是code我目前有:

I have a simple Android program that calculates how long it takes the phone to compute a certain mathematical problem. I want the mathematical problem to start when I hit the button, and while it is running I want a spinning progress bar to be displayed, and I want it to disappear after the math problem is done. This is the code I currently have:

public class main extends AppCompatActivity {
private TextView mScore;
private Button mRunButton;
private TextView mScoreText;
private ProgressBar mSpinner;
@Override


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mScore = (TextView) findViewById(R.id.score);
    mRunButton = (Button) findViewById(R.id.runbutton);
    mScoreText = (TextView) findViewById(R.id.scoreText);
    mSpinner = (ProgressBar) findViewById(R.id.progress);

    mSpinner.setVisibility(View.GONE);
    View.OnClickListener listener = new View.OnClickListener(){
        @Override

        public void onClick(View v) {
            mSpinner.setVisibility(View.VISIBLE);

            long startTime = System.nanoTime();
            long start  = System.currentTimeMillis();

            long count = 0l;
            for(long x=0;x<Integer.MAX_VALUE ;x++){
                count+=1;
            }
            long end = System.currentTimeMillis();

            long endTime = System.nanoTime();
            long duration = ((endTime - startTime) / 1000000);

            mScore.setText(duration + "");
            mSpinner.setVisibility(View.GONE);

        }
    };

    mRunButton.setOnClickListener(listener);
}

}

据我所知,没有在应用程序更新的视图,直到后手机与整个的onClick方法,这是不是我想要它做的事做。我要显示进度条只在程序工作。我怎么会去这样做呢?

From what I can tell, nothing in the view of the app updates until after the phone is done with the entire onClick method, which is not what I want it to do. I want the progress bar to be displayed ONLY while the program is 'working'. How would I go about doing this?

感谢您

推荐答案

作为黑带和vilpe89提到的,你必须将线程分离。您可以通过具有延伸的AsyncTask将处理计算另一类做到这一点。但问题是,进度对话框需要在UI线程上运行。你可以改变主类中的进度对话框的界面。

As Blackbelt and vilpe89 mentioned, you have to separate the threads. You can do this by having another class that extends ASyncTask which will handle the calculations. The problem with that is that the progress dialog needs to run on the UI thread. You can have an interface that changes the progress dialog in the main class.

计算器类:

public final class Calculator extends AsyncTask<Void, Void, Void> {

Context context;
calcCallback mCallback;

public Calculator(Context c) {
    this.context = c;
    this.mCallback = (calcCallback) c;
}

//The main class needs to implement this interface

public interface calcCallback {
    Void calcDone();
    Void calcStarted();
    //Other methods if necessary
}

@Override
protected Boolean doInBackground(Void... params) {
    mCallback.calcStarted();
    //Your calculations here
    mCallback.calcDone();
    return null;
}
}

MainActivity

MainActivity

public class MainActivity extends Activity implements Calculator.calcCallback,  {

private TextView mScore;
private Button mRunButton;
private TextView mScoreText;
private ProgressBar mSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mScore = (TextView) findViewById(R.id.score);
mRunButton = (Button) findViewById(R.id.runbutton);
mScoreText = (TextView) findViewById(R.id.scoreText);
mSpinner = (ProgressBar) findViewById(R.id.progress);

mSpinner.setVisibility(View.GONE);
View.OnClickListener listener = new View.OnClickListener(){
    @Override

    public void onClick(View v) {
        Calculator calculator = new Calculator(MainActivity.this);
        calculator.execute();
    }
};

mRunButton.setOnClickListener(listener);

}

@Override
public Void calcStarted() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mSpinner.setVisibility(View.VISIBLE);
            }
        });
    }
    return null;
}
@Override
public Void calcDone() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mSpinner.setVisibility(View.GONE);
            }
        });
    }
    return null;
}
}

您还可以设置calcDone()作为calcDone(INT持续时间),这样就可以通过计算的持续回主线程。

You can also set up your calcDone() as calcDone(int duration) so that you can pass the calculated duration back to the main thread.

这篇关于如何让进度条显示的同时OnClickListener运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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