当软键盘打开时,我想要滚动视图向下滚动到底部.
为此,我可以使用: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 /以这种方式使用活动(一个好地方在onCreate):
KeyboardUtil.setKeyboardVisibilityListener(this, mKeyboardVisibilityListener);
根据这篇文章以及关于android-developers的这篇文章,它似乎不可能做你想要的.您可能希望重新检查您的用例以了解您正在做的事情.也许其中一个softInputMode
标志对你有用.
看着日期,可能你有一个问题的解决方案,否则:
以下是我对另一个相关问题的回答:有没有办法判断是否显示了软键盘?
但我在这里复制完整的回复以避免死链接:
请检查您的活动的配置更改
这适用于您的AndroidManifest.xml
这适用于您的Activity类http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)
您将需要@Override您的Activity的onConfigurationChanged(android.content.res.Configuration)的公共方法,以便能够处理,例如,这个值:
hardKeyboardHidden,
keyboard,
keyboardHidden
对于所有可能的值,请查看http://developer.android.com/reference/android/content/res/Configuration.html
你会看到类似这样的东西:
HARDKEYBOARDHIDDEN_NO HARDKEYBOARDHIDDEN_UNDEFINED HARDKEYBOARDHIDDEN_YES KEYBOARDHIDDEN_NO KEYBOARDHIDDEN_UNDEFINED KEYBOARDHIDDEN_YES KEYBOARD_12KEY KEYBOARD_NOKEYS KEYBOARD_QWERTY KEYBOARD_UNDEFINED
在那里你可以阅读这样的东西:
public int hardKeyboardHidden A flag indicating whether the hard keyboard has been hidden. public int keyboard The kind of keyboard attached to the device. public int keyboardHidden A flag indicating whether any keyboard is available.
更新:
这是我正在谈论的具体样本:
http://developer.android.com/guide/topics/resources/runtime-changes.html
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Checks the orientation of the screen if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); } // Checks whether a hardware keyboard is available if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) { Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show(); } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) { Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show(); } }
我希望这对你有帮助