【问题标题】:Close keyboard when scrolling in dropdown on autocompletetextview in Android在Android中的autocompletetextview上滚动下拉菜单时关闭键盘
【发布时间】:2013-05-05 18:33:15
【问题描述】:

我有一个自动完成文本视图,我已将它链接到一个网络服务,因此它会在我输入时向我显示建议。现在,当用户开始滚动自动完成下拉菜单时,如何隐藏软键盘?我浏览了网络,但没有找到任何方法来消除自动完成下拉菜单的内容。

【问题讨论】:

标签: android drop-down-menu autocomplete keyboard hide


【解决方案1】:

我能想出的最佳解决方案是在用户开始滚动列表并再次显示键盘时隐藏键盘,如果用户再次触摸 textview。这几乎适用于大多数操作系统版本和设备,与您可以看到的其他解决方案不同,例如设置 dropDownHeight 的高度。

以下是当用户开始滚动时隐藏键盘的示例代码。基本上,您需要在 AutoCompleteTextView 的适配器中创建一个触摸监听器。

public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewHolder holder;
    if (convertView == null) {
        convertView = inflater.inflate(viewResourceId, parent, false);
        holder = new ViewHolder();
        init(convertView, holder);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    convertView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                InputMethodManager imm = (InputMethodManager) getContext()
                        .getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(
                        searchView.getWindowToken(), 0);
            }

            return false;
        }
    });

    setView(position, holder);
    return convertView;
}

【讨论】:

  • 谢谢,当我用 if (convertView != null) 包装 setOnTouchListener 部分时,代码对我有用
【解决方案2】:

我会将此答案或@ayorhan 的答案作为已接受的答案,这确实是在滚动下拉选择时处理关闭键盘的最佳方式。

这是@ayorhan 的解决方案,用于与 SimpleCursorAdapter 一起使用。我必须制作一个自定义的 SimpleCursorAdapter 类:

public class SimpCursAdap extends SimpleCursorAdapter {

public SimpCursAdap(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
    super(context, layout, c, from, to, flags);

}

public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                InputMethodManager imm = (InputMethodManager) getContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(
                        view.getApplicationWindowToken(), 0);
            }
            return false;
        }
    });
    return view;
   }
}

然后你可以在任何地方实例化这个类:

final SimpleCursorAdapter adapter = new SimpCursAdap(aContext,
            aRowLayout,
            null,
            aColNames,
            aRowViewsIds,
            0);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.setStringConversionColumn(aValueColId);
autocompletetextview.setAdapter(adapter);

【讨论】:

    【解决方案3】:

    如果我理解正确,您希望键盘消失,因为它会为您的下拉列表留出更多空间?也许这是相关的:

    Scrolling drop-down-menu over the keyboard in autocompletetextview

    【讨论】:

      【解决方案4】:

      将此行添加到 XML 中对我来说很好

      这将使键盘在滚动列表后面。

      android:dropDownHeight="wrap_content"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多