2011-03-31 49 views
11

我使用下面的代码将文本设置为AutoCompleteTextView字段。但是我注意到,当我将某些文本(并非全部文本,但部分文本)设置为该文本时,它会自动弹出下拉菜单。如果我不要求重点,那会更好,但更好,并非完全正确。我试过dissmissDropDwon(),它没有帮助。那么,在设置文本并关注它之后,是否有任何方法可以阻止下拉显示?如何禁用AutoCompleteTextView的下拉菜单显示?

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

谢谢!

回答

11

要回答的情况下,一个人自己的问题有同样的问题:

AutoCompleteTextView的

一个特点是,如果您以编程方式改变它的文本,它会如果满足以下两个条件,则下拉选择列表:1.它具有焦点; 2.该列表超过30个项目。

此行为实际上,恕我直言,设计缺陷。当程序将文本设置为AutoCompleteTextView时,这意味着文本已经正确,没有意义弹出过滤列表供用户进一步选择。

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

在上面的代码,requestFocus()方法强制ACTV获取焦点,这将导致下拉弹出。我尝试不要求重点,相反,我在设置文本后调用clearFocus()。但行为非常不自然。 dissmissDropdown()没有帮助,因为....我不知道,它只是没有帮助。所以,经过很多努力之后,我想出了这个解决方法:

  1. 初始化小部件时,我在类字段中记住了适配器。
  2. 更改上面的代码:

    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 
    

这里的技巧是设置ACTV的适配器为null。因为没有适配器,当然系统不会弹出下拉菜单。但该消息将在200ms的编程延迟后将适配器重置回ACTV,并且ACTV将照常正常工作。

这适用于我!

+0

这对我有效;我不需要设置程序化延迟。 – squeeish 2015-09-04 02:46:11

1

尝试在XML ...

<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:autoText="false" 
/> 
+0

感谢。它确实让事情变得更好,弹出下拉菜单的案例更少。但仍不能完全解决问题。但是,谢谢! – wwyt 2011-03-31 04:48:29

10

您也可以启用/禁用下拉像这样:

// disable 
actv.setDropDownHeight(0); 
// enable 
actv.setDropDownHeight(LayoutParams.WRAP_CONTENT); 
+0

超级想法!!!! +1 – IronBlossom 2016-05-02 16:28:57

16

你可以尝试以下步骤:

  1. 当设置文本,也阈值设置为一个较大价值,以便下拉不会来。

    actv.setThreshold(1000); 
    
  2. 然后重写OnTouch以将阈值重新设置为1。

    actv.setOnTouchListener(new OnTouchListener() { 
           @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         actv.setThreshold(1); 
         return false; 
        } 
    }); 
    
+0

这是否阻止搜索实际完成?或者它只是隐藏下拉? – Mauker 2017-10-01 02:09:32

16

另一个解决方案是设置文本之前清除焦点:

mContactTxt.setFocusable(false); 
mContactTxt.setFocusableInTouchMode(false); 
mContactTxt.setText("");    
mContactTxt.setFocusable(true); 
mContactTxt.setFocusableInTouchMode(true); 
1

我也面临着一个场景,我以这种方式解决。

  1. 以静态方式(Ex.POJO)存储要显示的值。
  2. 检查存储的静态变量是否为空且不为空。
  3. 如果其不为空/不为空/其长度大于0,则为该autocompleteTextView设置dismissDropDown()

请找到下面的代码片段

if(null != testText && testText.length() != 0) { 
    mAutoCompleteSearch.setText(incomingActivity.toString()); 
    mAutoCompleteSearch.dismissDropDown(); //Dismiss the drop down 
    } else { 
    mAutoCompleteSearchDocketActivity.setText(""); 
      // Here it(drop down) will be shown automatically 
    } 

希望,这将帮助别人,干杯!

0

dismissDropDown()在适配器效果很好:

 SimpleCursorAdapter autoCompleteAdapter = new SimpleCursorAdapter(this, 
        android.R.layout.select_dialog_item, null, 
        new String[] { ContactsContract.Contacts.DISPLAY_NAME }, 
        new int[] { android.R.id.text1 }, 0); 
     mSearchView.setAdapter(autoCompleteAdapter); 
     autoCompleteAdapter.setFilterQueryProvider(new FilterQueryProvider() { 
      @Override 
      public Cursor runQuery(CharSequence constraint) { 
       mSearchView.dismissDropDown(); 
       // return your query code 
      } 
     }); 

希望这会有所帮助。

2

wwyt,我只是重复使用你的诀窍,删除适配器,这些都是最基本的要点,以忽略/关闭下拉菜单。

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

我已经解决了同样的问题与此代码:

contacts_search.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       // TODO Auto-generated method stub 
       contacts_search.dismissDropDown();  
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 
       contacts_search.dismissDropDown(); 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       // TODO Auto-generated method stub 
       contacts_search.dismissDropDown(); 
      } 
     }); 

这里,contacts_search是我AutoCompleteTextView

3

滑稽把戏。工程100%:)

tv.setThreshold(Integer.MAX_VALUE); 
tv.setText(station.getName()); 
tv.setThreshold(1); 
+0

确实很有趣,但是达到了目的。 – 2016-09-30 15:18:33

3
act.post(new Runnable() 
{ 
    @Override 
    public void run() 
    { 
    act.dismissDropDown(); 
    } 
}); 

作品就好了!

1

您可以尝试

searchInput.setAdapter((ArrayAdapter<String>) null); 
searchInput.setText(text); 
searchInput.setAdapter(adapter); 

Source

4

也许是晚了,但我发现这个问题优雅的解决方案:

禁用过滤设置文本后启用它(而不是玩焦点或/和延迟)。在这种情况下,您应该使用自定义控件。

见下面的例子:

public class CustomCompliteTextView extends AutoCompleteTextView { 

    private boolean mIsSearchEnabled = true; 

    public CustomCompliteTextView(Context context) { 
     super(context); 
    } 

    public CustomCompliteTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomCompliteTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public void setSearchEnabled(boolean isEnabled) { 
     mIsSearchEnabled = isEnabled; 
    } 

    @Override 
    protected void performFiltering(CharSequence text, int keyCode) { 
     if (mIsSearchEnabled) { 
      super.performFiltering(text, keyCode); 
     } 
    } 
} 

与用法:

text.setSearchEnabled(false); 
    text.setText("Text you want to set"); 
    // optional, if you also want to set correct selection 
    text.setSelection(text.getText().length()); 
    text.setSearchEnabled(true); 
+1

非常好的+1 – 2016-08-12 12:37:21

0

我需要添加一些延迟做这件事情的工作。

这为我工作:

new Handler().postDelayed(new Runnable() { 
    public void run() { 
     if(carModel.isPopupShowing()) 
      carModel.dismissDropDown(); 
}}, 500); 
0

这里的目的是为了隐藏与AutoCompleteTextView下拉。在布局文件中将dropDownHeight设置为零。

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

对我而言,我有GridView,并且在该视图上方有一个地址栏。我想根据AutoCompleteTextView中的用户输入过滤GridView中的项目,而不需要TextView向我显示下拉列表。

1
setText("someText",false) 

false意味着它未过滤