Close keyboard when scrolling in dropdown on autocompletetextview in Android

旧时模样 提交于 2020-01-01 03:22:06

问题


I have an autocompletetextview which I have linked it to a webservice so it shows me suggestions as I type. Now how can I hide the soft keyboard when the user starts scrolling through the autocomplete dropdown? I looked through the net but didnt find any method to detech touches on the autocomplete dropdown.


回答1:


The best solution I could come up for this, is hiding the keyboard when the user starts scrolling the list and showing the keyboard again, if the user touches on the textview again. This pretty much works for most of the OS versions and devices, unlike other solutions you could see, such as setting the height of the dropDownHeight.

Below is a sample code to hide the keyboard, when the user starts scrolling. Basically, you need to create a touch listener in your AutoCompleteTextView's adapter.

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



回答2:


I would take this answer, or @ayorhan's answer as accepted answer, it really is the best way to handle dismissing the keyboard when scrolling a dropdown selection.

This is a play off @ayorhan's solution, for use with a SimpleCursorAdapter. I had to make a custom SimpleCursorAdapter class:

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

Then you can instantiate the class anywhere:

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:


If I understand correctly, you want the keyboard to disappear because it would leave more room for your drop down list? Perhaps this is related:

Scrolling drop-down-menu over the keyboard in autocompletetextview




回答4:


Adding this line into XML works fine to me

This will make the keyboard behind the scrolling list.

android:dropDownHeight="wrap_content"


来源:https://stackoverflow.com/questions/16493017/close-keyboard-when-scrolling-in-dropdown-on-autocompletetextview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!