android 焦点问题的汇总
16lz
2021-12-04
在xml中可以设置组件的焦点,有这些属性:
android:selectAllOnFocus="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusLeft=""
android:nextFocusRight=""
android:nextFocusUp=""
android:nextFocusDown="@id/textView3"
后面四个表示,按上下左右键时,把焦点交给id为*****的组件。
在代码中可以设置焦点的相关方法和监听:
tv.setFocusable(true);tv.setFocusableInTouchMode(true);tv.requestFocus();tv.requestFocus(direction);tv.requestFocusFromTouch();tv.clearFocus();tv.isFocusable();tv.addFocusables(views, direction);tv.isFocusableInTouchMode();tv.isFocused();tv.isFocusable();tv.focusSearch(0);tv.findFocus();tv.getFocusables(0);tv.getFocusedRect(r);tv.getNextFocusLeftId();tv.getNextFocusRightId();tv.getNextFocusDownId();tv.getNextFocusUpId();tv.getOnFocusChangeListener();tv.hasFocus();tv.hasFocusable();tv.hasWindowFocus();tv.setOnFocusChangeListener(new OnFocusChangeListener() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {// TODO Auto-generated method stub}});
关于PopuWindow利用键盘来消失的方法:
在网上找到一个很好的案例。
PopupWindow在设置了焦点之后,那么这个弹出窗口就是当前操作的窗口,如果想要在此时响应按键事件,就需要在PopupWindow上注册键盘事件来响应。但是PopupWindow好像只有一个OnDismissListener,怎么来注册键盘事件监听呢?
可以利用PopupWindow里的最外层View,对该子View监听键盘事件setOnKeyListener即可。
popupwindow.setBackgroundDrawable(new BitmapDrawable());popupwindow.setFocusable(true);popupwindow.setFocusableInTouchMode(true);childrenView.setOnKeyListener(new OnKeyListener(){@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event){// TODO Auto-generated method stubif (keyCode == KeyEvent.KEYCODE_BACK){Log.v("keyCode", "/" + keyCode); if(popupwindow != null) {popupwindow.dismiss();popupwindow = null;} } return false; }});
Gravity是设置内容对它本身的位置的
layout_gravity是设置它本身在什么位置的
更多相关文章
- Android(安卓)Button 常用属性设置,圆角,边框,点击事件
- Android(安卓)Button 常用属性设置,圆角,边框,点击事件
- 监控云服务器是否正常工作——TCPing命令的设置
- github的clone有安装不了依赖的问题错误代码128,设置GitHub代理
- 保存用户设置练习
- Vue:怎么设置,不让eslint报错
- localstroage的保存用户设置
- 前端基础,伸缩盒布局小案例及总结
- 如何设置checkbox复选框某一个选项一直处于选中状态,不可取消选择