您当前的位置: 首页 >  ui

Kevin-Dev

暂无认证

  • 2浏览

    0关注

    544博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Android -- UI 开发】TextClock 的基本使用

Kevin-Dev 发布时间:2022-06-11 10:28:43 ,浏览量:2

一、前言

TextClock 可以将当前日期和/或时间显示为格式化字符串。

二、效果图

在这里插入图片描述

三、简单使用



    

    

注意事项: TextClock 中使用了 DateFormat 来格式化时间,格式是根据系统的语言来变化的,你系统语言是中文就是中文的格式,英文就是英文。 还有就是中文的星期的叫法有两种,既:星期一、周一。

  • EEEE 代表使用“星期一”这样的格式;
  • E、EE或EEE都是代表“周一”,部分机型有所不同。
四、实战

4.1 效果图 在这里插入图片描述 4.2 布局文件



    
    
    
    
 
    
    
    

4.3 Java 代码

public class TextClockActivity extends AppCompatActivity {
    private TextClock tc_timeText_12,tc_dateText_12,tc_timeText_24,tc_dateText_24;
    private TextView tv_12hour,tv_24hour,tv_is24HourModeEnabled;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_textview_textclock);//加载布局文件
        initView();
    }
    private void initView() {
        tv_is24HourModeEnabled = findViewById(R.id.tv_is24HourModeEnabled);
 
        tc_timeText_12 = findViewById(R.id.tc_timeText_12);
        tc_dateText_12 = findViewById(R.id.tc_dateText_12);
        tv_12hour = findViewById(R.id.tv_12hour);
        //setTimeZone使用(UTC-7)无效,
        //原因:源码未对UTC+(-)进行处理,下面有具体的源码分析
        tc_timeText_12.setTimeZone("America/Los_Angeles");//有效
        tc_dateText_12.setTimeZone("America/Los_Angeles");//有效
        //tc_timeText_12.setTimeZone("GMT+7:00");//有效
        //tc_dateText_12.setTimeZone("GMT+7:00");//有效
        tc_dateText_12.setFormat24Hour("HH:mm");
        tc_dateText_12.setFormat12Hour("yyyy/MM/dd E");
        // EEEE:星期五 ;E/EE/EEE:周五;a:上午/下午
        tc_dateText_12.setFormat24Hour("yyyy年MM月dd日 EEEE aa HH:mm:ss");
        String format12 = "\n12小时模式格式:"+tc_timeText_12.getFormat12Hour();
        format12 = format12+"\n24小时模式格式:"+tc_timeText_12.getFormat24Hour();
        format12 = format12+"\n时区:"+tc_timeText_12.getTimeZone();
        tv_12hour.setText("Format:"+format12);
 
        tc_timeText_24 = findViewById(R.id.tc_timeText_24);
        tc_dateText_24 = findViewById(R.id.tc_dateText_24);
        tv_24hour = findViewById(R.id.tv_24hour);
        String format = "\n24小时模式格式:"+tc_timeText_24.getFormat24Hour();
        format = format+"\n12小时模式格式:"+tc_timeText_24.getFormat12Hour();
        format = format+"\n时区:"+tc_timeText_24.getTimeZone();
        String timeZome =TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT);
        format = format+"\n时区:"+timeZome;
        tv_24hour.setText("Format:"+format);
        String is24HourMode = String.format("系统当前是否使用 24 小时模式:%s。", tc_dateText_24.is24HourModeEnabled());
        tv_is24HourModeEnabled.setText(is24HourMode);
    }
}
关注
打赏
1658837700
查看更多评论
立即登录/注册

微信扫码登录

0.0472s