getText 與getString的區別

getText 與getString的區別

1.從源碼的註釋中可以看出 getText返回了帶有格式化信息的字串,getString返回了無格式化信息的字串

/**
 * Return a localized, styled CharSequence from the application's package's
 * default string table.
 *
 * @param resId Resource id for the CharSequence text
 */
@NonNull
public final CharSequence getText(@StringRes int resId) {
    return getResources().getText(resId);
}
/**
 * Returns a localized string from the application's package's
 * default string table.
 *
 * @param resId Resource id for the string
 * @return The string data associated with the resource, stripped of styled
 *         text information.
 */
@NonNull
public final String getString(@StringRes int resId) {
    return getResources().getString(resId);
}

通過例子來看下實際的效果

//直接引用,跟進源碼,使用了getText
TextView test = findViewById(R.id.test);
test.setText(R.string.cache_tips_message);
//getText 引用,帶有格式信息,字串中的color生效
TextView test1 = findViewById(R.id.test1);
test1.setText(getText(R.string.cache_tips_message));

//getString 引用,不帶有格式信息,字串中的color不生效
TextView test2 = findViewById(R.id.test2);
test2.setText(getString(R.string.cache_tips_message));

//fromHtml,因字串未帶格式信息,失效
Spanned htmlString = Html.fromHtml(getString(R.string.cache_tips_message));
TextView test3 = findViewById(R.id.test3);
test3.setText(htmlString);
//使用CDATA包裝,可以將字串中的全部信息保留下來,通過fromHtml解析,生效
Spanned htmlString1 = Html.fromHtml(getString(R.string.cache_tips_message1));
TextView test4 = findViewById(R.id.test4);
test4.setText(htmlString1);
//xml 中的字符串
<string name="cache_tips_message">當前緩存已達到<font color="#f55353">100MB</font>,建議您清理緩存,保證系統流暢!</string>

//使用cdata處理的字符串,CDATA全名爲character data,指不使用XML解析器解析的文本數據。在標記CDATA下,所有與XML規範衝突的關鍵字字符串都被XML處理程序一視同仁地當做字符數據看待
<string name="cache_tips_message1"><![CDATA[當前緩存已達到<font color="#f55353">100MB</font>,建議您清理緩存,保證系統流暢]]></string>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/test1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/test2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/test3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/test4"
        android:layout_width="wrap_content"
        android:text="@string/cache_tips_message"
        android:layout_height="wrap_content"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="check CurrentVision"
        android:onClick="onCheckCurrentVision"/>
        
  </LinearLayout>

實際效果

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-ZKM927DU-1575285776912)(D:\我的文檔\11070535\Desktop\Screenshot_2019_1202_174157.png)]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章