热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android开发分享Android:AutoCompleteTextView在没有input文字时显示build议

我正在使用AutoCompleteTextView,当用户点击它时,

我正在使用AutoCompleteTextView ,当用户点击它时,即使它没有文本,我也想显示build议 – 但setThreshold(0)setThreshold(1)完全相同 – 所以用户必须input至less1个字符才能显示的build议。

    这是logging的行为 :“当阈值小于或等于0时,应用阈值1。”

    您可以通过showDropDown()手动显示下拉列表,所以也许您可以安排在需要时显示它。 或者,子类AutoCompleteTextView并重写enoughToFilter() ,所有的时间都返回true

    这是我的课 – 我称之为InstantAutoComplete。 这是AutoCompleteTextView和微调之间的东西。

     import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.widget.AutoCompleteTextView; public class InstantAutoComplete extends AutoCompleteTextView { public InstantAutoComplete(Context context) { super(context); } public InstantAutoComplete(Context arg0, AttributeSet arg1) { super(arg0, arg1); } public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) { super(arg0, arg1, arg2); } @Override public boolean enoughToFilter() { return true; } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (focused && getAdapter() != null) { performFiltering(getText(), 0); } } } 

    像这样在你的xml中使用它:

      

    最简单的方法:

    只要使用setOnTouchListener和showDropDown()

     AutoCompleteTextView text; ..... ..... text.setOnTouchListener(new View.OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event){ text.showDropDown(); return false; } }); 

    当只有一个InstantAutoComplete对象时,Destil的代码工作得很好。 它虽然没有与两个工作 – 不知道为什么。 但是当我把showDropDown() (就像CommonsWarebuild议的那样)放到onFocusChanged()就像这样:

     @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (focused) { performFiltering(getText(), 0); showDropDown(); } } 

    它解决了这个问题。

    这两个答案恰好结合在一起,但我希望有时候可以节省一些。

    上面的Destil的答案几乎适用,但有一个微妙的错误。 当用户第一次把焦点放在工作区域时,如果他们离开,然后返回到这个区域,它将不会显示下拉菜单,因为mPopupCanBeUpdated的值在被隐藏的时候仍然是错误的。 修复方法是将onFocusChanged方法更改为:

     @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (focused) { if (getText().toString().length() == 0) { // We want to trigger the drop down, replace the text. setText(""); } } } 

    适配器最初不执行筛选。
    未执行过滤时,下拉列表为空。
    所以你可能不得不进行最初的过滤。

    为此,您可以在完成添加条目后调用filter()

     adapter.add("a1"); adapter.add("a2"); adapter.add("a3"); adapter.getFilter().filter(null); 

    你可以使用onFocusChangeListener;

     TCKimlikNo.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { TCKimlikNo.showDropDown(); } } }); 

    制作CustomAutoCompleteTextView。 1.覆盖setThreshold,enoughToFilter,onFocusChanged方法

     public class CustomAutoCompleteTextView extends AutoCompleteTextView { private int myThreshold; public CustomAutoCompleteTextView (Context context) { super(context); } public CustomAutoCompleteTextView (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public CustomAutoCompleteTextView (Context context, AttributeSet attrs) { super(context, attrs); } //set threshold 0. public void setThreshold(int threshold) { if (threshold <0) { threshold = 0; } myThreshold = threshold; } //if threshold is 0 than return true public boolean enoughToFilter() { return true; } //invoke on focus protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { //skip space and backspace super.performFiltering("", 67); // TODO Auto-generated method stub super.onFocusChanged(focused, direction, previouslyFocusedRect); } protected void performFiltering(CharSequence text, int keyCode) { // TODO Auto-generated method stub super.performFiltering(text, keyCode); } public int getThreshold() { return myThreshold; } } 

    只需在触摸或单击autoCompleteTextView事件或您想要的位置调用此方法。

     autoCompleteTextView.showDropDown() 

    尝试一下

      searchAutoComplete.setThreshold(0); searchAutoComplete.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {//cut last probel if (charSequence.length() > 1) { if (charSequence.charAt(charSequence.length() - 1) == ' ') { searchAutoComplete.setText(charSequence.subSequence(0, charSequence.length() - 1)); searchAutoComplete.setSelection(charSequence.length() - 1); } } } @Override public void afterTextChanged(Editable editable) { } }); //when clicked in autocomplete text view @Override public void onClick(View view) { switch (view.getId()) { case R.id.header_search_etv: if (searchAutoComplete.getText().toString().length() == 0) { searchAutoComplete.setText(" "); } break; } }): 

    这工作对我来说,伪代码:

      public class CustomAutoCompleteTextView extends AutoCompleteTextView { public CustomAutoCompleteTextView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean enoughToFilter() { return true; } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (focused) { performFiltering(getText(), 0); } } @Override public boolean onTouchEvent(MotionEvent event) { this.showDropDown(); return super.onTouchEvent(event); } } 

    只需将其粘贴到Java中的onCreate方法即可

     final ArrayAdapter arrayAdapter = new ArrayAdapter<>( this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.Loc_names)); textView1 =(AutoCompleteTextView) findViewById(R.id.acT1); textView1.setAdapter(arrayAdapter); textView1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View arg0) { textView1.setMaxLines(5); textView1.showDropDown(); } }); 

    这和你的Xml文件…

      

    并在值…下的string.xml中创build一个数组…

      Pakistan Germany Russia/NCR China India Sweden Australia  

    你很好走。

    七年后,伙计们,这个问题还是一样的。 这里有一个类的function,强制这个愚蠢的popup窗口显示在任何情况下。 所有你需要做的就是设置一个适配器到你的AutoCompleteTextView,添加一些数据,并随时调用showDropdownNow()函数。

    感谢@DavidVávra。 这是基于他的代码。

     import android.content.Context import android.util.AttributeSet import android.widget.AutoCompleteTextView class InstantAutoCompleteTextView : AutoCompleteTextView { constructor(context: Context) : super(context) constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) override fun enoughToFilter(): Boolean { return true } fun showDropdownNow() { if (adapter != null) { // Remember a current text val savedText = text // Set empty text and perform filtering. As the result we restore all items inside of // a filter's internal item collection. setText(null, true) // Set back the saved text and DO NOT perform filtering. As the result of these steps // we have a text shown in UI, and what is more important we have items not filtered setText(savedText, false) // Move cursor to the end of a text setSelection(text.length) // Now we can show a dropdown with full list of options not filtered by displayed text performFiltering(null, 0) } } } 

      以上就是android开发分享Android:AutoCompleteTextView在没有input文字时显示build议相关内容,想了解更多android开发(异常处理)及android游戏开发关注(编程笔记)。


      推荐阅读
      author-avatar
      王晓宁smile
      这个家伙很懒,什么也没留下!
      PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
      Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有