键盘遮挡了AutoCompleteTextView下拉菜单

11

我在DialogFragment底部有一个AppCompatAutoCompleteTextView

在平板电脑(API 19)横屏模式下,当建议列表中只有一个元素时,下拉菜单会被键盘覆盖。当有更多元素时,下拉菜单向上展开,运作良好。

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

我已经在清单文件的活动中添加了 android:windowSoftInputMode="adjustPan|stateHidden"

如何使下拉菜单始终向上展开或不被键盘覆盖?

3个回答

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。因此,在第一次后退按下时,它将关闭键盘,在第二次后退按下时,它将进入if条件并在那里解除对话框。


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); 

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));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接