使Edittext失去焦点,只需使其父布局获取焦点即可
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:text="搜索你感兴趣的内容..."
android:singleLine="true"
android:textSize="14sp"/>
</RelativeLayout>
代码中使其父布局获取焦点:
mTitle.setFocusableInTouchMode(true);
mTitle.requestFocus();
android:focusable和android:focusableInTouchMode的区别:
android:focusableInTouchMode
点击屏幕的上的某个控件时,不要立即执行相应的点击逻辑,而是先显示焦点(即控件被选中),再点击才执行逻辑。
android:focusable=“true”不会改变android:focusableInTouchMode,因此只在键盘状态下显示焦点,在TouchMode状态下,依旧无法显示焦点。
android:focusable=“false”,一定会使android:focusableInTouchMode=“false”。
相对的
android:focusableInTouchMode=“false”,不会影响android:focusable。
android:focusableInTouchMode=”true”,一定会是android:focusable=“true”
如果要获取焦点只需要设置
android:foucusableInTouchMode=“true”
所有的获取焦点,都要有一个前提,那就是该控件必须设置android:clickable=”true”