在OnClickListener运行时如何让ProgressBar显示?

时间:2016-02-16 13:39:28

标签: java android button onclicklistener android-progressbar

我有一个简单的Android程序,可以计算手机计算某个数学问题需要多长时间。我想在按下按钮时启动数学问题,当它运行时我想要显示一个旋转的进度条,我希望它在数学问题完成后消失。这是我目前的代码:

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方法,这不是我想要它做的。我希望仅在程序“工作”时显示进度条。我该怎么做呢?

谢谢

1 个答案:

答案 0 :(得分:1)

正如Blackbelt和vilpe89所提到的,你必须将线程分开。您可以通过另一个扩展ASyncTask的类来执行此操作,该类将处理计算。问题是进度对话框需要在UI线程上运行。您可以使用一个界面来更改主类中的进度对话框。

计算器类:

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

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 duration),以便将计算的持续时间传递回主线程。