Android4.0 时间显示12小时制没有显示AM、PM

本文详细介绍了如何在状态栏和锁屏界面的数字时钟中,根据不同时间显示模式(24小时制或12小时制),动态调整时间格式,包括去除上午/下午标识符AM/PM(在24小时制下不显示),以实现更灵活的时间显示功能。

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

修改状态栏中控制时间的类HoloClock.java的方法getTimeText()进行修改,判断时间是否为24小时制,若是则直接返回时间若不是则判断是上午还是下午,上午的话添加AM,下午添加PM。

private final CharSequence getTimeText() {
        Context context = getContext();
        int res = DateFormat.is24HourFormat(context)
            ? com.android.internal.R.string.twenty_four_hour_time_format
            : com.android.internal.R.string.twelve_hour_time_format;


        SimpleDateFormat sdf;
        String format = context.getString(res);
        if (!format.equals(mClockFormatString)) {
            // we don't want AM/PM showing up in our statusbar, even in 12h mode
            format = format.replaceAll("a", "").trim();
            mClockFormat = sdf = new SimpleDateFormat(format);
            mClockFormatString = format;
        } else {
            sdf = mClockFormat;
        }
        String result = sdf.format(mCalendar.getTime());
        //2012-9-14 START can not show AM\BM in 12hour
        //return result;
		if(DateFormat.is24HourFormat(context)){
    		return result;
    	}else{
    		String ampmValues;
    		if(mCalendar.get(Calendar.AM_PM) == 0){
    			ampmValues = "AM";
    		}else{
    			ampmValues = "PM";
    		}
    		return result+" "+ampmValues;
    	}
		//2012-9-14 END can not show AM\BM in 12hour
    }


修改锁屏界面控制时间的类DigitalClock.java的方法updateTime(),同上判断时间是否为24小时制和是上午还是下午。

private void updateTime() {
        mCalendar.setTimeInMillis(System.currentTimeMillis());

        CharSequence newTime = DateFormat.format(mFormat, mCalendar);
		
		//2012-9-14 START can not show AM\BM in 12hour
		Context context = getContext();
		String ampmValues;
		if(DateFormat.is24HourFormat(context)){
    		ampmValues = "";
    	}else{
    		if(mCalendar.get(Calendar.AM_PM) == 0){
    			ampmValues = " AM";
    		}else{
    			ampmValues = " PM";
    		}
    	}
		newTime = newTime+ampmValues;
		//2012-9-14 END can not show AM\BM in 12hour
		
        mTimeDisplayBackground.setText(newTime);
        mTimeDisplayForeground.setText(newTime);
        mAmPm.setIsMorning(mCalendar.get(Calendar.AM_PM) == 0);
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值