在android开发中,常常会遇到这样的问题:当你浏览完当前页面后,跳转到下一页面浏览,但你想回到上一页面查看一条信息时,发现上一页面显示底部。
这对客户来说体验可能不够好,如何让这一页面显示顶部?
可以在布局文件中添加两行代码:android:focusable=true
android:focusableInTouchMod=true
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:focusable="true"
android:focusableInTouchMod"true">
<!-- TODO: Update blank fragment layout 主页-->
具体而言,
android:focusable=true
android:focusableInTouchMod=true
也可以用在这样的情境下:进入页面时,不想让文字输入框(Editext)自动获取焦点;
<LinearLayout
style="@style/FillWrapWidgetStyle"
android:orientation="vertical"
android:background="@color/black"
android:gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
>
也可以在代码中实现
EditText对象的clearFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);(关闭软键盘。。。)
android:focusableInTouchMode="true"
这个属性的意思很明显,就是可以通过touch来获得focus。在手机上开发用的不多,因为大多手机都是触摸式的。最近一个TV上的应用需要用到。
一个界面上有一个自定义个button,我需要进入这个界面就高亮这个button。但是用遥控选择这个程序进入能正常显示,用鼠标点击进入就没有高亮的效果。最后添加上这个属性,效果出来了。
可见,click事件和touch事件在焦点处理上是不同的。