gpt4 book ai didi

Android:检测软键盘打开

转载 作者:IT老高 更新时间:2023-10-28 22:12:03 26 4
gpt4 key购买 nike

当软键盘打开时,我希望 ScrollView 向下滚动到底部。

为此我可以使用:fullScroll(View.FOCUS_DOWN);

但是在软键盘打开事件触发后如何触发该命令?

最佳答案

这是我的解决方案:

1/一个简单的界面

public interface KeyboardVisibilityListener {
void onKeyboardVisibilityChanged(boolean keyboardVisible);
}

2/一个实用方法(把它放在你想要的地方,例如在一个名为 KeyboardUtil 的类中)

public static void setKeyboardVisibilityListener(Activity activity, KeyboardVisibilityListener keyboardVisibilityListener) {
View contentView = activity.findViewById(android.R.id.content);
contentView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
private int mPreviousHeight;

@Override
public void onGlobalLayout() {
int newHeight = contentView.getHeight();
if (mPreviousHeight != 0) {
if (mPreviousHeight > newHeight) {
// Height decreased: keyboard was shown
keyboardVisibilityListener.onKeyboardVisibilityChanged(true);
} else if (mPreviousHeight < newHeight) {
// Height increased: keyboard was hidden
keyboardVisibilityListener.onKeyboardVisibilityChanged(false);
} else {
// No change
}
}
mPreviousHeight = newHeight;
}
});
}

3/以这种方式从 Activity 中使用(一个好地方在 onCreate):

KeyboardUtil.setKeyboardVisibilityListener(this, mKeyboardVisibilityListener);

关于Android:检测软键盘打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3081276/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com