如何生成本地化的相对时间跨度字符串,如Android报警?
我想生成一个类似于Android alarm在设置报警时所做的字符串 示例:如何生成本地化的相对时间跨度字符串,如Android报警?,android,Android,我想生成一个类似于Android alarm在设置报警时所做的字符串 示例: -“从现在起,警报设置为6天10小时9分钟。” -“警报设置为10小时9分钟。” -“从现在起,警报设置为9分钟。” 我能够使用: DateUtils.getRelativeTimeSpanString(alarm.triggerTime, System.currentTimeMillis(), DateUtils.MINUTE_IN_
-“从现在起,警报设置为6天10小时9分钟。”
-“警报设置为10小时9分钟。”
-“从现在起,警报设置为9分钟。” 我能够使用:
DateUtils.getRelativeTimeSpanString(alarm.triggerTime,
System.currentTimeMillis(),
DateUtils.MINUTE_IN_MILLIS,
flags).toString();
它会在19小时内生成类似于“的消息”。我似乎无法在19小时5分钟内制作出更像“或“5天10小时30分钟”
如果可能的话,我想弄清楚如何使用Android本地化的助手类,比如
DateUtils
,来实现这一点。我不想缝合可能会在其他语言上引起问题的字符串。如果您使用的是EclipseIDE
,请尝试安装本教程中的GrepCode\u插件
在grepcodesearch
框中完成后,使用android.text.format.DateUtils
进行搜索。它将列出所有文件。尝试下载最新版本
从该文件中获取方法,并根据需要对其进行修改
为了便于参考,我将使用android 4.1.1\u r1中列出的方法
public static CharSequence getRelativeTimeSpanString(long time, long now, long minResolution,
int flags) {
Resources r = Resources.getSystem();
boolean abbrevRelative = (flags & (FORMAT_ABBREV_RELATIVE | FORMAT_ABBREV_ALL)) != 0;
boolean past = (now >= time);
long duration = Math.abs(now - time);
int resId;
long count;
if (duration < MINUTE_IN_MILLIS && minResolution < MINUTE_IN_MILLIS) {
count = duration / SECOND_IN_MILLIS;
if (past) {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_num_seconds_ago;
} else {
resId = com.android.internal.R.plurals.num_seconds_ago;
}
} else {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_in_num_seconds;
} else {
resId = com.android.internal.R.plurals.in_num_seconds;
}
}
} else if (duration < HOUR_IN_MILLIS && minResolution < HOUR_IN_MILLIS) {
count = duration / MINUTE_IN_MILLIS;
if (past) {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_num_minutes_ago;
} else {
resId = com.android.internal.R.plurals.num_minutes_ago;
}
} else {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_in_num_minutes;
} else {
resId = com.android.internal.R.plurals.in_num_minutes;
}
}
} else if (duration < DAY_IN_MILLIS && minResolution < DAY_IN_MILLIS) {
count = duration / HOUR_IN_MILLIS;
if (past) {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_num_hours_ago;
} else {
resId = com.android.internal.R.plurals.num_hours_ago;
}
} else {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_in_num_hours;
} else {
resId = com.android.internal.R.plurals.in_num_hours;
}
}
} else if (duration < WEEK_IN_MILLIS && minResolution < WEEK_IN_MILLIS) {
count = getNumberOfDaysPassed(time, now);
if (past) {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_num_days_ago;
} else {
resId = com.android.internal.R.plurals.num_days_ago;
}
} else {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_in_num_days;
} else {
resId = com.android.internal.R.plurals.in_num_days;
}
}
} else {
// We know that we won't be showing the time, so it is safe to pass
// in a null context.
return formatDateRange(null, time, time, flags);
}
String format = r.getQuantityString(resId, (int) count);
return String.format(format, count);
}
public static CharSequence getRelativeTimeSpanString(长时间、长现在、长最小分辨率、,
int标志){
Resources r=Resources.getSystem();
布尔值abbrevRelative=(flags&(FORMAT_ABBREV_RELATIVE | FORMAT_ABBREV_ALL))!=0;
布尔过去=(现在>=时间);
长持续时间=Math.abs(现在-时间);
国际剩余;
长计数;
if(持续时间<分钟单位毫秒和最小分辨率<分钟单位毫秒){
计数=持续时间/秒(单位:毫秒);
如果(过去){
if(abbrevRelative){
resId=com.android.internal.R.plurals.abbrev\u num\u秒前;
}否则{
resId=com.android.internal.R.plurals.num\u秒前;
}
}否则{
if(abbrevRelative){
resId=com.android.internal.R.plurals.abbrev_(单位:秒);
}否则{
resId=com.android.internal.R.plurals.in_num_seconds;
}
}
}else if(持续时间<小时单位(毫秒)&&minResolution<小时单位(毫秒)){
计数=持续时间/分钟(单位:毫秒);
如果(过去){
if(abbrevRelative){
resId=com.android.internal.R.plurals.abbrev\u num\u分钟前;
}否则{
resId=com.android.internal.R.plurals.num\u分钟前;
}
}否则{
if(abbrevRelative){
resId=com.android.internal.R.plurals.abbrev_in_num_minutes;
}否则{
resId=com.android.internal.R.plurals.in_num_minutes;
}
}
}否则,如果(持续时间<以毫秒计的天<以毫秒计的分钟分辨率<以毫秒计的天){
计数=持续时间/小时(单位:毫秒);
如果(过去){
if(abbrevRelative){
resId=com.android.internal.R.plurals.abbrev\u num\u小时前;
}否则{
resId=com.android.internal.R.plurals.num\u小时前;
}
}否则{
if(abbrevRelative){
resId=com.android.internal.R.plurals.abbrev_in_num_小时;
}否则{
resId=com.android.internal.R.plurals.in_num_小时;
}
}
}else if(持续时间<以毫秒为单位的周数&&minResolution<以毫秒为单位的周数){
count=getNumberOfDaysPassed(时间,现在);
如果(过去){
if(abbrevRelative){
resId=com.android.internal.R.plurals.abbrev\u num\u天之前;
}否则{
resId=com.android.internal.R.plurals.num\u天之前;
}
}否则{
if(abbrevRelative){
resId=com.android.internal.R.plurals.abbrev_in_num_天;
}否则{
resId=com.android.internal.R.plurals.in_num_天;
}
}
}否则{
//我们知道我们不会显示时间,所以可以安全通过
//在空上下文中。
返回formatDateRange(空、时间、时间、标志);
}
字符串格式=r.getQuantityString(剩余,(int)计数);
返回String.format(格式、计数);
}
感谢您的回答,但是我已经查看了您发布的源代码,并且能够确认我无法从该功能中获得所需的内容。有了它,你只能得到天、小时或分钟的字符串,但永远不能像我所希望的那样把它们全部写进一句话中/