1.Android 之requestFocus
设置是否获得焦点。若有requestFocus()被调用时,后者优先处理。注意在表单中想设置某一个如EditText获取焦点,光设置这个是不行的,需要将这个EditText前面的focusable都设置为false才行。在Touch模式下获取焦点需要设置focusableInTouchMode为true。
/** * Call this to try to give focus to a specific view or to one of its descendants * and give it hints about the direction and a specific rectangle that the focus * is coming from. The rectangle can help give larger views a finer grained hint * about where focus is coming from, and therefore, where to show selection, or * forward focus change internally. * * @return Whether this view or one of its descendants actually took focus. */ public boolean requestFocus(int direction, Rect previouslyFocusedRect) { // need to be focusable if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE || (mViewFlags & VISIBILITY_MASK) != VISIBLE) { return false; } // need to be focusable in touch mode if in touch mode if (isInTouchMode() && (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) { return false; } // need to not have any parents blocking us if (hasAncestorThatBlocksDescendantFocus()) { return false; } handleFocusGainInternal(direction, previouslyFocusedRect); return true; }从代码中可以看出:
requestFocus() 方法在三种情况下获取焦点不能生效。
1)对应的View不支持Focus;
2) 对应的View支持Focus,但是不支持在Touch模式下的Focus;
3) 对应的View其祖先View 设置了FOCUS_BLOCK_DESCENDANTS 标志, 阻止其子View获取焦点。
而requestFocusFromTouch() 方法设计的目的就是解决requestFocus() 在第二种不能获得焦点的情况下,Touch模式下不支持焦点,也能够获得焦点使用的。
2.Android TextUtils类介绍
对于字符串处理Android为我们提供了一个简单实用的TextUtils类,如果处理比较简单的内容不用去思考正则表达式不妨试试这个在android.text.TextUtils的类,主要的功能如下:
是否为空字符 boolean
android.text.TextUtils.
isEmpty(CharSequence str)
拼接字符串 String android.text.TextUtils.join(CharSequence delimiter, Object[] tokens)
拆分字符串 String[]
android.text.TextUtils.
split(String text, String expression)
拆分字符串使用正则 String[]
android.text.TextUtils.
split(String text, Pattern pattern)
确定大小写是否有效在当前位置的文本 int
android.text.TextUtils.
getCapsMode(CharSequence cs, int off, int reqModes)
使用HTML编码这个字符串
String android.text.TextUtils.
htmlEncode(String s)
另外,
String[]
中的expression较特殊,如果采用android.text.TextUtils.
split(String text, String expression)
TextUtils.split(someString, "-");
来分割someString的话返回的将是错误结果,正确的用法应该是
TextUtils.split(line, ",|\\-");