zoukankan      html  css  js  c++  java
  • 安卓学习22(Date & Time组件)

    1、学习进度条:

    2、目标任务:

    学习Android——Date & Time组件

    3、预计时间:

    2天

    4、完成情况:

    (1)TextClock(文本时钟):两种不同的格式, 一种是在24进制中显示时间和日期,另一种是在12进制中显示时间和日期

    方法:

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

    布局代码:

    <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"/>

    (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

    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();
            }
        }
    }

    5、遇到问题:

  • 相关阅读:
    __init__ 构造行数的用法
    Android SDK下载安装及配置教程
    每条用例执行10次
    Python Json模块中dumps、loads、dump、load函数介绍
    Python接口测试实战2
    linux 下添加环境变量 和刷新
    mysql 练习题笔记
    http请求脚本排错指南
    docker命令及其常用事项
    anaconda 环境
  • 原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/14904545.html
Copyright © 2011-2022 走看看