gpt4 book ai didi

java - 当方向设置为纵向时隐藏键盘

转载 作者:太空宇宙 更新时间:2023-11-04 13:24:29 25 4
gpt4 key购买 nike

我最接近的是:

@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
}

但这行不通。在这种情况下,即使没有输入焦点,一旦方向更改为纵向,键盘也会被拉起。

最佳答案

使用此实用程序监听器代码

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;
}
}
}

以下代码在您的 Activity 中使用

new KeyboardUtil(context).setListener(keyboardListener);

KeyboardUtil.KeyboardListener keyboardListener = new KeyboardUtil.KeyboardListener() {
@Override
public void onKeyboardVisible(boolean visible) {
// get configuration here and
if(config is portrait)
hideSoftKeyboard(this); // Provide you activity context
}
};

public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

关于java - 当方向设置为纵向时隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32796963/

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