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

來源:互聯網
上載者:User

標籤:android   開發   秒錶   計時器   時鐘   

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

布局

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

<myapplication.nomasp.com.clock.StopWatchView    android : id = "@+id/tabStopWatch"    android : layout_width = "match_parent"    android : layout_height = "match_parent"    android : orientation = "vertical">    <LinearLayout    android : layout_width = "match_parent"    android : layout_height = "wrap_content"    android : orientation = "horizontal">    <TextView    android : id = "@+id/tvHour"    android : layout_width = "0dp"    android : layout_height = "wrap_content"    android : layout_weight = "1"    android : textAppearance = "?android:attr/textAppearanceLarge" / >    <TextView    android : text = ":"    android : layout_width = "wrap_content"    android : layout_height = "wrap_content"    android : textAppearance = "?android:attr/textAppearanceLarge" / >    <TextView    android : id = "@+id/tvMinute"    android : layout_width = "0dp"    android : layout_height = "wrap_content"    android : layout_weight = "1"    android : textAppearance = "?android:attr/textAppearanceLarge" / >    <TextView    android : text = ":"    android : layout_width = "wrap_content"    android : layout_height = "wrap_content"    android : textAppearance = "?android:attr/textAppearanceLarge" / >    <TextView    android : id = "@+id/tvSecond"    android : layout_width = "0dp"    android : layout_height = "wrap_content"    android : layout_weight = "1"    android : textAppearance = "?android:attr/textAppearanceLarge" / >    <TextView    android : text = "."    android : layout_width = "wrap_content"    android : layout_height = "wrap_content"    android : textAppearance = "?android:attr/textAppearanceLarge" / >    <TextView    android : id = "@+id/tvMSceond"    android : layout_width = "0dp"    android : layout_height = "wrap_content"    android : layout_weight = "1"    android : textAppearance = "?android:attr/textAppearanceLarge" / >    < / LinearLayout>    <ListView    android : id = "@+id/lvWatchTimeList"    android : layout_width = "match_parent"    android : layout_height = "0dp"    android : layout_weight = "1">    < / ListView>    <LinearLayout    android : orientation = "horizontal"    android : layout_width = "match_parent"    android : layout_height = "wrap_content">    <Button    android : id = "@+id/btnSWStart"    android : layout_width = "0dp"    android : layout_height = "wrap_content"    android : layout_weight = "1"    android : text = "@string/start" / >    <Button    android : id = "@+id/btnSWPause"    android : layout_width = "0dp"    android : layout_height = "wrap_content"    android : layout_weight = "1"    android : text = "@string/pause" / >    <Button    android : id = "@+id/btnSWResume"    android : layout_width = "0dp"    android : layout_height = "wrap_content"    android : layout_weight = "1"    android : text = "@string/resume" / >    <Button    android : id = "@+id/btnSWRecord"    android : layout_width = "0dp"    android : layout_height = "wrap_content"    android : layout_weight = "1"    android : text = "@string/record" / >    <Button    android : id = "@+id/btnSWReset"    android : layout_width = "0dp"    android : layout_height = "wrap_content"    android : layout_weight = "1"    android : text = "@string/reset" / >    < / LinearLayout></myapplication.nomasp.com.clock.StopWatchView>
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<String> 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<String>(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;            }        };    };
結束

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

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

項目也上傳到Github了,歡迎大家貢獻代碼啊——傳送門 。

著作權聲明:本文為 NoMasp柯於旺 原創文章,未經許可嚴禁轉載!歡迎訪問我的部落格:http://blog.csdn.net/nomasp

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.