键盘隐藏AutoCompleteTextView下拉列表

时间:2016-12-08 10:44:33

标签: android autocompletetextview

我在AppCompatAutoCompleteTextView的底部有DialogFragment

在横向模式下的平板电脑(API 19)上,当建议列表中只有一个元素时,键盘会覆盖下拉列表。当有更多元素时,下拉列表会向上移动,并且工作正常。

在移动设备(API 22)上,即使建议列表中只有一个元素,也没有任何问题,下拉列表始终显示在上方。

我已经将android:windowSoftInputMode="adjustPan|stateHidden"添加到了Manifest中的活动。

如何让下拉列表始终向上或不被键盘覆盖?

3 个答案:

答案 0 :(得分:0)

Work around the below the completionThreshold. Hope it works for you!
<AutoCompleteTextView 
  android:id="@+id/someID" 
  android:layout_width="200dp"
  android:layout_height="wrap_content"
  android:completionThreshold="1" />

autocomplete.setThreshold(2); 

答案 1 :(得分:0)

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

public void setupUI(View view) {

    // Set up touch listener for non-text box views to hide keyboard.
    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard(getActivity());
                return false;
            }
        });
    }

    //If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupUI(innerView);
        }
    }
}

oncreate setupUI(rootView.findViewById(R.id.parent));

中添加此行

答案 2 :(得分:0)

 dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                try {
                    if (view != null) {
                        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
                        if (!imm.hideSoftInputFromWindow(autoCompleteTextView.getWindowToken(), WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)) {
                            //call dialog dismiss code here
                        }
                    }
                } catch (Exception e) {
                    ExceptionUtils.logException(e);
                }
            }
            return false;
        }
    });

hideSoftInputFromWindow在键盘关闭时返回true,否则返回false。所以在第一次回来按下它会关闭键盘,在第二次按下它会进入,如果条件和解雇对话那里

相关问题