您的位置:首页 > 移动开发 > Android开发

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

2014-02-12 18:14 190 查看
最近一直在头疼怎样在修改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);
实现的效果如下:



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



网上收到的一些方法有如下,自己是小菜鸟一枚,看了好久还是没有解决掉这个问题,希望有大神能够帮助一下...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: