【问题标题】:android how an EditText work as AutoCompleteandroid EditText 如何作为 AutoComplete 工作
【发布时间】:2020-02-29 05:00:06
【问题描述】:

我希望我的 EditText 应该像 AutoComplete 一样工作,因为我写在 XML 文件中

android:inputType="textAutoComplete|textAutoCorrect"

但它不起作用。

我正在使用 API v2.2 并且我的 Activity 扩展了 MapActivity,在那里我放了一个简单的 EditText 和一个名为“搜索”的按钮。因此,如果我们在EditText 中输入位置名称并按下搜索按钮,则意味着它应该在地图中找到该位置。 所以我希望EditText 可以作为AutoComplete 工作。 我该怎么做?

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    只需使用AutoCompleteTextView 而不是普通的EditText

    hello-autocomplete 会有所帮助。

    编辑:上面的链接看起来已经过期。新页面在这里:https://developer.android.com/training/keyboard-input/style#AutoComplete

    【讨论】:

    • 对不起,我首先从谷歌搜索我也有 AutoCompleteTextView。但我想可能还有其他工作可以解决这个问题。这就是我问的原因。
    • @Jyosna - 你为什么想要另一种方式?你试过AutoCompleteTextView吗?它不能解决你的问题吗?
    • 我想要像谷歌搜索引擎一样,无论我在那里写什么,它都应该自动完成。例如,我为 location 所做的,我只是将一些城市名称输入到一个 xml 文件中,并且 AutoCompleteTextView 仅适用于我在 string.xml 中的字符串数组中提到的那些城市名称。但我希望我的 AutoCompleteTextview 应该作为谷歌搜索框工作。我该怎么做,有什么想法吗?
    • 我不知道 Google 是如何准确生成其自动完成的,但我相信它是在统计基础上生成它们的。您可以做的是每次用户搜索某些内容时将搜索查询保存到外部文件(或者您可以使用一些算法来生成更好的建议),并在运行时从该文件加载自动完成字符串。只是 Google Android 文件处理,我相信你会找到很多教程。
    • 这是一个简单易用的教程android.foxykeep.com/dev/… 它还涵盖了通过 WebService 实现的自动完成功能。
    【解决方案2】:

    首先转换您的EditText->AutoCompleteTextView

    然后使用 ArrayAdapter 将您的 XML 文件链接到 AutoCompleteTextView

    假设您创建的 XML string-array 命名为 list_of_countries,那么它可以链接到您的 AutoCompleteTextView,如下所示:

    String[] countries = getResources().getStringArray(R.array.list_of_countries);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,countries);
    actv.setAdapter(adapter);
    

    【讨论】:

      【解决方案3】:

      我使用这个代码:

      1) 在 AndroidManifest.xml 上

      <uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
      

      2) 在 xml 布局中,您必须使用 AutoCompleteTextView 而不是 EditText。

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

      3) 在 Activity 文件上使用它

      private ArrayAdapter<String> getEmailAddressAdapter(Context context) {
          Account[] accounts = AccountManager.get(context).getAccounts();
          String[] addresses = new String[accounts.length];
          for (int i = 0; i < accounts.length; i++) { 
              addresses[i] = accounts[i].name;
          }
          return new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, addresses);
      }
      

      4) onCreate 活动:

      AutoCompleteTextView autoCompleteTextView1 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
      autoCompleteTextView1.setAdapter(getEmailAddressAdapter(this));
      

      【讨论】:

      • 这仅在用户开始输入后显示建议。我也不确定它是否安全关于 android M 中的新权限。
      【解决方案4】:

      默认 ArrayAdapter 仅按第一个字符过滤。如果您还想查看 包含 搜索关键字的单词,则需要使用自定义 ArrayAdapter 并覆盖其 getViewgetFilter 方法。看看我在另一个 StackOverflow 问题中提供的完整解决方案:https://stackoverflow.com/a/37298258/1808829

      一些代码片段:

      public class AutoSuggestAdapter extends ArrayAdapter
      {
          @Override
          public View getView(int position, View convertView, ViewGroup parent)
          {
              // handle view here
          }
      
          @Override
          public Filter getFilter()
          {
             // implement filtering here
          }
      }
      

      【讨论】:

        【解决方案5】:

        此代码用于更改 MultiAutoCompleteTextView 的设置

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,codeKeyWords);
        MultiAutoCompleteTextView autoCompleteTextView1 = (MultiAutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        autoCompleteTextView1.setAdapter(adapter);
        autoCompleteTextView1.setThreshold(1);
        autoCompleteTextView1.setTokenizer(new this.CommaTokenizer());
        

        在该代码下方,用于通过 space char\n 字符分割单词.. (为什么我们需要此代码?因为 Normal multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 就像那样和它通过 ',' 字符分割单词,但是我们的代码可以帮助您通过这些字符 ' ' 和 '\n' 进行分割)

        /**
                 * This simple Tokenizer can be used for lists where the items are
                 * separated by a comma and one or more spaces.
                 */
            public static class CommaTokenizer implements Tokenizer {
                public int findTokenStart(CharSequence text, int cursor) {
                    int i = cursor;
        
                    while (i > 0 && text.charAt(i - 1) != ' ') {
                        i--;
                    }
                    while (i < cursor && text.charAt(i) == '\n') {
                        i++;
                    }
        
                    return i;
                }
        
                public int findTokenEnd(CharSequence text, int cursor) {
                    int i = cursor;
                    int len = text.length();
        
                    while (i < len) {
                        if (text.charAt(i) == '\n') {
                            return i;
                        } else {
                            i++;
                        }
                    }
        
                    return len;
                }
        
                public CharSequence terminateToken(CharSequence text) {
                    int i = text.length();
        
                    while (i > 0 && text.charAt(i - 1) == ' ') {
                        i--;
                    }
        
                    if (i > 0 && text.charAt(i - 1) == ' ') {
                        return text;
                    } else {
                        if (text instanceof Spanned) {
                            SpannableString sp = new SpannableString(text + "\n");
                            TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
                                                    Object.class, sp, 0);
                            return sp;
                        } else {
                            return text + " ";
                        }
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 2012-02-29
          • 2019-01-01
          • 2012-10-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-24
          相关资源
          最近更新 更多