Android Setting下修改时间与日期格式的问题???

          最近一直在头疼怎样在修改Settings-----Date&Time---Choose data format 后通知更新桌面时钟 AppWidget的日期格式跟着Setting的改变而改变...

想实现的效果就是如下:

原先日期格式是这样

当点击修改Settings-----Date&Time---Choose data format,

想动态实现,就有点不知道怎么解决了,

这个布局其实是一个TextClock 

          <TextClock
            android:id="@+id/date"
            style="@style/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:format12Hour="@string/abbrev_wday_month_day_no_year"
            android:format24Hour="@string/abbrev_wday_month_day_no_year"
            android:gravity="center"
            android:textColor="@color/clock_white" />


 android:format12Hour="@string/abbrev_wday_month_day_no_year"这个用到的一个布局如下

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
     
-->

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <!-- 字符串匹配的锁定屏幕显示日期的格式. MM/dd/yyy-->
    <string name="abbrev_wday_month_day_no_year">dd-MM-yyyy</string>
    <!-- Format for describing the date, for accessibility. -->
    <string name="full_wday_month_day_no_year">EEEE, MMMM d</string>
    <!-- Default clock style. -->
    <string name="default_clock_style">digital</string>
</resources>

第一种解决方法,属于治标不治本的方法,这种方法是静态修改 abbrev_wday_month_day_no_year的值为想要的日期格式,

比如修改为:<string name="abbrev_wday_month_day_no_year">EE-d-MMM-yyyy</string>

这个有点不靠谱,或者修改为如下格式

日期格式的值的方法有如下:

  <string-array name="date_format_values" translatable="false">
        <!-- The blank item means to use whatever the locale calls for. -->
        <item></item>
        <item>MM-dd-yyyy</item>
        <item>dd-MM-yyyy</item>
        <item>yyyy-MM-dd</item>
        <item>EE-MMM-d-yyyy</item>
        <item>EE-d-MMM-yyyy</item>
        <item>yyyy-MMM-d-EE</item>
    </string-array>

老大给的建议让用TextView代替TextClock,可是自己水平有限重写的几次都没达到理想的效果。。。。


用Textview简单实现的方法如下:

		/**
		 * 第一步
		 * 获取settings中日期的格式
		 * strTimeFormat的字符串是    yyyy-MM-dd
		 * 
		 * 第二步
		 * SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类。 
		 * 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化。
		 * 根据日期格式显示不同类型的日期
		 * 
		 * ***/ 
		ContentResolver cv = this.getContentResolver();
		String strTimeFormat = android.provider.Settings.System.getString(cv,
				android.provider.Settings.System.DATE_FORMAT);
		System.out.println("选择 Normal (2014-12-31)" + strTimeFormat);

		SimpleDateFormat dateFormatter = new SimpleDateFormat(strTimeFormat);
		time = dateFormatter.format(Calendar.getInstance().getTime());
		textView.setText(time);
实现的效果如下:

日期格式时间跟着设置里面的改变而改变..




网上收到的一些方法有如下,自己是小菜鸟一枚,看了好久还是没有解决掉这个问题,希望有大神能够帮助一下...





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Android

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值