检测软键盘隐藏状态
问题描述:
每次软键盘状态从显示更改为隐藏时,都希望输入Toast
文本.在这里,我只想从EditText
中选择getText()
,每次单击EditText
时,都必须打开软键盘,然后按回车键或返回后,文本必须显示为Toast
Want to Toast
a text everytime when soft keyboard state changes from shown to hidden. Here I just want to getText()
from EditText
and everytime I click on EditText
the soft Keyboard must open and after pressing back or return the text must be shown as Toast
预先感谢
答
没有用于键盘状态检测的直接侦听器,因此您需要以下一些编程实现
There is no direct listener for keyboard state detection so you need some programatic implementation as below
private boolean wasKeyboardOpen = false;
try {
activityMainView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
activityMainView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityMainView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) {
wasKeyboardOpen = true;
// kEYBOARD IS OPEN
} else {
if (wasKeyboardOpen) {
wasKeyboardOpen = false;
// Do your toast here
}
// kEYBOARD IS HIDDEN
}
}
});
} catch (Throwable e) {
e.printStackTrace();
}