Table of contents
foreword
During the development process, you will encounter the need to set buttons or touch screens for mobile phones or TVs.
1. Set focus attribute in XML
<!-- View中控件先后获取焦点的属性 -->
android:descendantFocusability="beforeDescendants"
1. beforeDescendants:viewgroup会优先其子类控件而获取到焦点
2. afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才能获取焦点
3. blockDescendants:viewGroup直接覆盖子类控件从而获取焦点
<!-- 设置可获取焦点 -->
android:focusable="true"
<!-- 设置可触摸 -->
android:focusableInTouchMode="true"
<!-- 设置可点击上下View可切换焦点 -->
android:touchscreenBlocksFocus="false"
<!-- 设置点击不穿透 -->
android:clickable="true"
<!-- 指定下一个获取焦点的控件 -->
android:nextFocusDown="@id/favorite"
2. Set focus in code
//指定控件获取焦点
mScreenshotLayout.requestFocus();
mScreenshotLayout.setNextFocusUpId(mScreenshotPreview.getId());
mScreenshotLayout.setNextFocusDownId(mScreenshotPreview.getId());
mScreenshotLayout.setNextFocusLeftId(mScreenshotPreview.getId());
mScreenshotLayout.setNextFocusRightId(mScreenshotPreview.getId());
//设置可获取焦点的属性(设置默认焦点)
mDrawerLayout.setFocusable(false);
mDrawerListView.setClickable(true);
mDrawerListView.setFocusable(true);
mDrawerListView.requestFocus();
mDrawerListView.setFocusableInTouchMode(true);
//设置listview的item可以顺畅获取焦点
mTrackList.setItemsCanFocus(true);
//设置下一个控件可获取焦点
mNextButton.setNextFocusUpId(mAlbumIcon.getId());
//设置延迟获取焦点
mSetViewFocusHandler.sendEmptyMessageDelayed(1,200);//进行延迟聚焦
Friends in need can refer to it for study, and welcome to add if there are deficiencies! ! !