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 ...
随机推荐
- LeetCode 31. Next Permutation【Medium】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- java——有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
package java_day10; /* * 有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? */ public class Demo04 { public stat ...
- React和vue的差异和相似地方
React 单向绑定(加插件后,还是可以双向绑定) Vue 双向绑定 组件化 1. React,需要编写render函数, 2. 当React状态的状态state改变是render就会重新被调用, 重 ...
- fuzzy commitment 和fuzzy vault
Alice,这位令人惊异的魔术天才,正表演关于人类意念的神秘技巧.她将在Bob选牌之前猜中Bob将选的牌!注意Alice在一张纸上写出她的预测.Alice很神秘地将那张纸片装入信封中并封上.就在人们吃 ...
- leetcode-212-单词搜索②
题目描述: 第一次提交:(超出时间限制) class Solution: def findWords(self, board: List[List[str]], words: List[str]) - ...
- Java 基础 - 比较方式选择(什么类型用equals()比较,什么类型用==比较)
ref: https://www.cnblogs.com/lori/p/8308671.html 在 java 中进行比较,我们需要根据比较的类型来选择合适的比较方式: 对象域,使用 equals 方 ...
- 【JZOJ6375】华灵[蝶妄想]
description analysis 明显括号序长度是偶数,如果其中一个是奇数,那么只能让这奇数行或列是括号序 对于两个都是偶数,需要分类讨论,假设\(n<m\) 有一种是牺牲掉\(n\ov ...
- php 数据导出到excel 2种带有合并单元格的导出
具体业务层面 可能会有所不同.以下两种方式涉及的合并单元格地方有所不同,不过基本思路是一致的. 第一种是非插件版本.可能更容易理解点,基本思路就是 组装table 然后 读取 输出到excel上.缺点 ...
- BZOJ 1040 (ZJOI 2008) 骑士
题目描述 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里, ...
- Hexo 博客图片添加至图床---腾讯云COS图床使用。
个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 腾讯云官网 登录注册 创建存储桶 进入上面的存 ...