網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android實現(xiàn)秒表功能的具體代碼,供大家參考,具體內(nèi)容如下
今天為了給師弟們講安卓,花了10分鐘寫了一個簡易的秒表app,現(xiàn)貼出代碼,供各位剛?cè)腴T以及還未入門的同學(xué)們參考
第一步:布局activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:paddingBottom="@dimen/activity_vertical_margin" ? ? android:paddingLeft="@dimen/activity_horizontal_margin" ? ? android:paddingRight="@dimen/activity_horizontal_margin" ? ? android:paddingTop="@dimen/activity_vertical_margin" ? ? tools:context=".MainActivity" > ? ? ? <RelativeLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_centerInParent="true" > ? ? ? ? ? <LinearLayout ? ? ? ? ? ? android:id="@+id/top" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? ? ? android:orientation="horizontal" > ? ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/mint" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="00" ? ? ? ? ? ? ? ? android:textSize="30dp" /> ? ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text=":" ? ? ? ? ? ? ? ? android:textSize="30dp" /> ? ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/sec" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="00" ? ? ? ? ? ? ? ? android:textSize="30dp" /> ? ? ? ? </LinearLayout> ? ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_below="@+id/top" ? ? ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? ? ? android:orientation="horizontal" > ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/start" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="start" /> ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/reset" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="reset" /> ? ? ? ? </LinearLayout> ? ? </RelativeLayout> ? </RelativeLayout>
第二步:實現(xiàn)秒表功能
package com.example.second;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
? ? private TextView mint;
? ? private TextView sec;
? ? private Button start;
? ? private Button reset;
? ? private long timeusedinsec;
? ? private boolean isstop = false;
? ? private Handler mHandler = new Handler() {
? ? ? ? /*
? ? ? ? ?* edit by yuanjingchao 2014-08-04 19:10
? ? ? ? ?*/
? ? ? ? @Override
? ? ? ? public void handleMessage(Message msg) {
? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? super.handleMessage(msg);
? ? ? ? ? ? switch (msg.what) {
? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? // 添加更新ui的代碼
? ? ? ? ? ? ? ? if (!isstop) {
? ? ? ? ? ? ? ? ? ? updateView();
? ? ? ? ? ? ? ? ? ? mHandler.sendEmptyMessageDelayed(1, 1000);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
?
? ? };
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? initViews();
? ? }
? ? private void initViews() {
? ? ? ? mint = (TextView) findViewById(R.id.mint);
? ? ? ? sec = (TextView) findViewById(R.id.sec);
? ? ? ? reset = (Button) findViewById(R.id.reset);
? ? ? ? start = (Button) findViewById(R.id.start);
? ? ? ? reset.setOnClickListener(new OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View arg0) {
? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ??
? ? ? ? ? ? ? ? mint.setText("00");
? ? ? ? ? ? ? ? sec.setText("00");
? ? ? ? ? ? ? ? start.setText("start");
? ? ? ? ? ? ? ? timeusedinsec=0;
? ? ? ? ? ? ? ? isstop=true;
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? start.setOnClickListener(new OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View arg0) {
? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ? ? mHandler.removeMessages(1);
? ? ? ? ? ? ? ? String aaa=start.getText().toString();
? ? ? ? ? ? ? ? if(aaa.equals("start")){
? ? ? ? ? ? ? ? ? ? mHandler.sendEmptyMessage(1);
? ? ? ? ? ? ? ? ? ? isstop = false;
? ? ? ? ? ? ? ? ? ? start.setText("pause");
? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? mHandler.sendEmptyMessage(0);
? ? ? ? ? ? ? ? ? ? isstop = true;
? ? ? ? ? ? ? ? ? ? start.setText("start");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private void updateView() {
? ? ? ? timeusedinsec += 1;
? ? ? ? int minute = (int) (timeusedinsec / 60)%60;
? ? ? ? int second = (int) (timeusedinsec % 60);
? ? ? ? if (minute < 10)
? ? ? ? ? ? mint.setText("0" + minute);
? ? ? ? else
? ? ? ? ? ? mint.setText("" + minute);
? ? ? ? if (second < 10)
? ? ? ? ? ? sec.setText("0" + second);
? ? ? ? else
? ? ? ? ? ? sec.setText("" + second);
? ? }
}
原文鏈接:https://blog.csdn.net/huanongjingchao/article/details/38374233
相關(guān)推薦
- 2022-12-06 React.memo?和?useMemo?的使用問題小結(jié)_React
- 2022-03-20 C語言輸出任意邊長的菱形(用c語言輸出一個菱形)
- 2022-03-16 C++冒泡排序與選擇排序詳解_C 語言
- 2022-08-26 Python中True(真)和False(假)判斷詳解_python
- 2022-09-13 C語言創(chuàng)建數(shù)組實現(xiàn)函數(shù)init,empty,reverse_C 語言
- 2022-04-21 C語言中const和指針的秘密你知道嗎_C 語言
- 2022-07-10 elementUI去掉el-card內(nèi)部padding
- 2022-12-14 Python中shape[0]、shape[1]和shape[-1]分別的意思詳解(附代碼)_pyt
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支