在安卓中没有一个单纯的函数用于判断当前键盘的可见性。最近工作中用到,在此记录一下。
1.在Manifest中设置Activity键盘弹出模式
android:windowSoftInputMode="adjustResize"
官方解释如下
"adjustResize" The activity's main window is always resized to make room for the soft keyboard on screen.
我的理解就是在键盘弹出或关闭的时候主窗口也会进行相应的调整。
2.使用的简单布局activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
3.一个阐述原理的Activity
package com.hang.studykeyapp;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
public class MainActivity extends Activity {
final String tag = MainActivity.class.getSimpleName();
View activityRootView;
//get status bar height+navigation height;
private int addtionHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addtionHeight = getStatusBarHeight() + getNavigationHeight(this);
activityRootView = findViewById(android.R.id.content);
//对页面布局变化进行监听
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalListener);
}
ViewTreeObserver.OnGlobalLayoutListener mGlobalListener = 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.
activityRootView.getWindowVisibleDisplayFrame(r);
//这里判断高度差,这里需要注意一下,使用不同Theme时,addtionHeight的情况不同(可能仅仅需要减去状态栏)
//这里没有关系的,那么高的一个键盘,不在意这点误差的,产生负数时不要紧的
//如果没有addtionHeight,仅仅使用100作为判断的标准是不可取的
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top) - addtionHeight;
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
Log.d(tag, "keyboard visible");
} else {
Log.d(tag, "keyboard invisible");
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
//取消监听,由于对Android原理理解不够透彻,防止内存没有释放等问题
if (activityRootView != null && activityRootView.getViewTreeObserver() != null) {
ViewTreeObserver obs = activityRootView.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(mGlobalListener);
} else {
obs.removeGlobalOnLayoutListener(mGlobalListener);
}
}
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
public int getNavigationHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
}
这里还有一点瑕疵,在初次键盘弹出时,打印了两次没有弹出,然后再打印弹出。以后倒是就没有问题了!
参考地址:
http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android