我想写一个计时器,点击tock_on 00:00:0开始计时,点击tock_off 暂停。再点击开始清零开始计时。
我自己根据秒数转换时间格式字符串的方法写了一个计时器,但是有BUG,网上问了是系统时间调用问题,要换种方法,我用timer重写了一个,点击按钮不起作用。求大神指点,详细点呀,我比较笨
我是在adapter中写的。因为gridview的每个item中都需要一个计时器。
```public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private Context mcontext;
private GridView myGridView;//操作的Gridview
private static boolean tock_flag = false;//tock标记
private static boolean voice_flag = true;//voice标记
//定义计时器参数
private long mlCount = 0;
private long mlTimerUnit = 100;
private Timer timer = null;
private TimerTask task = null;
private Handler handler = null;
private Message msg = null;
private static final String MYTIMER_TAG = "MYTIMER_LOG";
private static final int SETTING_SECOND_ID = Menu.FIRST + 101;
private static final int SETTING_100MILLISECOND_ID = Menu.FIRST + 102;
private int settingTimerUnitFlg = SETTING_100MILLISECOND_ID;
public MyAdapter(GridView gv, Context c) {
this.mcontext = c;
this.myGridView = gv;
}
public int getCount() {
return dev_grp.dev_num;
}
public Object getItem(int position) {
return dev_grp.dev_list[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View grid, final ViewGroup parent) {
grid = LayoutInflater.from(mcontext).inflate(R.layout.activity_dev_tock_control, null);
final ImageButton tock_on = (ImageButton) grid.findViewById(R.id.tock_on);
final ImageButton tock_off = (ImageButton) grid.findViewById(R.id.tock_off);
final ImageButton search_voice = (ImageButton) grid.findViewById(R.id.search_voice);
final TextView tvTime = (TextView)grid.findViewById(R.id.tvTime);
handler = new Handler(){
public void handleMessage(Message msg) {
switch(msg.what) {
case 1:
mlCount++;
int totalSec = 0;
int yushu = 0;
if (SETTING_SECOND_ID == settingTimerUnitFlg) {
// second
totalSec = (int)(mlCount);
} else if (SETTING_100MILLISECOND_ID == settingTimerUnitFlg) {
// 100 millisecond
totalSec = (int)(mlCount / 10);
yushu = (int)(mlCount % 10);
}
// Set time display
int min = (totalSec / 60);
int sec = (totalSec % 60);
try{
if (SETTING_SECOND_ID == settingTimerUnitFlg) {
// second(1000ms)
tvTime.setText(String.format("%1$02d:%2$02d", min, sec));
} else if (SETTING_100MILLISECOND_ID == settingTimerUnitFlg) {
// 100 millisecond
tvTime.setText(String.format("%1$02d:%2$02d:%3$d", min, sec, yushu));
}
} catch(Exception e) {
tvTime.setText("" + min + ":" + sec + ":" + yushu);
e.printStackTrace();
}
break;
default:
break;
}
super.handleMessage(msg);
}
};
tock_on.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if(tock_flag){}
else {
tock_flag = true;
//计时器
if (null != timer) {
task.cancel();
task = null;
timer.cancel(); // Cancel timer
timer.purge();
timer = null;
handler.removeMessages(msg.what);
mlCount = 0;
if (SETTING_SECOND_ID == settingTimerUnitFlg) {
// second
tvTime.setText(R.string.init_time_second);
} else if (SETTING_100MILLISECOND_ID == settingTimerUnitFlg) {
// 100 millisecond
tvTime.setText(R.string.init_time_100millisecond);
}
}
task = new TimerTask() {
public void run() {
if (null == msg) {
msg = new Message();
} else {
msg = Message.obtain();
}
msg.what = 1;
handler.sendMessage(msg);
}
};
timer = new Timer();
timer.schedule(task, mlTimerUnit, mlTimerUnit); // set timer duration
}
}
});
tock_off.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if(tock_flag){
tock_flag = false;
voice_flag = true;
try {
task.cancel();
timer.cancel(); // Cancel timer
timer.purge();
handler.removeMessages(msg.what);
btnStartPause.setImageResource(R.drawable.start);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
return grid;
}
}