android Rom修改关于系统时间日期格式的修改
前两天,刚开始接触rom修改,boss给了个小的不能再小的任务,关于android自带时钟时间日期格式的修改,对于android rom的初次接触,导致这些问题的处理花费了大量的精力,在网上查阅来大量资料,发现都是不想要的,对于这次任务无关紧要,所以还是自行研究,总算是将这个问题给解决。
首先,第一步当然是定位了,打开deskClock发现,上面的时间与下面的日期分别是在两个不同的控件中,现在android 5.1.0版本中我发现,对于时间,android使用的是TextClock显示,对于下面的日期, android采用还是TextView显示,原本想,格式化日期,应该只需要改掉格式字串就行,就去寻找关于格式化的xml文件,发现在apps/DeskClock/res/values/donottranslate.xml中的相关代码:
首先,第一步当然是定位了,打开deskClock发现,上面的时间与下面的日期分别是在两个不同的控件中,现在android 5.1.0版本中我发现,对于时间,android使用的是TextClock显示,对于下面的日期, android采用还是TextView显示,原本想,格式化日期,应该只需要改掉格式字串就行,就去寻找关于格式化的xml文件,发现在apps/DeskClock/res/values/donottranslate.xml中的相关代码:
<span style="font-size:18px;"> <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- String matching the lock screen format for displaying the date. -->
<string name="abbrev_wday_month_day_no_year">EEEMMMMd</string>
<!-- Format for describing the date, for accessibility. -->
<string name="full_wday_month_day_no_year">EEEEMMMMd</string>
<!-- Default clock style. -->
<string name="default_clock_style">digital</string>
</resources>
把EEEMMMMd修改成了EEEdMMMMyy结果发现,所说年份出来但是格式根本没有发生任何改变,究其原因,发现此字符串只是提供需要显示年还是月,而字符串的顺序对于时间日期无任何影响。
回到原点,现在就开始定位用到了日期的控件使用到的位子,也就是在apps/DeskClock/src/com/android/deskclock/Utils.java中运用到,源码如下:
</span><pre name="code" class="java"><span style="font-size:18px;"> /** Clock views can call this to refresh their date. **/
public static void updateDate(
String dateFormat, String dateFormatForAccessibility, View clock) {
Date now = new Date();
TextView dateDisplay;
dateDisplay = (TextView) clock.findViewById(R.id.date);
if (dateDisplay != null) {
final Locale l = Locale.getDefault();
String fmt = DateFormat.getBestDateTimePattern(l, dateFormat);
SimpleDateFormat sdf = new SimpleDateFormat(fmt, l);
dateDisplay.setText(sdf.format(now));
dateDisplay.setVisibility(View.VISIBLE);
fmt = DateFormat.getBestDateTimePattern(l, dateFormatForAccessibility);
sdf = new SimpleDateFormat(fmt, l);
dateDisplay.setContentDescription(sdf.format(now));
}
}
上面方法中发现通过dateDisplay的setText方法将日期的格式字符串给赋值,也就是sdf.format(now)这个字符串就是我们希望修改的内容,
通过更改语言,发现不同的语言显示的方式不同,所以为们必须要截取这个字符串去重新给它格式设置 ,下面是我的修改方案源码如下:
</span><pre name="code" class="java"><span style="font-size:18px;">public static void updateDate(
String dateFormat, String dateFormatForAccessibility, View clock) {
Date now = new Date();
TextView dateDisplay;
Log.i("Utils",dateFormat);
dateDisplay = (TextView) clock.findViewById(R.id.date);
if (dateDisplay != null) {
final Locale l = Locale.getDefault();
String fmt = DateFormat.getBestDateTimePattern(l, dateFormat);
fmt = getFormatString(fmt);
Log.i("Utils",dateFormat);
SimpleDateFormat sdf = new SimpleDateFormat(fmt, l);
Log.i("Utils","sdf"+sdf.toString());
dateDisplay.setText(sdf.format(now));
dateDisplay.setVisibility(View.VISIBLE);
fmt = DateFormat.getBestDateTimePattern(l, dateFormatForAccessibility);
sdf = new SimpleDateFormat(fmt, l);
dateDisplay.setContentDescription(sdf.format(now));
}
}
/**
根据给定格式修改成指定的时间,格式为:
星期,日,月,年
其他形式不变,只是调整相应位子
*/
public static String getFormatString(String str){
return getDateString("E",str)+
getDateString("d",str)+
getDateString("M",str)+
getDateString("y",str);
}
public static String getDateString(String s,String str){
String string ="";
int begin = str.indexOf(s);
int mChar = s.charAt(0);
for(int i = begin; i<str.length(); i++){
int flag = str.charAt(i);
if(flag >'A' && flag <'Z'
|| flag > 'a' && flag <'z'){
if(str.charAt(i) != mChar ) break;
}
string += str.substring(i,i+1);
}
return string;
}
</span>