2011-11-16 54 views
4

是否有可能有一个AutoCompleteTextView,它寻找一个字符串,但建议一个替代字符串?Android AutoCompleteTextView建议替代

例如,如果用户键入'Yau'或'yauh',它会提示'Yáuh',因此用户不需要键入特殊字符。

回答

-1

还有的AutoCompleteTextView,所以我想这就是你需要:

<AutoCompleteTextView 
     android:id="@+id/autoCompleteTextView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="MyAutoCompleteTextView" > 
</AutoCompleteTextView> 

这里的官方信息:AutoCompleteTextView

我刚看到甚至还有一个教程,Tutorial of AutoCompleteTextView

+0

如果你想让它搜索这工作正常一个词的一部分,并提出整个词,但这不是我想要做的。 – Chimeara

+0

如果你想要修改正在写入的内容,那么我敢说你必须编写自己的函数(如果string ==“Yau”,那么textview =“Yáuh”) – ArcDare

+0

@ArcDan,不,它不是一个简单的转换,因为用户可能想要“Yauh”,“Yáuh”,“Yāuh”或“Yàuh”。所以我想要的是这些在自动完成中列为建议。看起来没有简单的方法来做到这一点,我会考虑扩展AutoCompleteTextView – Chimeara

0

我的天堂试过这个,但这肯定会做...

你还没有指定你的数据是在一个数组还是数据库中,所以我将要假设一个数组(或一个列表)。

覆盖适配器的getFilter()。 (查看this question看看如何)

现在,在该例中(在链接中),作者使用startsWith来筛选结果。相反,你将不得不使用自己的比较方法来考虑这些特殊字符。 (如,等同áa

1

我在扩展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(); 
      } 
     } 
     } 
    } 
相关问题