hideSoftInputFromWindow不工作?

Paw*_*zek 14 android

对于某些EditText视图,我想使用自定义键盘而不是键盘.

问题是当我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);
    }
}
Run Code Online (Sandbox Code Playgroud)

tax*_*axo 8

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

    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);
            }
Run Code Online (Sandbox Code Playgroud)

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

您还可以尝试将InputMethodManager代码放在EditTextonClickListener中,然后调用showCustomKeyboard()方法.

放一个Else语句并在之后登录if (v != null),也许你的View vnull.