问题描述
我有一个20秒倒计时顺利工作在我的问答游戏。我想添加一个进度
(不是 ProgressDialog
)到屏幕上。我发现为Android混乱的开发者指南。我GOOGLE了大量实例,并试图将它们组合成我的code。现在所有的,当我运行游戏与游戏中的每个问题时没有取得进步的空栏显示。
QuestionView.java
公共类QuestionView延伸活动{ INT correctAnswers = 0;
INT wrongAnswers = 0;
INT答案= 0;
INT I = 0; 长得分= 0; 长STARTTIME = 20000;
长间隔= 1000;
长点; 布尔timerHasStarted = FALSE; 串类; 按钮ANSWER1,ANSWER2,ANSWER3,answer4;
TextView的问题,pointCounter,questionNumber,timeCounter; ArrayList的<问题>查询;
定时器cdTimer; 进度条; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.questionviewmain); ANSWER1 =(按钮)findViewById(R.id.answer1);
ANSWER2 =(按钮)findViewById(R.id.answer2);
ANSWER3 =(按钮)findViewById(R.id.answer3);
answer4 =(按钮)findViewById(R.id.answer4); 问题=(的TextView)findViewById(R.id.question); 类别= getIntent()getStringExtra(类别)。
查询= getIntent()getParcelableArrayListExtra(查询)。 pointCounter =(的TextView)findViewById(R.id.timer);
questionNumber =(的TextView)findViewById(R.id.timeElapsedView);
timeCounter =(的TextView)findViewById(R.id.timeCounter); cdTimer =新的Timer(startTime时,间隔); 巴=(进度)findViewById(R.id.progressbar);
bar.setIndeterminate(假);
bar.setMax(9); loadQuestion();
} 公共无效loadQuestion(){ bar.setProgress(ⅰ); 如果(我== 10){ endQuiz(); }其他{ 如果(!timerHasStarted){
cdTimer.start();
timerHasStarted = TRUE;
}其他{
cdTimer.start();
timerHasStarted = FALSE;
} 答案= queries.get(I).getCorrectAnswer(); question.setText(queries.get(ⅰ).getQuery()); answer1.setText(queries.get(ⅰ).getA1());
answer2.setText(queries.get(ⅰ).getA2());
answer3.setText(queries.get(ⅰ).getA3());
answer4.setText(queries.get(ⅰ).getA4()); answer1.setOnClickListener(新OnClickListener(){
公共无效的onClick(查看为arg0){
queries.get(ⅰ).setSelectedAnswer(0);
如果(答案== 0){
correctAnswers ++;
下一个问题();
}其他{
wrongAnswers ++;
下一个问题();
}
}
}); answer2.setOnClickListener(新OnClickListener(){
公共无效的onClick(查看为arg0){
queries.get(ⅰ).setSelectedAnswer(1);
如果(回答== 1){
correctAnswers ++;
下一个问题();
}其他{
wrongAnswers ++;
下一个问题();
}
}
}); answer3.setOnClickListener(新OnClickListener(){
公共无效的onClick(查看为arg0){
queries.get(ⅰ).setSelectedAnswer(2);
如果(回答== 2){
correctAnswers ++;
下一个问题();
}其他{
wrongAnswers ++;
下一个问题();
}
}
}); answer4.setOnClickListener(新OnClickListener(){
公共无效的onClick(查看为arg0){
queries.get(ⅰ).setSelectedAnswer(3);
如果(回答== 3){
correctAnswers ++;
下一个问题();
}其他{
wrongAnswers ++;
下一个问题();
}
}
});
}
} 公众的ArrayList<问题> getQueries(){
返回查询;
} 公共无效nextQuestion(){
得分=评分+分;
我++;
loadQuestion();
} 公共类定时器扩展CountDownTimer { 公共无效startCountdownTimer(){
} 公共定时器(长的startTime,长间隔){
超(startTime时,间隔);
} @覆盖
公共无效onFinish(){
如果(ⅰ> = 9){
cdTimer.cancel();
endQuiz();
}其他{
wrongAnswers ++;
下一个问题();
}
} @覆盖
公共无效onTick(长millisUntilFinished){
timeCounter.setText(剩余时间+(millisUntilFinished / 100));
点=(millisUntilFinished / 100)/ 2;
pointCounter.setText(积分剩余:+分);
如果(ⅰ小于10){
questionNumber.setText(问题+第(i + 1)+10);
}
}
} 公共无效endQuiz(){
意向意图=新意图(QuestionView.this,Results.class);
intent.putExtra(correctAnswers,correctAnswers);
intent.putExtra(wrongAnswers,wrongAnswers);
intent.putExtra(得分,得分);
intent.putParcelableArrayListExtra(查询,查询);
intent.putExtra(类别,类别);
startActivity(意向);
}
}
XML code
<进度
机器人:ID =@ + ID /进度
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:知名度=看得见
风格=@安卓风格/ Widget.ProgressBar.Horizontal/>
New Answer
To do this let's add a new line to onTick()
:
bar.setProgress((int) Math.round(millisUntilFinished / 1000.0));
(You may need to tweak the data types, I am away from my compiler... Also I don't like the CountDownTimer class, it is inaccurate and often skips the second to last number. I wrote an alternate class here: android CountDownTimer - additional milliseconds delay between ticks)
Original Answer
I have a couple pointers:
Have you defined a maximum value for your ProgressBar?
bar.setMax(9);
I suggest loading
i
as the progress instead of the constant value of 10:bar.setProgress(i);
If you still do not see any progress ensure that you are not in indeterminate mode:
bar.setIndeterminate(false);
(This assume that you are using a ProgressBar that can depict progress.)
Addition
Move this code into onCreate()
:
bar = (ProgressBar)findViewById(R.id.progressbar);
bar.setIndeterminate(false); // May not be necessary
bar.setMax(9);
Then move this line to loadQuestion()
:
bar.setProgress(i);
Otherwise the progress will never be updated since you only create one CountDownTimer and you never actually call startCountdownTimer()
.
这篇关于用countdowntimer进度 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!