我正在研究在线考试准备Android应用程序..现在我想要的是,我想用用户保存每个问题时间并在结果屏幕中显示每个问题时间....如何做到这一点
答案 0 :(得分:1)
public class MainActivity extends Activity implements OnClickListener {
Button one,two,three,four;
int count = 0;
Timer time;
int position = 1;
int oneTime = 0;
HashMap<Integer , Integer> hashCount = new HashMap<Integer, Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button)findViewById(R.id.oneButton1);
two = (Button)findViewById(R.id.twoButton1);
three = (Button)findViewById(R.id.threeButton1);
four = (Button)findViewById(R.id.fourButton1);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
background();
System.out.println(position);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
count = 0;
switch (v.getId()) {
case R.id.oneButton1:
position = 1;
System.out.println(position);
pauseTime(position);
background();
break;
case R.id.twoButton1:
position = 2;
pauseTime(position);
background();
break;
case R.id.threeButton1:
position = 3;
pauseTime(position);
background();
break;
case R.id.fourButton1:
position = 4;
pauseTime(position);
background();
break;
default:
break;
}
}
public void background(){
oneTimeExcute();
time.cancel();
time = new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
count++;
Log.d("Timer", String.valueOf(count));
store();
}
}, 0,1000);
}
public void store(){
hashCount.put(position, count);
}
public void oneTimeExcute(){
if(oneTime == 0){
time = new Timer();
oneTime = 1;
}
}
public void pauseTime(int position){
try {
count = hashCount.get(position);
} catch (Exception e) {
// TODO: handle exception
}
}
}