Android基础控件TextClock和Chronometer的使用
1、简介
DigitalClock, TextClock,AnalogClock,Chronometer其中DigitalClock和AnalogClock废弃了!
TextClock是在Android 4.2(API 17)后推出的用来替代DigitalClock的一个控件!
android:format12Hour //设置12时制的格式
android:format24Hour //设置24时制的格式
android:timeZone //设置时区
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="MM/dd/yy h:mmaa"/> android:format12Hour="MMM dd, yyyy h:mmaa"
android:format12Hour="MMMM dd, yyyy h:mmaa"
android:format12Hour="E, MMMM dd, yyyy h:mmaa"
android:format12Hour="EEEE, MMMM dd, yyyy h:mmaa"
android:format12Hour="Noteworthy day: M/d/yy"
Chronometer作为一个计时器。用来记录用户进行某项操作所消耗的时间!
long getBase(); //返回基地的时间,
String getFormat();//返回当前字符串格式
void setBase(long base);//设置时间,计数定时器指定的值
void setFormat(String format);//设置显示的内容
2、简单使用

布局的xml文件:
<LinearLayout 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:gravity="center"
android:orientation="vertical"
tools:context=".LoginActivity">
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="MM/dd/yy h:mmaa"/>
<Chronometer
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/chronometer"
android:gravity="center"
android:textColor="@color/text_color_select"
android:textSize="60dip"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:orientation="horizontal"> <Button
android:id="@+id/btnStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="开始记时" /> <Button
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止记时" /> <Button
android:id="@+id/btnReset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重置" /> <Button
android:id="@+id/btn_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="格式化" />
</LinearLayout> </LinearLayout>
Java文件:
public class LoginActivity extends AppCompatActivity implements View.OnClickListener,Chronometer.OnChronometerTickListener{
private Chronometer chronometer;
private Button btn_start,btn_stop,btn_base,btn_format;
private long recordingTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
}
private void initView() {
chronometer = (Chronometer) findViewById(R.id.chronometer);
btn_start = (Button) findViewById(R.id.btnStart);
btn_stop = (Button) findViewById(R.id.btnStop);
btn_base = (Button) findViewById(R.id.btnReset);
btn_format = (Button) findViewById(R.id.btn_format);
chronometer.setOnChronometerTickListener(this);
btn_start.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_base.setOnClickListener(this);
btn_format.setOnClickListener(this);
}
@Override
public void onClick(View view){
switch (view.getId()){
case R.id.btnStart:
onChronoStar();
break;
case R.id.btnStop:
onChronoStop();
break;
case R.id.btnReset:
onChronoRes();
break;
case R.id.btn_format:
chronometer.setFormat("Time: %s");
break;
}
}
@Override//每秒调用一次
public void onChronometerTick(Chronometer chronometer){
String time = chronometer.getText().toString();
Log.d("time--", time);
}
public void onChronoStar(){
chronometer.setBase(SystemClock.elapsedRealtime()-recordingTime);// 跳过已经记录了的时间
chronometer.start();
}
public void onChronoStop(){
chronometer.stop();
recordingTime = SystemClock.elapsedRealtime()-chronometer.getBase();// 保存这次记录了的时间
}
public void onChronoRes(){
recordingTime = 0;
chronometer.setBase(SystemClock.elapsedRealtime());// 从当前时刻重新计时
}
}
Android基础控件TextClock和Chronometer的使用的更多相关文章
- Android基础控件ListView基础操作
1.简介 基于Android基础控件ListView和自定义BaseAdapter适配器情况下,对ListView的数据删除和添加操作: public boolean add(E e) {//添加数据 ...
- android 基础控件(EditView、SeekBar等)的属性及使用方法
android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...
- 矩阵, 矩阵 , Android基础控件之ImageView
天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...
- android 基础控件 EditText
EditText 简介: EditText 控件继承 TextView ,它有TextView的所有属性和方法,并且自身是可编辑的: extends TextView java.lang.Object ...
- Android 基础控件 TextView
一TextView介绍: TextView是UI最基本的组件,使用TextView可以显示丰富的文本信息.设置添加TextView最常见的方法就是在xml中添加TextView元素,并指定属性.Tex ...
- Android基础控件ProgressBar进度条的使用
1.简介 ProgressBar继承与View类,直接子类有AbsSeekBar和ContentLoadingProgressBar, 其中AbsSeekBar的子类有SeekBar和RatingBa ...
- Android基础控件单选按钮RadioButton和Checkbox复选按钮的使用
1.相关简介 RadioButton需要和RadioGroup结合使用,在RadioGroup设置布局方式! Checkbox是单独使用,本文为了方便放在了RadioGroup中! 2.简单使用 方法 ...
- Android基础控件Button的使用
1.相关属性 Android的按钮有Button和ImageButton(图像按钮),Button extends TextView, ImageButton extends ImageView! a ...
- Android基础控件EditText
1.常用属性 <!--selectAllOnFocus 获得焦点后全选组件内所有文本内容--> <!--inputType 限制输入方式--> <!--singleLin ...
随机推荐
- 天道神诀---DHCP服务(下篇)
DHCP作用域详解 subnet 定义一个作用域 netmask 定义作用域的掩码 range 允许发放的IP范围 option routers 指定网关地址 option domain-nam ...
- 【2018ACM/ICPC网络赛】焦作赛区
A Magic Mirror 题目链接:https://nanti.jisuanke.com/t/31710 题意:输入字符串,如果是“Jessy”就输出“Good Guy!",否则输出“D ...
- Shiro学习笔记1 —— Hello World
1.创建一个Maven工程加载Shiro的jar包 <!-- junit --> <dependency> <groupId>junit</groupId&g ...
- 《人月神话》读书笔记 PB16110698 第七周(~4.19)
每逢读书笔记上交作业时刻,班级blog页面上总能看到<人月神话>相关的读书笔记,本次软工课邓老师推荐的第一篇读书笔记也是写的<人月神话>,算是对它“耳濡目染”了.本周,我终于抽 ...
- markdown 表情包大法
前段时间偶然发现了markdown竟然可以插入表情,而且竟然如此的简单 表情包网站 (有可能是官网):点击跳转 这些东西真的是有点意思啊,容我举个栗子
- AOP与IOC的概念(即spring的核心)
a) IOC:Spring是开源框架,使用框架可以使我们减少工作量,提高工作效率并且它是分层结构,即相对应的层处理对应的业务逻辑,减少代码的耦合度.而spring的核心是IOC控制反转和AOP面向切面 ...
- vue组件参数校验
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- Android Studio 配置快速生成模板代码
前言 Android studio 有提供快速生成模板代码的功能,其实这个功能也可以自定义配置.此篇博客将讲解如何使用此功能 进入Settings 选择 Editor > Live Templa ...
- python读文件判断是否已到EOF
python读文件判断是否已到EOF,也即结尾,一般其它语言都是以EOF直接来判断的,比如 if ( fp.read(chunk_size) == EOF), 但python到结尾后是返回空字符串的, ...
- [Usaco2005 Dec]Cleaning Shifts
[Usaco2005 Dec]Cleaning Shifts 给出n段区间,左右端点分别为\(l_i,r_i\),以及选取这段区间的费用\(c_i\),现在要选出若干个区间,使其完全覆盖区间\([m, ...