android Rom修改关于系统时间日期格式的修改

本文详细介绍了如何在Android ROM中修改系统时间日期的显示格式。从定位问题到解决问题,涉及到TextClock和TextView的日期显示,以及在Utils.java中的关键代码修改,包括自定义getFormatString方法以调整日期的格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android Rom修改关于系统时间日期格式的修改
            前两天,刚开始接触rom修改,boss给了个小的不能再小的任务,关于android自带时钟时间日期格式的修改,对于android rom的初次接触,导致这些问题的处理花费了大量的精力,在网上查阅来大量资料,发现都是不想要的,对于这次任务无关紧要,所以还是自行研究,总算是将这个问题给解决。

           首先,第一步当然是定位了,打开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>
 
 
   
  
 
  


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值