是否有可能有一個AutoCompleteTextView,它尋找一個字符串,但建議一個替代字符串?Android AutoCompleteTextView建議替代
例如,如果用戶鍵入'Yau'或'yauh',它會提示'Yáuh',因此用戶不需要鍵入特殊字符。
是否有可能有一個AutoCompleteTextView,它尋找一個字符串,但建議一個替代字符串?Android AutoCompleteTextView建議替代
例如,如果用戶鍵入'Yau'或'yauh',它會提示'Yáuh',因此用戶不需要鍵入特殊字符。
還有的AutoCompleteTextView,所以我想這就是你需要:
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MyAutoCompleteTextView" >
</AutoCompleteTextView>
這裏的官方信息:AutoCompleteTextView
我剛看到甚至還有一個教程,Tutorial of AutoCompleteTextView
我的天堂試過這個,但這肯定會做...
你還沒有指定你的數據是在一個數組還是數據庫中,所以我將要假設一個數組(或一個列表)。
覆蓋適配器的getFilter()
。 (查看this question看看如何)
現在,在該例中(在鏈接中),作者使用startsWith
來篩選結果。相反,你將不得不使用自己的比較方法來考慮這些特殊字符。 (如,等同á
與a
)
我在擴展ArrayAdapter了個去,但它deosn't似乎覆蓋:
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
public class foreignAdapter<T> extends ArrayAdapter implements Filterable {
String temp;
String word;
private List<T> mObjects;
private final Object mLock = new Object();
private ArrayList<T> mOriginalValues;
public foreignAdapter(Context context, int resource, T[] object) {
super(context, resource, object);
}
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (mLock) {
mOriginalValues = new ArrayList<T>(mObjects);
}
}
if (prefix == null || prefix.length() == 0) {
synchronized (mLock) {
ArrayList<T> list = new ArrayList<T>(mOriginalValues);
results.values = list;
results.count = list.size();
}
}
else {
String prefixString = prefix.toString().toLowerCase();
final ArrayList<T> values = mOriginalValues;
final int count = values.size();
final ArrayList<T> newValues = new ArrayList<T>(count);
for (int i = 0; i < count; i++) {
final T value = values.get(i);
final String valueText = value.toString().toLowerCase();
/* Run for each character in the string */
for(int j=0;j<valueText.length();j++)
{
switch(valueText.charAt(j))
{
case 'á':
case 'ā':
case 'à':
temp+='a';
break;
case 'í':
case 'ī':
case 'ì':
temp+='i';
break;
case 'è':
case 'ē':
case 'é':
temp+='e';
break;
case 'ó':
case 'ō':
case 'ò':
temp+='o';
break;
case 'ú':
case 'ū':
case 'ù':
temp+='u';
break;
case 'ģ':
temp+='g';
break;
default:
temp+=word.charAt(j);
break;
}
}
// First match against the whole, non-splitted value
if (temp.startsWith(prefixString)) {
newValues.add(value);
} else {
final String[] words = temp.split(" ");
final int wordCount = words.length;
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString)) {
newValues.add(value);
break;
}
}
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//noinspection unchecked
mObjects = (List<T>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
如果你想讓它搜索這工作正常一個詞的一部分,並提出整個詞,但這不是我想要做的。 – Chimeara
如果你想要修改正在寫入的內容,那麼我敢說你必須編寫自己的函數(如果string ==「Yau」,那麼textview =「Yáuh」) – ArcDare
@ArcDan,不,它不是一個簡單的轉換,因爲用戶可能想要「Yauh」,「Yáuh」,「Yāuh」或「Yàuh」。所以我想要的是這些在自動完成中列爲建議。看起來沒有簡單的方法來做到這一點,我會考慮擴展AutoCompleteTextView – Chimeara