Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 開發第七彈:簡易時鐘(秒表)

Android 開發第七彈:簡易時鐘(秒表)

編輯:關於Android編程

本文承接,Android 開發第五彈:簡易時鐘(鬧鐘) 和 Android 開發第六彈:簡易時鐘(計時器),這一部分是關於秒表的。

這裡寫圖片描述

布局

同樣是新建一個類(StopWatchView)並擴展自LinearLayout,並將其用作布局。



    

    

    

    

    

    

    

    

    < / LinearLayout>

    

    < / ListView>

    

StopWatchView

同樣是一開始要定義好的這些balabala的東西:

    private int tenMSecs = 0;
    private Timer timer = new Timer();
    private TimerTask timerTask = null;
    private TimerTask showTimeTask = null;

    private TextView tvHour, tvMinute, tvSecond, tvMSecond;
    private Button btnSWStart, btnSWResume, btnSWReset, btnSWPause, btnSWRecord;
    private ListView lvWatchTimeList;
    private ArrayAdapter adapter;

    private static final int MSG_WHAT_SHOW_TIME = 1;

    public StopWatchView(Context context) {
        super(context);
    }

    public StopWatchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StopWatchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

看看這些注釋,發現和前面兩篇的也沒有區別啊,我就不廢話直接上代碼好了。

 @Override
    protected void onFinishInflate(){
        super.onFinishInflate();

        // 為每個相應的TextView控件設置成0
        tvHour = (TextView)findViewById(R.id.tvHour);
        tvHour.setText(0);
        tvMinute = (TextView)findViewById(R.id.tvMinute);
        tvMinute.setText(0);
        tvSecond = (TextView)findViewById(R.id.tvSecond);
        tvSecond.setText(0);
        tvMSecond = (TextView)findViewById(R.id.tvMSceond);
        tvMSecond.setText(0);

        // 為每個Button設置監聽事件
        btnSWRecord = (Button)findViewById(R.id.btnSWRecord);
        btnSWRecord.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.insert(String.format(%d:%d:%d.%d,
                        tenMSecs/100/60/60,
                        tenMSecs/100/60%60,
                        tenMSecs/100%60,
                        tenMSecs%100),
                        0);
            }
        });

        btnSWPause = (Button)findViewById(R.id.btnSWPause);
        btnSWPause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 停止
                stopTimer();

                btnSWPause.setVisibility(View.GONE);
                btnSWResume.setVisibility(View.VISIBLE);
                btnSWReset.setVisibility(View.VISIBLE);
                btnSWRecord.setVisibility(View.GONE);
            }
        });

        btnSWReset = (Button)findViewById(R.id.btnSWReset);
        btnSWReset.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 停止
                stopTimer();
                tenMSecs = 0;
                adapter.clear();

                btnSWStart.setVisibility(View.VISIBLE);
                btnSWPause.setVisibility(View.GONE);
                btnSWReset.setVisibility(View.GONE);
                btnSWRecord.setVisibility(View.GONE);
                btnSWResume.setVisibility(View.GONE);
            }
        });

        btnSWResume = (Button)findViewById(R.id.btnSWResume);
        btnSWResume.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 開始
                startTimer();

                btnSWResume.setVisibility(View.GONE);
                btnSWReset.setVisibility(View.GONE);
                btnSWRecord.setVisibility(View.VISIBLE);
                btnSWPause.setVisibility(View.VISIBLE);
            }
        });

        btnSWStart = (Button)findViewById(R.id.btnSWStart);
        btnSWStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 開始
                startTimer();

                btnSWStart.setVisibility(View.GONE);
                btnSWPause.setVisibility(View.VISIBLE);
                btnSWRecord.setVisibility(View.VISIBLE);
            }
        });

        btnSWRecord.setVisibility(View.GONE);
        btnSWPause.setVisibility(View.GONE);
        btnSWReset.setVisibility(View.GONE);
        btnSWResume.setVisibility(View.GONE);

        // 將適配器添加到列表
        lvWatchTimeList = (ListView)findViewById(R.id.lvWatchTimeList);
        adapter = new ArrayAdapter(getContext(),
                android.R.layout.simple_list_item_1);
        lvWatchTimeList.setAdapter(adapter);

        // 向Handler發送消息
        showTimeTask = new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(MSG_WHAT_SHOW_TIME);
            }
        };
        // 開始計時
        timer.schedule(showTimeTask,200,200);
    }

    // 開始
    private void startTimer(){
        if(timerTask == null){
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    tenMSecs++;
                }
            };
            timer.schedule(timerTask,10,10);
        }
    }

    // 結束
    private void stopTimer(){
        if(timerTask != null){
            timerTask.cancel();
            timerTask = null;
        }
    }

    // 取消計時
    public void onDestory(){
        timer.cancel();
    }

    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what){
                // 如果消息匹配,則將相應時間計算後顯示在相應TextView上
                case MSG_WHAT_SHOW_TIME:
                    tvHour.setText(tenMSecs/100/60/60+);
                    tvMinute.setText(tenMSecs/100/60%60+);
                    tvSecond.setText(tenMSecs/100%60+);
                    tvMSecond.setText(tenMSecs%100+);
                    break;
                default:
                    break;
            }
        };
    };

結束

好吧,這次是真的結束了。

同樣的,需要代碼就直接評論留郵箱吧。代碼會繼續更新的,注釋也會繼續更新……

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved