【问题标题】:android getText(int resId) implementationandroid getText(int resId) 实现
【发布时间】:2012-03-13 14:40:45
【问题描述】:

我需要查看getText(int resId) 方法的实现。此方法在抽象类Context.java 中定义为final,我在ContextWrapper.javaContextThemeWrapper.java 中搜索了实现,但找不到任何实现。有人可以在这个方法实现中发布一个 ling 吗?我在 netmite.com 上查看了类实现。谢谢

【问题讨论】:

  • 如果方法在Context.java中是final,那么它的实现一定在Context.java中

标签: android methods implementation


【解决方案1】:

它是Context.javagetText()的实现。类是抽象的,但它有这个方法的实现。

public final CharSequence getText(int resId) {
    return getResources().getText(resId);
}

Resources.getText()的实现:

public CharSequence getText(int id) throws NotFoundException {
    CharSequence res = mAssets.getResourceText(id);
    if (res != null) {
        return res;
    }
    throw new NotFoundException("String resource ID #0x"
                                + Integer.toHexString(id));
}

AssetManager.getResourceText()的实现:

final CharSequence getResourceText(int ident) {
    synchronized (this) {
        TypedValue tmpValue = mValue;
        int block = loadResourceValue(ident, (short) 0, tmpValue, true);
        if (block >= 0) {
            if (tmpValue.type == TypedValue.TYPE_STRING) {
                return mStringBlocks[block].get(tmpValue.data);
            }
            return tmpValue.coerceToString();
        }
    }
    return null;
}

更新: 正如@zapl 所提到的,loadResourceValue() 是原生的,可以在 android_util_AssetManager.cpp 中找到。

【讨论】:

    【解决方案2】:

    ICS - frameworks/base/core/java/android/content/res/AssetManager.java

    /**
     * Retrieve the string value associated with a particular resource
     * identifier for the current configuration / skin.
     */
    /*package*/ final CharSequence getResourceText(int ident) {
        synchronized (this) {
            TypedValue tmpValue = mValue;
            int block = loadResourceValue(ident, (short) 0, tmpValue, true);
            if (block >= 0) {
                if (tmpValue.type == TypedValue.TYPE_STRING) {
                    return mStringBlocks[block].get(tmpValue.data);
                }
                return tmpValue.coerceToString();
            }
        }
        return null;
    }
    

    loadResourceValue() 是原生的,并在 frameworks/base/core/jni/android_util_AssetManager.cpp 中定义

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      相关资源
      最近更新 更多