【问题标题】:i am not able to saving time taken by user in application我无法节省用户在应用程序中花费的时间
【发布时间】:2015-04-18 13:01:53
【问题描述】:

我正在开发在线考试准备 android 应用程序..现在我想要的是,我想保存用户的每个问题时间并在结果屏幕中显示每个问题时间....如何做到这一点

【问题讨论】:

  • 你真的需要在这里显示一些代码。

标签: java android


【解决方案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
    }
}
}

【讨论】:

    猜你喜欢
    • 2018-01-10
    • 2021-01-22
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多