android全屏下的输入框未跟随软键盘弹起问题
最近开发中遇到,全屏模式下输入框在底部不会跟随软键盘弹起。于是网上搜索了解决的方案。大致找到了两种方案。
第一种
定义好此类
public class SoftKeyBoardListener {
private View rootView;//activity的根视图
int rootViewVisibleHeight;//纪录根视图的显示高度
private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
public SoftKeyBoardListener(Activity activity) {
//获取activity的根视图
rootView = activity.getWindow().getDecorView();
//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//获取当前根视图在屏幕上显示的大小
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int visibleHeight = r.height();
System.out.println(""+visibleHeight);
if (rootViewVisibleHeight == 0) {
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
if (rootViewVisibleHeight == visibleHeight) {
return;
}
//根视图显示高度变小超过200,可以看作软键盘显示了
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度变大超过200,可以看作软键盘隐藏了
if (visibleHeight - rootViewVisibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
}
});
}
private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
}
public interface OnSoftKeyBoardChangeListener {
void keyBoardShow(int height);
void keyBoardHide(int height);
}
public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
}
}
在Activity中实现
private SoftKeyBoardListener.OnSoftKeyBoardChangeListener changeListener = new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
//软键盘弹起,inputEt为底部输入框
ObjectAnimator animator = ObjectAnimator.ofFloat(inputEt, "translationY", 0, -height);
animator.setDuration(100);
animator.start();
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();//获取焦点 光标出现
}
@Override
public void keyBoardHide(int height) {
//软键盘隐藏
ObjectAnimator animator = ObjectAnimator.ofFloat(inputEt, "translationY", -height, 0);
animator.setDuration(100);
animator.start();
}
};
第二种此方法在全屏模式webview的输入框被遮盖时可以解决。
public class KeyBoardListener {
private Activity activity;
// private Handler mhanHandler;
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private static KeyBoardListener keyBoardListener;
public static KeyBoardListener getInstance(Activity activity) {
// if(keyBoardListener==null){
keyBoardListener = new KeyBoardListener(activity);
// }
return keyBoardListener;
}
public KeyBoardListener(Activity activity) {
super();
// TODO Auto-generated constructor stub
this.activity = activity;
// this.mhanHandler = handler;
}
public void init() {
final FrameLayout content = (FrameLayout) activity
.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
// content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
// int rootViewHeight;
// @Override
// public void onGlobalLayout() {
// int viewHeight = content.getHeight();
// if (rootViewHeight != viewHeight) {
// rootViewHeight = viewHeight;
// if (viewHeight == getRealHeight()) {
// //隐藏虚拟按键
// if (navigationListener != null) {
// navigationListener.hide();
// }
// } else {
// //显示虚拟按键
// if (navigationListener != null) {
// navigationListener.show();
// }
// }
// }
// }
// });
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent
.getLayoutParams();
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = 0;
if(getNavigationBarHeight(activity) == getRealHeight() - getHeight()){
usableHeightSansKeyboard = getHeight();
}else{
usableHeightSansKeyboard = getRealHeight();
}
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard / 4)) {
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard
- heightDifference;
} else {
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightSansKeyboard;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
// private void showLog(String title, String msg) {
// Log.d("Unity", title + "------------>" + msg);
// }
/**
* 获取屏幕真实高度(包括虚拟键盘)
*/
public static int getRealHeight() {
WindowManager windowManager = (WindowManager) MyApp.mApp.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
display.getRealMetrics(dm);
} else {
display.getMetrics(dm);
}
int realHeight = dm.heightPixels;
return realHeight;
}
/**
* 获取手机屏幕高度
*/
public static int getHeight() {
WindowManager windowManager = (WindowManager) MyApp.mApp.getSystemService(Context.WINDOW_SERVICE);
LogUtil.e("height->",windowManager.getDefaultDisplay().getHeight() + "aa");
return windowManager.getDefaultDisplay().getHeight();
}
public static int getNavigationBarHeight(Activity activity) {
Resources resources = activity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height",
"dimen", "android");
//获取NavigationBar的高度
int height = resources.getDimensionPixelSize(resourceId);
return height;
}
}
在Activity中的实现:
KeyBoardListener.getInstance(this).init()
---------------------
android全屏下的输入框未跟随软键盘弹起问题的更多相关文章
- android全屏去掉title栏的多种实现方法
android全屏去掉title栏的多种实现方法 作者: 字体:[增加 减小] 类型:转载 时间:2013-02-18我要评论 android全屏去掉title栏包括以下几个部分:实现应用中的所有ac ...
- Android 全屏显示
Android全屏显示: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst ...
- 仿新浪游戏频道js多栏目全屏下拉菜单导航条
仿新浪游戏频道js多栏目全屏下拉菜单导航条,新浪,游戏频道,js组件,多栏目,全屏下拉,下拉菜单,导航条.代码下载地址:http://www.huiyi8.com/sc/26765.html更多请访问 ...
- H5软键盘弹起收回(IOS与Android)
IOS下中,软键盘处于窗口最顶层,与原有的窗口不冲突,所以底部导航条不会被顶起,但是在android下,软键盘与窗口处于同一层,所以当软键盘弹起时,当前窗口缩小,那么窗口内容自然要被挤: 解决办法: ...
- Android之监听手机软键盘弹起与关闭
背景: 在很多App开发过程中需要在Activity中监听Android设备的软键盘弹起与关闭,但是Android似乎没有提供相关的的监听API给我们来调用,本文提供了一个可行的办法来监听软键盘的弹起 ...
- android在全屏下第一次触摸屏幕没有触发事件
A.设置全屏的方法很多就不多说了,常见如下两种(记录用以备忘): 1.在Androidmanifest.xml文件中设定,如: <activity android:name="com. ...
- android 全屏视频播放(SurfaceView + MediaPlayer)
介绍个第三方: JieCaoVideoPlayer 实现Android的全屏视频播放,支持完全自定义UI.手势修改进度和音量.hls.rtsp,设置http头信息,也能在ListView.ViewPa ...
- 【转】Android 全屏方案(隐藏NavigationBar)
http://www.07net01.com/2015/04/822292.html 在android4.0及其以上的版本中,出现了一个很屌的东西,叫做Navigation Bar,它和Status ...
- Android全屏(包含3种隐藏顶部状态栏及标题栏和一种隐藏Android 4.0平板底部状态栏的方法)
http://www.xuebuyuan.com/558284.html 方法一 public class MainActivity extends Activity { @Override prot ...
随机推荐
- 06springMVC数据验证
u 声明式数据验证 u 内置的验证约束和注解 u 错误消息 u 功能处理方法上多个验证参数的处理 u 异常处理的支持 1 声明式数据验证 Spring3开始支持JSR-303验证框 ...
- hdu_1863_畅通工程_201403122000
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- linux下环境变量C_INCLUDE_PATH
环境变量定义一般都是/etc/profile文件(对所有用户有效),或者在Home目录下的.bashrc或.profile(只对当前用户有效)一般系统安装了编译工具之后无需设置这些变量编译都不会出现问 ...
- Gulp帮你自己主动搞定coffee和scss的compile
今天继续说说gulp的watch,能够自己主动搞定非常多事情.不用每次都去敲命令了! 上次说到用gulp能够非常方便的进行css,js,html的压缩.而且能够对coffee和scss进行编译. cs ...
- mysql高可用架构方案之中的一个(keepalived+主主双活)
Mysql双主双活+keepalived实现高可用 文件夹 1.前言... 4 2.方案... 4 2.1.环境及软件... 4 2.2.IP规划... 4 2.3.架构图... ...
- ZOJ 3814 Sawtooth Puzzle (2014年牡丹江赛区网络赛F题)
1.题目描写叙述:点击打开链接 2.解题思路:本题是一道隐式图的搜索题目.一般来说,这类题目首先要定义状态,接下来是弄清楚状态怎样转移,以及状态怎样判重,怎样推断当前状态是否和目标状态同样.至于求解最 ...
- 3.CCFadeOutTRTiles,部落格效果,跳动的方块特效,3D瓷砖晃动特效,破碎的3D瓷砖特效,瓷砖洗牌特效,分多行消失特效,分多列消失特效
1 TiledGrid3D //TiledGrid3D //CCFadeOutTRTiles * action = CCFadeOutTRTiles::create(2, CCSize(20,2 ...
- NYOJ15括号匹配
NYOJ15括号匹配 括号匹配(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 给你一个字符串,里面只包含"(",")" ...
- 关于我们ajax异步请求的方法与知识
做前端开发的朋友对于ajax异步更新一定印象深刻,作为刚入坑的小白,今天就和大家一起聊聊关于ajax异步请求的那点事.既然是ajax就少不了jQuery的知识,推荐大家访问www.w3school ...
- 树形$dp$学习笔记
今天学习了树形\(dp\),一开始浏览各大\(blog\),发现都\(TM\)是题,连个入门的\(blog\)都没有,体验极差.所以我立志要写一篇可以让初学树形\(dp\)的童鞋快速入门. 树形\(d ...