Java “方向”设置为“纵向”时隐藏键盘
我最接近的是:Java “方向”设置为“纵向”时隐藏键盘,java,android,rotation,keyboard,Java,Android,Rotation,Keyboard,我最接近的是: @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { InputMethodManager inputMethodManager = (I
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(input.getWindowToken(), 0);
}
//Some other code
}
但这不起作用。在这种情况下,即使没有输入具有焦点,一旦方向更改为纵向,键盘也将被拉起。使用此UTI侦听器代码
import android.app.Activity;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
public class KeyboardUtil implements OnGlobalLayoutListener {
static public interface KeyboardListener {
public void onKeyboardVisible(boolean visible);
}
private int mHeight;
private boolean mVisible;
private View mRootView;
private KeyboardListener mListener;
public KeyboardUtil(Activity activity) {
mVisible = false;
mHeight = 0;
if (activity == null) {
return;
}
try {
mRootView = activity.getWindow().getDecorView()
.findViewById(android.R.id.content);
mRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setListener(KeyboardListener listener) {
this.mListener = listener;
}
@Override
public void onGlobalLayout() {
if (mHeight == 0) {
mHeight = mRootView.getMeasuredHeight();
return;
}
if (mListener == null) {
return;
}
int height = mRootView.getHeight();
if (!mVisible && mHeight > (height + 100)) {
mVisible = true;
mListener.onKeyboardVisible(mVisible);
mHeight = height;
} else if (mVisible && mHeight < (height - 100)) {
mVisible = false;
mListener.onKeyboardVisible(mVisible);
mHeight = height;
}
}
}
使用此util侦听器代码
import android.app.Activity;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
public class KeyboardUtil implements OnGlobalLayoutListener {
static public interface KeyboardListener {
public void onKeyboardVisible(boolean visible);
}
private int mHeight;
private boolean mVisible;
private View mRootView;
private KeyboardListener mListener;
public KeyboardUtil(Activity activity) {
mVisible = false;
mHeight = 0;
if (activity == null) {
return;
}
try {
mRootView = activity.getWindow().getDecorView()
.findViewById(android.R.id.content);
mRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setListener(KeyboardListener listener) {
this.mListener = listener;
}
@Override
public void onGlobalLayout() {
if (mHeight == 0) {
mHeight = mRootView.getMeasuredHeight();
return;
}
if (mListener == null) {
return;
}
int height = mRootView.getHeight();
if (!mVisible && mHeight > (height + 100)) {
mVisible = true;
mListener.onKeyboardVisible(mVisible);
mHeight = height;
} else if (mVisible && mHeight < (height - 100)) {
mVisible = false;
mListener.onKeyboardVisible(mVisible);
mHeight = height;
}
}
}
试试这个
视图=this.getCurrentFocus()
试试这个
视图=this.getCurrentFocus()
代替0,你试试这个?-->InputMethodManager.HIDE\u不总是在没有键盘可用时弹出键盘?在我的例子中,如果屏幕上已经有键盘,它就会工作。它会被移除,并且不会弹出键盘。是的,即使没有输入有焦点,键盘也会打开。你对mainifest文件使用过这个权限吗?android:WindowsOfInputMode=“StateAllwaysVisible”是的,但不是作为权限-而是在
中。如果不使用0,请尝试此操作?-->InputMethodManager.HIDE\u不总是在没有键盘可用时弹出键盘?在我的例子中,如果屏幕上已经有键盘,它就会工作。它会被移除,并且不会弹出键盘。是的,即使没有输入有焦点,键盘也会打开。你对mainifest文件使用过这个权限吗?android:WindowsOfInputMode=“StateAllwaysVisible”是的,但不是作为权限-而是在
中。我不确定该如何使用它。该类将在main活动之后编写。当我希望键盘关闭时,是否需要写入第二个块代码?我不确定该如何使用此块代码。该类将在main活动之后编写。当我想关闭键盘时,是否需要写入第二个块代码?
try {
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
} catch (Exception e) {
e.printStackTrace();
}