我在android中做了一个进度条,这是横向的,我想在我的活动中点击任何按钮时冻结它,我已经尝试过如下,但是进度仍然显示。我的代码如下:我想做点什么就像一个接一个的问题加载,并点击任何按钮计数器和进度状态应停止,但现在我的计时器停止但进度仍然上升..不停止。
码
public class QuestionAnswerActivity extends Activity implements OnClickListener {
// visible gone
ArrayList<HashMap<String, String>> QuestionList;
private int progressStatus = 0;
private Handler handler = new Handler();
Intent i;
private boolean run = true;
/**
* Answer [1=true / 0 =false]
*/
int myans;
String ans;
ProgressDialog pDialog;
TextView text_player_one;
String JsonStr;
ArrayList<HashMap<String, String>> questionList;
ImageView player_one_pic;
int count;
private DisplayImageOptions options;
public static ImageLoader imageLoader;
ImageView answer_right_one;
ProgressBar pg_loading;
private CountDownTimer countDownTimer;
TextView timer_text;
private final long startTime = 8 * 1000;
private final long interval = 1 * 1000;
Button opt_1, opt_2, opt_3, opt_4;
Thread splashThread;
TextView answer_question;
LinearLayout answer_linear_3;
// Response variables...!!
// Single player*************************************
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.question_answer_activity);
init();
}
// ***********************************************************************************************
/**
* Initialise the views
*/
@SuppressWarnings({ "deprecation", "unchecked" })
private void init() {
answer_linear_3 = (LinearLayout) findViewById(R.id.answer_linear_3);
answer_linear_3.setVisibility(View.GONE);
questionList = new ArrayList<HashMap<String, String>>();
answer_question = (TextView) findViewById(R.id.answer_question);
player_one_pic = (ImageView) findViewById(R.id.player_one_pic);
text_player_one = (TextView) findViewById(R.id.text_player_one);
text_player_one.setText(Pref.getValue(QuestionAnswerActivity.this,
Const.PREF_NAME, ""));
pg_loading = (ProgressBar) findViewById(R.id.pg_loading_answer);
timer_text = (TextView) findViewById(R.id.timer_text_loading11);
answer_right_one = (ImageView) findViewById(R.id.answer_right_one);
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration
.createDefault(QuestionAnswerActivity.this));
options = new DisplayImageOptions.Builder().cacheOnDisc(true)
.displayer(new RoundedBitmapDisplayer((int) 3.5f))
.showStubImage(R.drawable.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher).build();
imageLoader.displayImage(Pref.getValue(QuestionAnswerActivity.this,
Const.PREF_PROFILE_PIC, ""), player_one_pic, options);
opt_1 = (Button) findViewById(R.id.option_1);
opt_2 = (Button) findViewById(R.id.option_2);
opt_3 = (Button) findViewById(R.id.option_3);
opt_4 = (Button) findViewById(R.id.option_4);
count = 0;
questionList = (ArrayList<HashMap<String, String>>) getIntent()
.getSerializableExtra("queList");
System.out.println("::::::::My questions::::::::;+++++++"
+ questionList);
/*
* answer_question.setText(questionList.get(count).get("q_title"));
* opt_1.setText(questionList.get(count).get("opt1"));
* opt_2.setText(questionList.get(count).get("opt2"));
* opt_3.setText(questionList.get(count).get("opt3"));
* opt_4.setText(questionList.get(count).get("ans"));
*/
handler.postDelayed(new ViewUpdater(answer_linear_3), 1000);
answer_question.setText(questionList.get(count).get("q_title"));
opt_1.setText(questionList.get(count).get("opt1"));
opt_2.setText(questionList.get(count).get("opt2"));
opt_3.setText(questionList.get(count).get("opt3"));
opt_4.setText(questionList.get(count).get("ans"));
opt_1.setOnClickListener(this);
opt_2.setOnClickListener(this);
opt_3.setOnClickListener(this);
opt_4.setOnClickListener(this);
countDownTimer = new MyCountDownTimer(startTime, interval);
/*
* Initialise NoResponse Timer
*/
timer_text.setText(timer_text.getText()
+ String.valueOf(startTime / 1000));
countDownTimer.start();
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
// current value in the text view
handler.post(new Runnable() {
public void run() {
if (run) {
pg_loading.setProgress(progressStatus);
pg_loading.setProgressDrawable(getResources()
.getDrawable(R.drawable.progress));
}
}
});
try {
// Sleep for 200 milliseconds.
// Just to display the progress slowly
Thread.sleep(62);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
// ***********************************************************************************************
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
run = false;
switch (v.getId()) {
case R.id.option_1:
if (count <= 7) {
count++;
progressStatus = pg_loading.getProgress();
/* questionTask(count); */
new LeaveResponse()
.execute(questionList.get(count).get("q_id"));
}
break;
case R.id.option_2:
if (count <= 7) {
count++;
progressStatus = pg_loading.getProgress();
new LeaveResponse()
.execute(questionList.get(count).get("q_id"));
}
break;
case R.id.option_3:
if (count <= 7) {
count++;
progressStatus = pg_loading.getProgress();
new LeaveResponse()
.execute(questionList.get(count).get("q_id"));
}
break;
case R.id.option_4:
if (count <= 7) {
count++;
progressStatus = pg_loading.getProgress();
new LeaveResponse()
.execute(questionList.get(count).get("q_id"));
}
/*
* answer_question.setText(QuestionList.get(count).get("q_title"));
* opt_1.setText(QuestionList.get(count).get("opt1"));
* opt_2.setText(QuestionList.get(count).get("opt2"));
* opt_3.setText(QuestionList.get(count).get("opt3"));
* opt_4.setText(QuestionList.get(count).get("ans"));
* timer_text.setText(timer_text.getText() +
* String.valueOf(startTime / 1000));
*
* countDownTimer.start();
* answer_right_one.setVisibility(View.VISIBLE);
* pg_loading.setVisibility(View.GONE); countDownTimer.cancel();
*
* Toast.makeText(QuestionAnswerActivity.this,
* timer_text.getText().toString(), 1).show();
*/
break;
}
}
// ***********************************************************************************************
/**
* CountDown Timer for Starting No response activity if user doesnt
* responds.
*/
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
public void onpause() {
// TODO Auto-generated method stub
Toast.makeText(QuestionAnswerActivity.this, "Clicked", 1).show();
}
@Override
public void onFinish() {
Toast.makeText(getApplicationContext(), "onFinish",
Toast.LENGTH_SHORT).show();
/*
* If finish , that is user not responding , go to NoResponse
* screen.
*/
Intent intent = null;
intent = new Intent(QuestionAnswerActivity.this,
NoResponseActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
finish();
}
@Override
public void onTick(long millisUntilFinished) {
timer_text.setText("" + millisUntilFinished / 1000);
}
}
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
void questionTask(int count) throws InterruptedException {
if (count < 8) {
answer_linear_3.setVisibility(View.GONE);
answer_question.setText(questionList.get(count).get("q_title"));
handler.postDelayed(new ViewUpdater(answer_linear_3), 100);
myans = randomGenerator();
System.out.println(":::::::::::::;randomized answer::::::::::::;;;"
+ myans);
if (myans == 1) {
opt_1.setBackgroundDrawable(getResources().getDrawable(
R.drawable.true_selector));
opt_1.setText(questionList.get(count).get("ans"));
opt_2.setText(questionList.get(count).get("opt2"));
opt_3.setText(questionList.get(count).get("opt3"));
opt_4.setText(questionList.get(count).get("opt1"));
} else if (myans == 2) {
opt_2.setBackgroundDrawable(getResources().getDrawable(
R.drawable.true_selector));
opt_1.setText(questionList.get(count).get("opt2"));
opt_2.setText(questionList.get(count).get("ans"));
opt_3.setText(questionList.get(count).get("opt3"));
opt_4.setText(questionList.get(count).get("ans"));
} else if (myans == 3) {
opt_3.setBackgroundDrawable(getResources().getDrawable(
R.drawable.true_selector));
opt_1.setText(questionList.get(count).get("opt1"));
opt_2.setText(questionList.get(count).get("opt2"));
opt_3.setText(questionList.get(count).get("ans"));
opt_4.setText(questionList.get(count).get("opt3"));
} else {
opt_4.setBackgroundDrawable(getResources().getDrawable(
R.drawable.true_selector));
opt_1.setText(questionList.get(count).get("opt1"));
opt_2.setText(questionList.get(count).get("opt2"));
opt_3.setText(questionList.get(count).get("opt3"));
opt_4.setText(questionList.get(count).get("ans"));
}
countDownTimer = new MyCountDownTimer(startTime, interval);
/*
* Initialise NoResponse Timer
*/
timer_text.setText(timer_text.getText()
+ String.valueOf(startTime / 1000));
countDownTimer.start();
}
}
private class ViewUpdater implements Runnable {
private LinearLayout mView;
public ViewUpdater(LinearLayout linView) {
mView = linView;
}
@Override
public void run() {
mView.setVisibility(View.VISIBLE);
}
}
// Answer webservice call..!
private class LeaveResponse extends AsyncTask<String, String, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
countDownTimer.cancel();
pDialog = new ProgressDialog(QuestionAnswerActivity.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
BackendAPIService sh = new BackendAPIService();
String ResponseURL = Const.API_RESPONSE
+ "?os=droid&user_id="
+ Pref.getValue(QuestionAnswerActivity.this,
Const.PREF_USER_ID, "") + "&myrole="
+ Const.MY_ROLL + "&q_id=" + params[0] + "&ans=" + ans
+ "&time=5&game_id=" + Const.GAME_ID + "";
JsonStr = sh.makeServiceCall(ResponseURL, BackendAPIService.GET);
System.out.println("::::::::::::;My Response request::::::::::;;;"
+ ResponseURL);
Log.d("Response: ", "> " + JsonStr);
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
try {
countDownTimer.cancel();
pg_loading.setProgress(0);
progressStatus = 0;
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
// current value in the text view
handler.post(new Runnable() {
public void run() {
if (run) {
pg_loading.setProgress(progressStatus);
pg_loading
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress));
}
}
});
try {
// Sleep for 200 milliseconds.
// Just to display the progress slowly
Thread.sleep(62);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
questionTask(count);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int randomGenerator() {
Random r = new Random();
int ans = r.nextInt(4 - 1) + 1;
return ans;
}
}
答案 0 :(得分:0)
你可以轻松地为这种情况保留一些布尔变量:
private boolean run = true;
然后在一个单击侦听器中收集所有按钮onClickListeners
,就像类的主要部分一样,并且在任意点击时创建该变量run=false
,因此,在设置进度之前检查:
if(run){
pg_loading.setProgress(progressStatus);
}else{
//stop, finish, do whatever you want
}
onClick侦听器中的:
public void onClick(View v) {
// TODO Auto-generated method stub
run = false;
Intent i;
switch (v.getId()) {
.
.
.
}
<强>更新强>
使线程如下:
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
// current value in the text view
handler.post(new Runnable() {
public void run() {
if(run){
pg_loading.setProgress(progressStatus);
pg_loading.setProgressDrawable(getResources()
.getDrawable(R.drawable.progress));
}
}
});
try {
// Sleep for 200 milliseconds.
// Just to display the progress slowly
Thread.sleep(62);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
更新2:
onPostExcute
中的AsyncTask
必须是:
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
try {
countDownTimer.cancel();
pg_loading.setProgress(0);
progressStatus = 0;
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
// current value in the text view
handler.post(new Runnable() {
public void run() {
pg_loading.setProgress(progressStatus);
pg_loading
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress));
}
});