hideSoftInputFromWindow不工作?

时间:2014-02-05 09:44:32

标签: android

对于某些EditText次观看,我想使用自定义键盘而不是 soft

问题是我第一次点击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);
    }
}

2 个答案:

答案 0 :(得分:7)

尝试在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 语句和日志,也许您的View v null

答案 1 :(得分:0)

Rotemmiz 在主题Close/hide the Android Soft Keyboard中提供的解决方案为我工作。

摘要:

public void setEditTextFocus(EditText editText, boolean isFocused)
{
    editText.setCursorVisible(isFocused);
    editText.setFocusable(isFocused);
    editText.setFocusableInTouchMode(isFocused);

    if (isFocused)
    {
        editText.requestFocus();
    }
}