【问题标题】:AutoCompleteTextView drop down not showing weirdlyAutoCompleteTextView 下拉菜单没有奇怪地显示
【发布时间】:2016-05-02 09:42:23
【问题描述】:

建议存储在ArrayList<String>. getSuggestions() 方法返回String 的正确列表,关键是我将特殊字符替换为随意字符。 IE。当我输入xeres 时,我希望得到与输入xérès 相同的结果,但问题是当我输入xérès 时,dropdown 出现,而​​当我输入xeres,它没有,即使建议变量具有相同的值...

@Override
 public void onTextChanged(CharSequence s, int start, int before, int count) {
     if (s.length() > 2) {
        suggestions = getSuggestions(s.toString());
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), R.layout.dropdown_textview_row, suggestions);
        autoTxtSearchPointer.setAdapter(adapter);
    }
}

感谢您的帮助! :)

编辑:更精确的代码

    autoTxtSearchPointer = new AutoCompleteTextViewCustom(this);
    autoTxtSearchPointer.setHint(R.string.search);
    autoTxtSearchPointer.setSingleLine();
    autoTxtSearchPointer.setImeOptions(EditorInfo.IME_ACTION_DONE);
    autoTxtSearchPointer.setLayoutParams(editParams);
    autoTxtSearchPointer.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
    autoTxtSearchPointer.setThreshold(2);
    autoTxtSearchPointer.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() > 2) {
                suggestions = getSuggestions(s.toString());
                ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), R.layout.dropdown_textview_row, suggestions);
                autoTxtSearchPointer.setAdapter(adapter);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {}
    });

}

编辑 2:是因为我使用了 Normalizer 吗?

private String normalize(String str) {
        return Normalizer
            .normalize(str, Normalizer.Form.NFKD)
            .replaceAll("[^\\p{ASCII}]", "");
}

【问题讨论】:

    标签: android android-arrayadapter autocompletetextview textwatcher unicode-normalization


    【解决方案1】:

    切勿将 EdittestonTextChanged 用于此目的。

    使用下面的代码:

    AutoCompleteTextView text;
    
    text = (AutoCompleteTextView)findViewById(R.id.text);
    
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,your_array_list);
    text.setAdapter(adapter);
    

    XML

    <AutoCompleteTextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"            
            android:ems="10"
            android:textColor="#000000" />
    

    【讨论】:

    • 请用我添加的代码重新考虑你的答案,我不为这个对象使用xml。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多