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 ...
随机推荐
- Selenium(一)---Selenium的安装和使用
一.前言 最近在帮一个老师爬取网页内容,发现网页是动态加载的,为了拿到全部的网页数据,这里使用到了Selenium.Selenium 是一个用于Web应用程序测试的工具,它可以模拟真实浏览器,支持多种 ...
- MQTT--笔记
一.MQTT协议基本介绍 1.1.MQTT是什么? MQTT,全称为Message Queue Telemetry Transport.在1999年,由IBM的Andy Stanford-Clark和 ...
- thinkphp 系统流程
用户URL请求 调用应用入口文件(通常是网站的index.php) 载入框架入口文件(ThinkPHP.php) 记录初始运行时间和内存开销 系统常量判断及定义 载入框架引导类(Think\Think ...
- 获取硬件信息的delphi源码CPUID、操作系统、Mac物理地址、计算机名称、IP地址、用户名
{-----------------------------------------------------------------------------作者:sushengmiyan 2013.0 ...
- redis 本地连接可以 远程连接不上问题
1.所连主机防火墙关一下. 1:查看防火状态 systemctl status firewalld service status iptables 2:暂时关闭防火墙 systemctl stop ...
- 第六篇:fastJson常用方法总结
1.了解json json就是一串字符串 只不过元素会使用特定的符号标注. {} 双括号表示对象 [] 中括号表示数组 "" 双引号内是属性或值 : 冒号表示后者是前者的值(这个值 ...
- 豌豆荚Redis集群方案:Codis
Codis简介 Codis是一个分布式Redis解决方案,对于上层的应用来说,连接到CodisProxy和连接原生的RedisServer没有明显的区别(不支持的命令列表),上层应用可以像使用单机的R ...
- Python操作三大主流数据库✍✍✍
Python操作三大主流数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库, ...
- 菲波那切数列(Fibonacci Number)
什么是菲波那切数列?自己google一下,面试题里面经常遇到,考试递归算法用的. 在菲波那切数列中用递归不太好.第三种算法最好. 第一 递归算法最差了,不想说.测试一下,当N=6000时,半天出不来数 ...
- C++函数或者命名空间前面加::
命名空间和函数前面加上:: 经常看到命名空间前就只有:: 比如 ::test;这种代表是全局的test 比如 ::CreateDirectory(..),代表使用系统API也就是全局的 避免使用到 ...