如何生成本地化的相对时间跨度字符串,如Android警报?

时间:2013-02-07 20:12:52

标签: android

我想在设置闹钟时产生与Android闹钟类似的字符串。

实施例
  - “警报设定为6天,10小时,9分钟后。”
  - “闹钟设定为10小时,从现在起9分钟。”
  - “闹钟设定为9分钟。”

我可以使用:

DateUtils.getRelativeTimeSpanString(alarm.triggerTime, 
                    System.currentTimeMillis(), 
                    DateUtils.MINUTE_IN_MILLIS, 
                    flags).toString();

在19小时内生成类似 “的消息 。我似乎无法在5小时10小时内在19小时5分钟内 ”生成更多内容30分钟“

我想弄清楚如何使用Android本地化辅助类(如DateUtils)来实现此目的。我不想拼写可能导致其他语言问题的字符串。

1 个答案:

答案 0 :(得分:-1)

如果您使用的是Eclipse IDE,请尝试安装本教程中的GrepCode_Plugin GrepCode_Plugin

使用GrepCode Search进行android.text.format.DateUtils框搜索后,完成该操作。它会列出所有文件。尝试下载最新版本。

从该文件中获取方法并根据需要进行修改。

对于您的参考,我将使用android 4.1.1_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);
}