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()
---------------------

原文地址:https://www.cnblogs.com/ly570/p/10995909.html

时间: 2024-11-06 19:07:22

android全屏下的输入框未跟随软键盘弹起问题的相关文章

输入框单独被软键盘弹起的解决方案

1.重新开一个Activity 重新开一个透明的activity<style name="TransparentTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <item name="android:windowNoTitle"&

android 软键盘在全屏下的布局计算问题

在非全屏模式下,将activity的windowSoftInputMode的属性设置为:adjustResize.同时在View的onSizeChanged(int w, int h, int oldw, int oldh)里可以得到变化后的尺寸,然后根据前后变化的结果来计算屏幕需要移动的距离. 但是在全屏模式下,即使将activity的windowSoftInputMode的属性设置为:adjustResize.在键盘显示时它未将Activity的Screen向上推动,所以你Activity的

android 截屏, 消息栏未截图

原文:http://www.zuidaima.com/share/1550463684332544.htm 源代码下载地址:android 截屏, 消息栏未截图 android 截屏, 消息栏未截图, 如果谁有截屏(截全屏的) 源码截图

Android 全屏显示 and 取消标题栏

先介绍去掉标题栏的方法: 第一种:也一般入门的时候经常使用的一种方法 requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 注意这句一定要写在setContentView()方法的前面,不然会报错的 第二种:在AndroidManifest.xml文件中定义 <application android:icon="@drawable/icon" android:label="@string/app_name"

Android之监听手机软键盘弹起与关闭

背景: 在很多App开发过程中需要在Activity中监听Android设备的软键盘弹起与关闭,但是Android似乎没有提供相关的的监听API给我们来调用,本文提供了一个可行的办法来监听软键盘的弹起与关闭. 预备知识: 在manifest文件中可以设置Activity的android:windowSoftInputMode属性,这个属性值常见的设置如下: android:windowSoftInputMode="stateAlwaysHidden|adjustPan" 那么这里值的含

android在全屏下第一次触摸屏幕没有触发事件

A.设置全屏的方法很多就不多说了,常见如下两种(记录用以备忘): 1.在Androidmanifest.xml文件中设定,如: <activity android:name="com.yeertc.android.RoomActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:screenOrientation="landscape">

(转) Android 全屏控制:动态切换全屏和非全屏

转自:http://blog.csdn.net/michaelpp/article/details/7302308 动态切换全屏和非全屏: package com.screen;   import android.app.Activity;  import android.os.Bundle;  import android.view.View;  import android.view.WindowManager;  import android.view.View.OnClickListen

Android 全屏方法

先介绍去掉标题栏的方法: 第一种:也一般入门的时候经常使用的一种方法 requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 注意这句一定要写在setContentView()方法的前面,不然会报错的 第二种:在AndroidManifest.xml文件中定义 <application android:icon="@drawable/icon" android:label="@string/app_name"

Android 全屏显示的方法(不包含状态栏)

我们都知道在Android中某些功能的实现往往有两种方法:一种是在xml文件中设置相应属性,另一种是用代码实现.同样Android实现全屏显示也可以通过这两种方法实现: 1.在AndroidManifest.xml的配置文件里面的<activity>标签添加属性: android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 2.在Activity的onCreate()方法中的super()和setContentVie