編輯:關於Android編程
本文承接,Android 開發第五彈:簡易時鐘(鬧鐘) 和 Android 開發第六彈:簡易時鐘(計時器),這一部分是關於秒表的。
同樣是新建一個類(StopWatchView)並擴展自LinearLayout,並將其用作布局。
< / LinearLayout>
< / ListView>
同樣是一開始要定義好的這些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;
}
};
};
好吧,這次是真的結束了。
同樣的,需要代碼就直接評論留郵箱吧。代碼會繼續更新的,注釋也會繼續更新……
碰到幾個問題,記錄一下,方便其他網友查詢 我是win7,64位系統,JAVA以前裝過,所以沒有安裝JDK直接下載Android Studio進行安裝,結果提示沒有安裝
我們知道,在AndroidOS中,提供了五中數據存儲方式,分別是:ContentProvider存儲、文件存儲、SharedPreference存儲、SQLite數據庫存
華為麥芒5和小米5哪個好?它們之間有什麼區別?下面小編將帶來華為麥芒5和小米5對比評測,大家一起來看看吧!華為麥芒5和小米5對比評測: 華為麥芒5介紹:
1 引入如何高效地實現以下界面?public class User { private String name; private int score;