简体   繁体   中英

Android: don't show soft keyboard in AutocompleteTextView

I have an AutocompleteTextView to select a train station, which is using two different adapters :

ADAPTER 1 : containing a fixed list of recent and nearby stations ( DOESN'T require a soft keyboard for filtering by typing)
ADAPTER 2 : containing the cursor to the sqlite database of stations ( DOES require a soft keyboard for filtering by typing)

So, I would like to prevent the soft keyboard from showing when the AutocompleteTextView gets the focus and the ADAPTER 1 is used, but I haven't found a way yet.

I am currently using this, but the soft keyboard keeps popping up:
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);

I finally found a way!
On the onFocusChange you first set the focus to its parent and after that you call showDropDown() to display the dropdown. In this way the keyboard doesn't pop up, because the AutocompleteTextView doesn't have the focus!

    setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) {
                setAdapter(Adapter1);
                ((ViewGroup)getParent()).setFocusableInTouchMode(true);
                ((ViewGroup)getParent()).requestFocus();
                showDropDown();
            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM