首页 > 其他分享 >3.13号今日总结

3.13号今日总结

时间:2023-03-13 20:34:01浏览次数:35  
标签:总结 24 findViewById TextClock 3.13 chronometer 今日 btn id

1.TextClock(文本时钟)

TextClock是在Android 4.2(API 17)后推出的用来替代DigitalClock的一个控件!
TextClock可以以字符串格式显示当前的日期和时间,因此推荐在Android 4.2以后使用TextClock。
这个控件推荐在24进制的android系统中使用,TextClock提供了两种不同的格式, 一种是在24进制中显示时间和日期,另一种是在12进制中显示时间和日期。大部分人喜欢默认的设置。

可以通过调用:TextClock提供的is24HourModeEnabled()方法来查看,系统是否在使用24进制时间显示! 在24进制模式中:

  • 如果没获取时间,首先通过getFormat24Hour()返回值;
  • 获取失败则通过getFormat12Hour()获取返回值;
  • 以上都获取失败则使用默认;

另外他给我们提供了下面这些方法,对应的还有get方法:

Attribute NameRelated MethodDescription
android:format12Hour setFormat12Hour(CharSequence) 设置12时制的格式
android:format24Hour setFormat24Hour(CharSequence) 设置24时制的格式
android:timeZone setTimeZone(String) 设置时区

其实更多的时间我们是花在时间形式定义上,就是里面这个CharSequence! 这里提供下常用的写法以及结果:

<TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="MM/dd/yy h:mmaa"/>
    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="MMM dd, yyyy h:mmaa"/>
    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="MMMM dd, yyyy h:mmaa"/>
    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="E, MMMM dd, yyyy h:mmaa"/>
    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="EEEE, MMMM dd, yyyy h:mmaa"/>
    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:format12Hour="Noteworthy day: 'M/d/yy"/>

运行结果:

PS:另外minsdk 要大于或者等于17哦!


2.AnalogClock(模拟时钟)

就是下图这种:

官网中我们可以看到这样三个属性:

依次是:表背景,表时针,分时针的图片,我们可以自行定制:

示例代码如下:

<AnalogClock
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:dial="@mipmap/ic_c_bg"
        android:hand_hour="@mipmap/zhen_shi"
        android:hand_minute="@mipmap/zhen_fen" />

运行结果:


3.Chronometer(计时器)

如题,就是一个简单的计时器,我们直接上使用示例吧:

使用示例:

实现代码:

布局代码:

 

<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:orientation="vertical"
    tools:context=".MainActivity">


    <Chronometer
        android:id="@+id/chronometer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#ff0000"
        android:textSize="60dip" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnStart"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="开始记时" />

        <Button
            android:id="@+id/btnStop"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止记时" />

        <Button
            android:id="@+id/btnReset"
            android:layout_width="fill_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>

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener,Chronometer.OnChronometerTickListener{

    private Chronometer chronometer;
    private Button btn_start,btn_stop,btn_base,btn_format;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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 v) {
        switch (v.getId()){
            case R.id.btnStart:
                chronometer.start();// 开始计时
                break;
            case R.id.btnStop:
                chronometer.stop();// 停止计时
                break;
            case R.id.btnReset:
                chronometer.setBase(SystemClock.elapsedRealtime());// 复位
                break;
            case R.id.btn_format:
                chronometer.setFormat("Time:%s");// 更改时间显示格式
                break;
        }
    }

    @Override
    public void onChronometerTick(Chronometer chronometer) {
        String time = chronometer.getText().toString();
        if(time.equals("00:00")){
            Toast.makeText(MainActivity.this,"时间到了~",Toast.LENGTH_SHORT).show();
        }
    }
}

运行截图:

标签:总结,24,findViewById,TextClock,3.13,chronometer,今日,btn,id
From: https://www.cnblogs.com/psh888/p/17212765.html

相关文章

  • java基础-总结案例
    利用java基础,实现如图所示的功能案例涉及要点:#程序基本概念变量数据类型#流程控制顺序选择循环......
  • 2023/3/13结对总结
    设计思路:1.创建两个表<1>第一个:存储所有车站信息:(1)车站唯一标识:id(2)线路号:number(3)车站名:station(4)车站所在线路标识号:characteristic<2>第二个:所有联通车站节点数......
  • 3月10日课后总结
    3/10课后总结random模块importrandomprint(random.random())#产生一个0-1之间的随机数注意会是小数print(random.uniform(1,5))#产生1-5之间的随机数,会是小......
  • 2023/3/13结对总结
    设计思路:1.创建两个表 <1>第一个:存储所有车站信息: (1)车站唯一标识: (2)线路号:number (3)车站名:station (4)车站所在线路标识号:characte......
  • 前端学习案例6-数组方法得总结和运用6
     ......
  • 图神经网络的数学原理总结
    图深度学习(GraphDeepLearning)多年来一直在加速发展。许多现实生活问题使GDL成为万能工具:在社交媒体、药物发现、芯片植入、预测、生物信息学等方面都显示出了很大的前......
  • c-方法总结版
    哦吼,记住了就无敌了1.运算符顺序C语言的运算符包括单目运算符、双目运算符、三目运算符,优先级如下:第1优先级:各种括号,如()、[]等、成员运算符.;第2优先级:所有单目运......
  • OPenGL 学习笔记之 VBO VAO EBO 概念和使用方法总结
    目录一.基本概念:二.理解缓冲对象glVertex函数顶点数组(VertexArray)三.VBO(VertexBufferObject)顶点缓冲区对象大体流程理解:Qt中使用QOpenGLWidget的VBO例......
  • Qz学算法-数据结构篇(排序算法--基数、总结)
    基数排序1.基本介绍基数排序(radixsort)属于“分配式排序”(distributionsort),又称“桶子法”(bucketsor)或binsort,顾名思义,它是通过键值的各个位的值,将安排序的元素分......
  • 实验2.2——VAO,VBO的总结
    这俩个O,前面搞了这么多链接,已经迷糊了,捋一捋吧。事物的发展都是一个过程,所以如果能按着这个技术发展的过程来学习,应该就能捋顺了。简单的说,如果能亲身经历一遍这个发展过......