gpt4 book ai didi

android - hideSoftInputFromWindow 不起作用?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:26:31 27 4
gpt4 key购买 nike

对于某些 EditText View ,我想使用自定义键盘而不是键盘。

问题是当我第一次点击 EditText 时,两个键盘都会显示。当我第二次点击时 - 软键盘终于消失了。

这种行为的原因可能是什么?

这是我使用的代码:

package pkleczek.profiwan.keyboards;

import android.app.Activity;
import android.inputmethodservice.KeyboardView;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public abstract class CustomKeyboard {

/** A link to the KeyboardView that is used to render this CustomKeyboard. */
protected KeyboardView mKeyboardView;
/** A link to the activity that hosts the {@link #mKeyboardView}. */
protected Activity mHostActivity;

/** Returns whether the CustomKeyboard is visible. */
public boolean isCustomKeyboardVisible() {
return mKeyboardView.getVisibility() == View.VISIBLE;
}

/**
* Make the CustomKeyboard visible, and hide the system keyboard for view v.
*/
public void showCustomKeyboard(View v) {
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);

if (v != null) {
InputMethodManager inputManager = (InputMethodManager) mHostActivity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}

/** Make the CustomKeyboard invisible. */
public void hideCustomKeyboard() {
mKeyboardView.setVisibility(View.GONE);
mKeyboardView.setEnabled(false);
}

/**
* Register <var>EditText<var> with resource id <var>resid</var> (on the
* hosting activity) for using this custom keyboard.
*
* @param resid
* The resource id of the EditText that registers to the custom
* keyboard.
*/
public void registerEditText(int resid) {
EditText edittext = (EditText) mHostActivity.findViewById(resid);

edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
showCustomKeyboard(v);
} else {
hideCustomKeyboard();
}
}
});

edittext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showCustomKeyboard(v);
}
});

edittext.setInputType(edittext.getInputType()
| InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
}

最佳答案

尝试在 inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); 行之前将以下条件放入 InputMethodManager 代码中:

    if (inputManager!=null) {
Activity activity = getActivity();
if (acvivity == null)
return;
if (activity.getCurrentFocus() == null)
return;
if (activity.getCurrentFocus().getWindowToken() == null)
return;
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

我在 ListFragment 中使用它来隐藏默认键盘。首先,当我按下 EditText 并显示键盘时,当打开 onScrollStateChanged 时,我将其隐藏。

您也可以尝试将 InputMethodManager 代码放在 EditText 的 onClickListener 中,然后调用您的 showCustomKeyboard() 方法。

if (v != null)后面放一个Else语句和Log,也许你的View vnull

关于android - hideSoftInputFromWindow 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21573586/

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