无法使用onTouchListener制作秒表

时间:2017-01-28 05:30:01

标签: java android button sharedpreferences ontouchlistener

所以我在这里使用当前的代码,但是没有工作。

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);   

        mChronometer = new Chronometer(this);
        Timercount = (TextView)findViewById(R.id.timer_count);
        touch = (Button)findViewById(R.id.btn_touch);
        touch.setOnTouchListener(this);
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
        if(v==touch){
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                 //TOUCH

                v.setOnTouchListener(mStartListener);

                return true;
            case MotionEvent.ACTION_UP:
                // RELEASED

                v.setOnTouchListener(mStopListener);

                return true;
              }
          }
          return false;

       }

这是格式化时间

View.OnTouchListener mStartListener = new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {

    long elapsedMillis;

             int millis  = (int) (elapsedMillis % 1000);
             int seconds = (int) (elapsedMillis / 1000) % 60;
             int minutes = (int) ((elapsedMillis / (1000*60)) % 60);
             int hours   = (int) ((elapsedMillis / (1000*60*60)) % 24);
             StringBuilder sb = new StringBuilder();
             sb.append((char)('0' + hours / 10))
               .append((char)('0' + hours % 10)).append(":")                 
               .append((char)('0' + minutes / 10))
               .append((char)('0' + minutes % 10)).append(":")
               .append((char)('0' + seconds / 10))
               .append((char)('0' + seconds % 10)).append(".")
               .append((char)('0' + '0' + '0' + millis / 1));

             Timercount.setText(sb);             

         //I was using chronometer before but not working very well, it just count M:SS and random the time like "8:20"
             //mChronometer.setBase(hours + minutes + seconds + millis);
            //mChronometer.start();

       return false;
       }
   };   

我想让它显示为

  

00:00:00.000

所以它将从1毫秒计算到999毫秒,但它在模拟器中显示如下

enter image description here

    View.OnTouchListener mStopListener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

           //I purpose to saved the time when it stop, like using sharedpreferences or something like that

            return false;
        }
  };     

任何人都可以帮我修改代码吗?我的代码没有用,我很困惑下一步做什么

0 个答案:

没有答案