版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第Android实现简易秒表功能本文实例为大家分享了Android实现秒表功能的具体代码,供大家参考,具体内容如下
今天为了给师弟们讲安卓,花了10分钟写了一个简易的秒表app,现贴出代码,供各位刚入门以及还未入门的同学们参考
第一步:布局activity_main.xml:
RelativeLayoutxmlns:android="/apk/res/android"
xmlns:tools="/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
第二步:实现秒表功能
packagecom.example.second;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.app.Activity;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.TextView;
publicclassMainActivityextendsActivity{
privateTextViewmint;
privateTextViewsec;
privateButtonstart;
privateButtonreset;
privatelongtimeusedinsec;
privatebooleanisstop=false;
privateHandlermHandler=newHandler(){
/*
*editbyyuanjingchao2014-08-0419:10
*/
@Override
publicvoidhandleMessage(Messagemsg){
//TODOAuto-generatedmethodstub
super.handleMessage(msg);
switch(msg.what){
case1:
//添加更新ui的代码
if(!isstop){
updateView();
mHandler.sendEmptyMessageDelayed(1,1000);
}
break;
case0:
break;
}
}
};
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
privatevoidinitViews(){
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(newOnClickListener(){
@Override
publicvoidonClick(Viewarg0){
//TODOAuto-generatedmethodstub
mint.setText("00");
sec.setText("00");
start.setText("start");
timeusedinsec=0;
isstop=true;
}
});
start.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewarg0){
//TODOAuto-generatedmethodstub
mHandler.removeMessages(1);
Stringaaa=start.getText().toString();
if(aaa.equals("start")){
mHandler.sendEmptyMessage(1);
isstop=false;
start.setText("pause");
}else{
mHandler.sendEmptyMessage(0);
isstop=true;
start.setText("start");
}
}
});
}
privatevoidupdateView(){
timeusedinsec+=1;
intminute=(int)(timeusedinsec/60)%60;
intsecond=(int)(timeusedinsec%60);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 北芯生命深度报告:心血管精准介入国产龙头
- 兰坪交警考试题型及答案
- 黔西南布依族苗族自治州安龙县2025-2026学年第二学期四年级语文期中考试卷(部编版含答案)
- 2026年工时休假制度落实考试试卷及答案
- 2026年高校辅导员面试思政问答试题及答案
- 2026年专利独家转让合同(1篇)
- 2026年铁路业绩合同(1篇)
- 互联网技术领域信息安全保障保证承诺书4篇
- 健康生活模式标杆小区承诺函3篇范文
- 线上运营诚信保障承诺函3篇
- DZ∕T 0171-2017 大比例尺重力勘查规范(正式版)
- 《网络综合布线技术案例教程》教学课件-第4章-配线子系统的设计与施工
- 2024年濮阳职业技术学院高职单招(英语/数学/语文)笔试历年参考题库含答案解析
- 木雕手工坊项目计划书
- (完整word版)中医病证诊断疗效标准
- 初中语文八年级下册第二单元作业设计 科技之光《大自然的语言》 《阿西莫夫短文两篇》《大雁归来》 《时间的脚印》 单元作业设计
- 人教版道德与法治五年级下册全册课件【完整版】
- 城镇污水处理工艺比选及运行效果分析
- 生产过程控制程序
- 集团公司财务管理制度(全套)
- GB/T 23549-2021丙环唑乳油
评论
0/150
提交评论