How to disable AutoCompleteTextView's drop-down from showing up?

前端 未结 17 1975
终归单人心
终归单人心 2020-12-05 15:30

I use the following code to set text to an AutoCompleteTextView field. But I noticed that when I set certain text (not all text, but some) to it, it will automatically pop u

相关标签:
17条回答
  • 2020-12-05 15:52

    The intent here is to hide the drop-down with AutoCompleteTextView. Set the dropDownHeight to zero in the layout file.

    <!--Skipping all other attributes for simplicity-->
    <AutoCompleteTextView
            android:id="@+id/address_bar"
            android:dropDownHeight="0dp"
            />
    

    In my case, I have the GridView and there is address bar above that view. I want to filter items in GridView based upon user input in AutoCompleteTextView without requiring TextView to show me the dropdown.

    0 讨论(0)
  • 2020-12-05 15:56

    In AppCompatAutoCompleteTextView can not setTreshold(). If use AppCompatAutoCompleteTextView class, can try this:

    actv.setText("Tim Hortons");
    actv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actv.setText("");
            }
        });
    
    0 讨论(0)
  • 2020-12-05 15:57

    wwyt, I simply reused your trick with removing the Adapter and these are the bare essentials to unfocus/dismiss the dropdown.

    AutoCompleteTextView tvSuburbs;
    ArrayAdapter<Suburb> a = (ArrayAdapter<Suburb>) tvSuburbs.getAdapter();
    tvSuburbs.setAdapter(null); // Remove the adapter so we don't get a dropdown
    tvSuburbs.setText(s.name); // when text is set programmatically.
    tvSuburbs.setAdapter(a); // Restore adapter
    
    0 讨论(0)
  • 2020-12-05 15:58
    act.post(new Runnable() 
    {
      @Override
      public void run()
      {
        act.dismissDropDown();
      }
    });
    

    Works just fine!

    0 讨论(0)
  • 2020-12-05 16:00

    To answer my own question in case someone had the same problem:

    One characteristics of AutoCompleteTextView is that if you change its text programmatically, it will drop down the selection list if the following two conditions are met: 1. It has focus; 2. The list is longer than 30-something items.

    This behavior is actually, IMHO, a design flaw. When the program sets text to an AutoCompleteTextView, it would mean that the text is already correct, there is no point to popup the filtered list for user to further choose from.

    actv.setText("Tim Hortons"); 
    actv.setSelection(0, actv.getText().length()); 
    actv.requestFocus(); 
    actv.dismissDropDown();    // doesn't help 
    

    In the above code, requestFocus() forces the ACTV to get the focus, and this causes the drop-down to pop up. I tried not to reqeuest focus, instead, I called clearFocus() after setting text. But the behavior is very .... unnatural. dissmissDropdown() doesn't help because .... I don't know, it just doesn't help. So, after much strugle, I came up with this work-around:

    1. When initializing the widget, I remembered the adapter in a class field.
    2. Change the above code to:

      mAdapter = (ArrayAdapter<String>)actv.getAdapter(); // mAdapter is a class field        
      actv.setText("Tim Hortons"); 
      actv.setSelection(0, actv.getText().length()); 
      actv.setAdapter((ArrayAdapter<String>)null); // turn off the adapter
      actv.requestFocus();
      Handler handler = new Handler() {
      public void handleMessage(Message msg) {
          ((AutoCompleteTextView)msg.obj).setAdapter(mAdapter);
          };
      Message msg = mHandler.obtainMessage();
      msg.obj = actv;
      handler.sendMessageDelayed(msg, 200);   // turn it back on after 200ms
      

    Here the trick is set the ACTV's adapter to null. Because there is no adapter, of course the system will not pop up the drop-down. But the message will reset the adapter back to the ACTV after the programmed delay of 200ms, and the ACTV will work normally as usual.

    This works for me!

    0 讨论(0)
  • 2020-12-05 16:01

    Try it in XML...

    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:autoText="false"
    />
    
    0 讨论(0)
提交回复
热议问题