【问题标题】:Suggestions not shown in AutoCompleteTextViewAutoCompleteTextView 中未显示的建议
【发布时间】:2016-01-09 11:10:28
【问题描述】:

我的布局中有一个 AutoCompleteTextView。当用户在其中输入“@”字符时,我必须向他们展示一些建议。它通常命名我从互联网上得到它。

我正在获取名称并创建一个 ArrayAdapter,如下所示。

autoCtextView.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            String lsatChar = s.toString().substring(s.length()-1,s.length()); 
            if(lsatChar.equals("@")) {
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(DisplayQuestionDetails.this, 
                         android.R.layout.simple_list_item_1, namesLsist);
                autoCtextView.setAdapter(adapter);
            }


        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

但没有显示建议。我做错什么了吗?请询问是否需要澄清问题

【问题讨论】:

标签: android autocompletetextview


【解决方案1】:

你想念autoCtextView.setThreshold(1); 吗?

(从第一个字符开始)

例如演示:

String[] strList={"a","aaa","aabb","b","bbc","cbb","c","cdd","caa","d","ddc","dda","e","eea","ebc","aec"};  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        //Creating the instance of ArrayAdapter containing list
           ArrayAdapter<String> adapter = new ArrayAdapter<String>  
            (this,android.R.layout.select_dialog_item,strList);  

        //Getting the instance of AutoCompleteTextView  
           AutoCompleteTextView autoCtextView= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);  
           autoCtextView.setThreshold(1);         //to start working from first character  
           autoCtextView.setAdapter(adapter);//set the adapter data to the AutoCompleteTextView  




}  

【讨论】:

    【解决方案2】:

    在声明 autocompleteTextView 后比填充第一个适配器。

    点赞Ref here

    public class CountriesActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.countries);
    
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, COUNTRIES);
         AutoCompleteTextView textView = (AutoCompleteTextView)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter);
     }
    
     private static final String[] COUNTRIES = new String[] {
         "Belgium", "France", "Italy", "Germany", "Spain"
     };
    

    }

    【讨论】:

      【解决方案3】:

      在我的片段布局中,如果我不使用 ems 属性(字段大小以“m”为单位),则不会显示建议。当它存在时,会显示建议。

      <AutoCompleteTextView
              android:id="@+id/enter_activities_text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="100dp"
              android:ems="10"
              android:completionThreshold="1"/>
      

      【讨论】:

      • 这是我的情况。你为我节省了很多时间,伙计
      【解决方案4】:

      这样做:

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(DisplayQuestionDetails.this, android.R.layout.simple_list_item_1, namesLsist);
      autoCtextView.setAdapter(adapter);
      

      之前 autoCtextView.addTextChangedListener(new TextWatcher() {...

      【讨论】:

        【解决方案5】:

        在blog 的帮助下,我制作了自定义自动完成文本框。它在用户输入“@something”时起作用,例如“@f”,然后所有匹配“f”的名称都会出现。

        CustomAutoComplete.java

        public class CustomAutoComplete extends AutoCompleteTextView {
            private String previous = "";
            private String seperator = "@";
        
            public CustomAutoComplete(final Context context, final AttributeSet attrs, final int defStyle) {
                super(context, attrs, defStyle);
                this.setThreshold(0);
            }
        
            public CustomAutoComplete(final Context context, final AttributeSet attrs) {
                super(context, attrs);
                this.setThreshold(0);
            }
        
            public CustomAutoComplete(final Context context) {
                super(context);
                this.setThreshold(0);
            }
        
            /**
             * This method filters out the existing text till the separator
             * and launched the filtering process again
             */
            @Override
            protected void performFiltering(final CharSequence text, final int keyCode) {
                String filterText = text.toString().trim();
                previous = filterText.substring(0, filterText.lastIndexOf(getSeperator()) + 1);
                filterText = filterText.substring(filterText.lastIndexOf(getSeperator()) + 1);
                if (!TextUtils.isEmpty(filterText)) {
                    super.performFiltering(filterText, keyCode);
                }
            }
        
            /**
             * After a selection, capture the new value and append to the existing
             * text
             */
            @Override
            protected void replaceText(final CharSequence text) {
                super.replaceText(previous + text + getSeperator());
            }
        
            public String getSeperator() {
                return seperator;
            }
        
        }
        

        布局文件:

         <com.example.app.CustomAutoComplete
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/autoCompleteTextView"
                android:layout_gravity="center_horizontal|top" />
        

        主活动:

        public class MainActivity extends ActionBarActivity {
            CustomAutoComplete autoCompleteTextView;
            String[] namesLsist = {"zivame","ziooo","zoooO","flipme","flipkart"};
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        
                autoCompleteTextView = (CustomAutoComplete) findViewById(R.id.autoCompleteTextView);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_list_item_1, namesLsist);
                autoCompleteTextView.setAdapter(adapter);
        }
        }
        

        【讨论】:

        • @user965071 如果您发现比这个答案更好的东西,请发布。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-29
        • 1970-01-01
        • 2013-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多