工具类-虚拟键盘相关大全
2016-04-25 11:44
190 查看
工具类-虚拟键盘相关大全
1.监听输入隐藏及其高度。(可用于edittext被遮挡时候滑动调整的距离)
2.显示隐藏软键盘,强制显示软键盘
1.监听虚拟键盘隐藏及其高度
2.显示隐藏软键盘,强制显示软键盘
1.监听输入隐藏及其高度。(可用于edittext被遮挡时候滑动调整的距离)
2.显示隐藏软键盘,强制显示软键盘
1.监听虚拟键盘隐藏及其高度
public static interface OnSoftKeyBoardVisibleListener{ void onSoftKeyBoardVisible(boolean isVisible,int displayHight,int hight); } /**监听软键盘状态 * @param activity * @param listener */ public static void addOnSoftKeyBoardVisibleListener(Activity activity, final OnSoftKeyBoardVisibleListener listener) { final View decorView = activity.getWindow().getDecorView(); decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { private boolean sLastVisiable; @Override public void onGlobalLayout() { Rect rect = new Rect(); decorView.getWindowVisibleDisplayFrame(rect); int displayHight = rect.bottom - rect.top; int hight = decorView.getHeight(); boolean visible = (double) displayHight / hight < 0.8; // Log.d("rex", "DecorView display hight = " + displayHight); // Log.d("rex", "DecorView hight = " + hight); // Log.d("rex", "softkeyboard visible = " + visible); if(visible != sLastVisiable){ listener.onSoftKeyBoardVisible(visible,displayHight,hight); } sLastVisiable = visible; } });
2.显示隐藏软键盘,强制显示软键盘
/** * 隐藏软键盘 * @param activity */ public static void hideInput(Activity activity) { View view = activity.getWindow().peekDecorView(); if (view != null) { InputMethodManager inputmanger = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0); } } //强制显示或者关闭系统键盘 public static void KeyBoard(final EditText txtSearchKey,final String status) { Timer timer = new Timer(); timer.schedule(new TimerTask(){ @Override public void run() { InputMethodManager m = (InputMethodManager) txtSearchKey.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); if(status.equals("open")) { m.showSoftInput(txtSearchKey,InputMethodManager.SHOW_FORCED); } else { m.hideSoftInputFromWindow(txtSearchKey.getWindowToken(), 0); } } }, 300); } //显示虚拟键盘 public static void ShowKeyboard(View v) { InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE ); imm.showSoftInput(v,InputMethodManager.SHOW_FORCED); } //隐藏虚拟键盘 public static void HideKeyboard(View v) { InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE ); if ( imm.isActive( ) ) { imm.hideSoftInputFromWindow( v.getApplicationWindowToken( ) , 0 ); } }
相关文章推荐
- 图片加载的学习体会吧
- 资源分配问题
- 【.net 深呼吸】自定义缓存配置(非Web项目)
- linux设置定时任务
- hp prodesk 498g2 mt 升级win10后网卡红叉叉,灯不闪烁
- Google protocol buffers 小结(二)
- C/C++ volatile让你看的更明白
- LeetCode 215. Kth Largest Element in an Array
- rpm命令汇总
- SVN提交改动时报错You must input more than 5 chars as comment!
- 关于Weex你需要知道的一切
- linux top命令详解
- 论div,button,input点击跳转页面不同window.open()
- OpenCv学习笔记(四)--Mat基本图像容器Mat对象信息头,矩阵体的创建,深复制,浅复制详解
- apache本地配置多域名(wampserver本地配置多域名)
- kafka简单安装部署
- centos 7安装jdk和elk
- Android 项目 UI混乱,Button变大,解决方案
- 阿里云ecs云服务器安装wdcp控制面板教程(推荐CentOS6.5)
- 阿里百川与极客邦科技达成战略合作 Weex宣布开源