软键盘的应用
2017-01-03 12:31
363 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zwhtiger/article/details/53993310
在安卓开发中软键盘是在评论模块中不可缺少的内容:
1.评论嵌套在scrollView中,这是评论的弹出是遮盖到评论的内容的,这是要用到scrollTO的方法;
其中parentLayout是根布局的main
parentLayout.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect r = new Rect(); // r will be populated with the coordinates of your view // that area still visible. parentLayout.getWindowVisibleDisplayFrame(r); DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics( metric); int screenHeight = metric.widthPixels; // 屏幕宽度(像素) int heightDiff = screenHeight - (r.bottom - r.top); if (heightDiff > 100) // if more than 100 pixels, its probably a keyboard // get status bar height try { Class<?> c = Class .forName("com.android.internal.R$dimen"); Object obj = c.newInstance(); Field field = c.getField("status_bar_height"); int x = Integer.parseInt(field.get(obj) .toString()); statusBarHeight = context.getResources() .getDimensionPixelSize(x); } catch (Exception e) { e.printStackTrace(); } realKeyboardHeight = heightDiff - statusBarHeight; if (realKeyboardHeight > 0) { //这里的post是必不可少的。 scrollView.post(new Runnable() { @Override public void run() { scrollView.scrollBy(0, 5 * realKeyboardHeight); } }); }else { scrollView.post(new Runnable() { @Override public void run() { scrollView.scrollBy(0, -5 * realKeyboardHeight); } }); } } }); rl_more.setFocusable(true); rl_more.setFocusableInTouchMode(true); rl_more.requestFocus(); 还有一种就是监听软键盘的确认按钮: tv.setOnEditorActionListener(this); public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean isOK = true; switch (actionId) { case EditorInfo.IME_ACTION_NONE: Toast.makeText(this, "点击-->NONE", Toast.LENGTH_SHORT).show(); break; case EditorInfo.IME_ACTION_GO: Toast.makeText(this, "点击-->GO", Toast.LENGTH_SHORT).show(); break; case EditorInfo.IME_ACTION_SEARCH: Toast.makeText(this, "点击-->SEARCH", Toast.LENGTH_SHORT).show(); break; case EditorInfo.IME_ACTION_SEND: Toast.makeText(this, "点击-->SEND", Toast.LENGTH_SHORT).show(); break; case EditorInfo.IME_ACTION_NEXT: Toast.makeText(this, "点击-->NEXT", Toast.LENGTH_SHORT).show(); break; default: isOK = false; break; } return isOK; } 以上两种是必备的。
相关文章推荐
- android 表情,软键盘冲突解决方案(仿微博等SNS应用)
- android 表情,软键盘冲突解决方案(仿微博等SNS应用)
- android 表情,软键盘冲突解决方案(仿微博等SNS应用)
- EditText实现搜索框常见需求详解(配合软键盘的应用和模拟搜索List结果集)
- 基础数据结构代码及其应用
- ARM的体系结构与编程系列博客——ARM的历史与应用范围
- 栈的应用(一)-括号匹配
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- 第6章 CXF的前端应用
- 《Computer Vision:Algorithms and Applications》计算机视觉 算法与应用 学习系列 目录
- ajax在实际应用中是要选择同步还是异步
- 精仿百思不得姐客户端应用iOS源码
- 机器学习系列(3)_逻辑回归应用之Kaggle泰坦尼克之灾
- 如何在移动端app中应用字体图标icon fonts (转)
- Unity3D自学笔记——架构应用(八)人物移动与角色相机的跟随
- 如何快速学会单片机编程并应用?
- 弹性布局基础讲解与高效应用
- iOS 做应用项目开始前的准备工作
- 【建模必备】遗传算法应用举例(简单的一元函数优化实例)
- Python之opencv应用