凌云志轩 2015-05-21 04:06 采纳率: 89%
浏览 1806
已采纳

android girdview里面timer没反应

我想写一个计时器,点击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;
}

}



  • 写回答

4条回答 默认 最新

  • JPF1024 2015-05-21 04:51
    关注

    private Timer getTimer(){
    //在这里面去生成你的timer代码,记得一定要仿照单例的模式:如果不存在就创建,存在就不用创建了.
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL