Android開發中這些小技巧你都知道嗎?(一)



public void startActivities (Intent[] intents)

Added in API level 11

  • 該方法和我們平常用到的startActivity非常相似,只不過將Intent[]中的Intent所指向的跳轉目標Activity從后往前依次添加到返回棧中。跳轉完后如果按Back鍵的話會發現返回的順序和Intent[]中的順序前后一致。

Same as startActivities(Intent[],
Bundle)
 with no options specified.

Parameters
intents The intents to start.
Throws
  android.content.ActivityNotFoundException

public static boolean isEmpty (CharSequence str)

Added in API level 1

  • 我經常在項目中使用,判斷字符創是否為空或是否為null非常方便。

Returns true if the string is null or 0-length.

Parameters
str the string to be examined
Returns
  • true if str is null or zero length

public static Spanned fromHtml (String source)

Added in API level 1

  • 一個很方便格式化Html代碼的方法,但因為處理速度不太快,所以我不太經常用它。不建議用該方法處理String樣式,通常建議使用Spannable來處理。

Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.

This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.

public void setError (CharSequence error)

Added in API level 1

  • 在TextView上不太常用,更多用在EditText上(EditText繼承自TextView)提示用戶輸入非法。還有個多態方法setError(CharSequence error,Drawable icon)來讓開發者自定義錯誤提示圖片。
效果圖:

Sets the right-hand compound drawable of the TextView to the "error" icon and sets an error message that will be displayed in a popup when the TextView has focus. The icon and error message will be reset to null when any key events cause changes to the TextView's text. If the error is null, the error message and icon will be cleared.

public static String getStackTraceString (Throwable tr)

Added in API level 1

  • 有時候我們希望程序拋出異常時能把異常信息保存到制定目錄的文件中,getStackTraceString就可以將異常信息轉換成字符串的形式。
使用示例:
try {
//TODO
} catch (Exception e) {
String exceptionStr = Log.getStackTraceString(e);
}

Handy function to get a loggable stack trace from a Throwable

Parameters
tr An exception to log

public static LayoutInflater from (Context context)

Added in API level 1

  • 和冗長的getSystemService()說Goodbye。

Obtains the LayoutInflater from the given context.

public abstract File getCacheDir ()

Added in API level 1

  • 獲取應用默認緩存路徑“/data/data/應用包名/cache”

Returns the absolute path to the application specific cache directory on the filesystem. These files will be ones that get deleted first when the device runs low on storage. There is no guarantee when these files will be deleted. Note: you should not rely on the system deleting these files for you; you should always have a reasonable maximum, such as 1 MB, for the amount of space you consume with cache files, and prune those files when exceeding that space.

Returns
  • The path of the directory holding application cache files.

public void reverse ()

Added in API level 11

  • 因為當調用這個方法時,如果動畫正在播放,可以反向播放動畫直到回播放的原點。所以我喜歡用它來平滑的結束動畫的播放。

Plays the ValueAnimator in reverse. If the animation is already running, it will stop itself and play backwards from the point reached when reverse was called. If the animation is not currently running, then it will start from the end and play backwards. This behavior is only set for the current animation; future playing of the animation will use the default behavior of playing forward.

參考資料:

Android Tips Round-Up, Part 2









注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com