隐藏键盘autocompletetextview

时间:2015-11-23 10:20:28

标签: android

我遇到问题,当我尝试从视图中点击(自动完成),我的键盘没有消失,我不知道我做错了什么。

在我的活动hace:autocomplete.setOnItemClickListener到这里onItemClick,当用户点击我打电话时:

InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(parent.getWindowToken(), 0);

...但永远不要隐藏键盘。

我也尝试了而不是工作:

/*autoCompleteTextView1.requestFocus();
 autoCompleteTextView1.requestFocusFromTouch();*/

这是我自动填充的课程自定义

public class InstantAutoComplete extends AutoCompleteTextView  {

    public InstantAutoComplete(Context context) {
        super(context);
    }

    public InstantAutoComplete(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
    }

    public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
        super(arg0, arg1, arg2);
    }

    @Override
    public boolean enoughToFilter() {
        return true;
    }

    private boolean mIsKeyboardVisible;

    @Override
    protected void onFocusChanged(boolean focused, int direction,Rect previouslyFocusedRect) {

        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (getWindowVisibility() == View.GONE) {
            Log.d("InstantAutoComplete", "Window not visible, will not show drop down");
            return;
        }
        if (focused) {
            performFiltering(getText(), 0);
        }
        mIsKeyboardVisible = focused;
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing()) {
            InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            // inputManager.isAcceptingText() will not work because view is still focused.
            if (mIsKeyboardVisible) { // Is keyboard visible?
                // Hide keyboard.
                inputManager.hideSoftInputFromWindow(getWindowToken(), 0);
                mIsKeyboardVisible = false;

                // Consume event.
                return true;
            } else {
                // Do nothing.
            }
        }

        return super.onKeyPreIme(keyCode, event);
    }

}

如何正确关闭键盘?感谢。

1 个答案:

答案 0 :(得分:2)

AutoCompleteTextView中,您可以在点击列表项后隐藏键盘onItemClick

public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) {
     InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
     // whatever else should be done
}