Iam已經完成了IQ測試app.Iam使用定時器來計算完成IQTest中的問題所需的時間。當我暫停定時器時,我會去Timeplease活動。這是正確的,但是當我回到測試時,計時器不會恢復。也有人可以告訴如何計算所需的時間來回答正確的問題。當我回到QuizActivity時,定時器不會恢復
這是AgeCalculation.java
package com.example.iqtest;
public class AgeCalculation extends Activity implements OnTouchListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_age_calculation);
mDate = (EditText) findViewById(R.id.edDate);
age = (EditText) findViewById(R.id.edAge);
calage = (Button) findViewById(R.id.btnCalculateAge);
go = (Button) findViewById(R.id.button1);
category=(EditText) findViewById(R.id.category);
final Calendar c = Calendar.getInstance();
Year = c.get(Calendar.YEAR);
Month = c.get(Calendar.MONTH);
Day = c.get(Calendar.DAY_OF_MONTH);
mDate.setOnTouchListener(this);
calage.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
Calendar now= Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.set(Year, Month, Day);
int years = now.get(Calendar.YEAR) - calendar.get(Calendar.YEAR);
int months = now.get(Calendar.MONTH) - calendar.get(Calendar.MONTH);
int days = now.get(Calendar.DAY_OF_MONTH) - calendar.get(Calendar.DAY_OF_MONTH);
if (days < 0){
months --;
days +=now.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if (months < 0){
years --;
months +=12;
}
String yearsold = years+" YEARS "+months+ " MONTHS "+days+" DAYS"+" "+"OLD";
age.setText(yearsold);
if(years>=0 && years<=10)
{
opt=0;
}else if(years>=11 && years<=15)
{
opt=1;
}else if(years>=16 && years<=30)
{
opt=2;
}
else if(years>=31 && years<=50)
{
opt=3;
}else if(years>=51 && years<=120)
{
opt=4;
}else
{
opt=5;
}
switch (opt) {
case 0:
start=0;
end=21;
indtime=10;
temp=" KIDS";
timetotake=480;
break;
case 1:
start=21;
end=38;
indtime=11;
temp=" CHILDREN";
timetotake=480;
break;
case 2:
start=38;
end=56;
indtime=12;
temp="TEENAGERS";
timetotake=540;
break;
case 3:
start=56;
end=76;
indtime=11;
temp="ADULTS";
timetotake=540;
break;
case 4:
start=76;
end=99;
indtime=13;
temp="ELDERS";
timetotake=600;
break;
case 5:
temp="ALIENS!!";
break;
default:
temp="ALIENS!!";
break;
}
category.setText(temp);
go.setText("GO"+" "+"TO"+" "+temp+" "+"IQTEST");
}
});
go = (Button) findViewById(R.id.buttongo);
go.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent(AgeCalculation.this, QuizActivity.class);
startActivity(intent);
}
});
}
@Override
protected Dialog onCreateDialog(int id)
{
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(
this, DateSetListener, Year, Month, Day);
}
return null;
}
private DatePickerDialog.OnDateSetListener DateSetListener =
new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Year = year;
Month = monthOfYear;
Day = dayOfMonth;
String sdate = lp(Day + "", "0", 2)+ " " + montharray[Month] + " " + Year;
mDate.setText(sdate);
}
};
private String lp(String sc, String sp, int len) {
String sr = sc;
for (int i = sr.length(); i < len; i++) {
sr = sp + sr;
}
return new String(sr);
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
showDialog(DATE_DIALOG_ID);
return false;
}
}
這是QuizActivity.java
package com.example.iqtest;
public class QuizActivity extends Activity {
List<Question> quesList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
timerValue = (TextView) findViewById(R.id.textView2);
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
pause = (Button) findViewById(R.id.pause);
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
pause.setVisibility(View.VISIBLE);
Intent i = new Intent(QuizActivity.this,Timeplease.class);
startActivity(i);
}
});
agecal = (Button) findViewById(R.id.buttoncalage);
agecal.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent i = new Intent(QuizActivity.this,AgeCalculation.class);
startActivity(i);
finish();
}
});
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(start);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
rdd=(RadioButton)findViewById(R.id.radio3);
rde=(RadioButton)findViewById(R.id.radio4);
butNext=(Button)findViewById(R.id.button1);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
if(currentQ.getanswer().equals(answer.getText()))
{
score++;
}
if(start<end){
currentQ=quesList.get(start);
setQuestionView();
}else{
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score);
b.putInt("time",secs);
//Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getquestion());
rda.setText(currentQ.getopta());
rdb.setText(currentQ.getoptb());
rdc.setText(currentQ.getoptc());
rdd.setText(currentQ.getoptd());
rde.setText(currentQ.getopte());
start++;
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
secs = (int) (updatedTime/1000);
int mins = secs/60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":" + String.format("%02d", secs) + ":"+ String.format("%03d", milliseconds));customHandler.postDelayed(this, 0);
}
};
}
這是Timeplease.java
package com.example.iqtest;
public class Timeplease extends Activity {
Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timeplease);
addListenerOnButton();
}
public void addListenerOnButton()
{
back = (Button) findViewById(R.id.buttonback);
back.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent i = new Intent(Timeplease.this,QuizActivity.class);
finish();
}
});
}
}
試試我的答案!!!!!!! – Hardik