Android - Context中的getText(int resId)方法和getString(int resId)方法的區別


Android開發中,經常在Activity中使用getText(int resId)和getString(int resId)這兩個方法,那么這兩個方法有什么區別和聯系呢?

這兩個方法的參數都是資源ID,區別在於getText(int resId)返回的是一個CharSequence,而getString(int resId)返回的是一個String。源代碼如下:

getText(int resId):

 /**
  * Return a localized, styled CharSequence from the application's package's
  * default string table.
  *
  * @param resId Resource id for the CharSequence text
  */
 public final CharSequence getText(int resId) {
     return getResources().getText(resId);
 }

 

getString(int resId):

/**
  * Return a localized string from the application's package's
  * default string table.
  *
  * @param resId Resource id for the string
  */
 public final String getString(int resId) {
     return getResources().getString(resId);
 }

 

可以看到,他們在各自的內部又調用了Resources類的getText(int id)和getString(int id)方法,那么我們就再看一下Resources類中的這兩個方法是怎么寫的:

Resources類中的getText(int id):

 /**
   * Return the string value associated with a particular resource ID.  The
   * returned object will be a String if this is a plain string; it will be
   * some other type of CharSequence if it is styled.
   * {@more}
   *
   * @param id The desired resource identifier, as generated by the aapt
   *           tool. This integer encodes the package, type, and resource
   *           entry. The value 0 is an invalid identifier.
   *
   * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
   *
   * @return CharSequence The string data associated with the resource, plus
   *         possibly styled text information.
   */
  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));
  }

 

Resources類中的getString(int id):

/**
   * Return the string value associated with a particular resource ID.  It
   * will be stripped of any styled text information.
   * {@more}
   *
   * @param id The desired resource identifier, as generated by the aapt
   *           tool. This integer encodes the package, type, and resource
   *           entry. The value 0 is an invalid identifier.
   *
   * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
   *
   * @return String The string data associated with the resource,
   * stripped of styled text information.
   */
  public String getString(int id) throws NotFoundException {
      CharSequence res = getText(id);
      if (res != null) {
          return res.toString();
      }
      throw new NotFoundException("String resource ID #0x"
                                  + Integer.toHexString(id));
  }

 

看到這里我想大家就都明白了,Resources類的中getString(int id)方法其實就是調用了Resources類的getText(int id)方法后,多做了一個toString()處理,那么也就是說,我們要討論的問題的結論就可以理解為:Context類中的getString(int resId)==getText(int resId).toString()。

 

更直觀一點,我們來做個小demo:

首先,在Strings中定義兩個string資源:

<string name="get_text"><b>getText</b></string>
<string name="get_string"><b>getString</b></string>

 

接着,在layout文件中定義兩個TextView,分別是textView1和textView2(代碼省略)。

最后,分別使用getText(int resId)和getString(int resId)獲取上面兩個資源,並在textView1和textView2中顯示:

CharSequence charSequence = getText(R.string.get_text);
String str = getString(R.string.get_string);
((TextView)findViewById(R.id.textView1)).setText(charSequence);
((TextView)findViewById(R.id.textView2)).setText(str);

 

結果一目了然:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM