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)来让开发者自定义错误提示图片。
效果图: Android开发中这些小技巧你都知道吗?(一)_第1张图片

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








你可能感兴趣的:(android)