【问题标题】:Auto Complete Hint with Material in Android在 Android 中使用 Material 自动完成提示
【发布时间】:2021-06-28 06:23:45
【问题描述】:

我在material-text-field 中阅读了文档,并且在 editText 上有 auto-complete-textView 作为提示而不是下拉菜单。你可以看到下面的图片。但我做不到。我尝试了一些带有 textinputlayout 和 material-auto-complete-text 的代码

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til"
        ..>
               
    <com.google.android.material.textfield.MaterialAutoCompleteTextView
       .../>


</com.google.android.material.textfield.TextInputLayout>

在片段中

    ArrayList<String> items = new ArrayList<>();
    items.add("Material");
    items.add("Design");
    items.add("Components");

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.item, items);
    TextInputLayout textInputLayout = findViewById(R.id.til);

并设置适配器

((MaterialAutoCompleteTextView) textInputLayout.getEditText()).setAdapter(adapter);
    ((MaterialAutoCompleteTextView) textInputLayout.getEditText()).setText(adapter.getItem(1),false);

但是 auto-complete-textview 提示下拉菜单中的项目而不是 editText 作为提示。

我想要AutoC完整,因为“AutoC”必须是文本,“完整”必须是提示

我该怎么做??

【问题讨论】:

  • 这是材料组件库中的正常行为

标签: android kotlin autocompletetextview material-components-android


【解决方案1】:

编辑:

您正在寻找以下内容吗? (它将根据用户输入设置提示。但这不能用于实际的自动完成,因为据我所知,您无法更改键盘上的建议):

TextInputEditText editText = ((TextInputEditText) textInputLayout.getEditText());
editText.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) {}

    @Override
    public void afterTextChanged(Editable s) {
        try {
            String userInput = s.toString();
            userInput = userInput.trim().toLowerCase();

            String match = null;
            for (String item : items) {
                if (item == null) continue;
                String fItem = item.trim().toLowerCase();
                if (fItem.startsWith(userInput)) {
                    match = item;
                    break;
                }
            }
            if (match == null) editText.setHint(""); // Insert your default hint text here
            else editText.setHint(match);
        } catch (Throwable tr) {
            tr.printStackTrace();
        }
    }
});

以前的

将以下内容作为属性添加到您的 TextInputLayout:

style="@style/Widget.MaterialComponents.TextInputLayout.*.ExposedDropdownMenu"

您是否也正确实现了您的 list_item.xml(您将其称为“item”(代码中的 R.layout.item))?


官网上是这么写的:

在布局中:

<com.google.android.material.textfield.TextInputLayout
    ...
    style="@style/Widget.MaterialComponents.TextInputLayout.*.ExposedDropdownMenu">
​
    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"
    />
​
</com.google.android.material.textfield.TextInputLayout>

在代码中:

val items = listOf("Material", "Design", "Components", "Android")
val adapter = ArrayAdapter(requireContext(), R.layout.list_item, items)
(textField.editText as? AutoCompleteTextView)?.setAdapter(adapter)

在项目布局(list_item.xml)中:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:textAppearance="?attr/textAppearanceSubtitle1"
/>

(https://material.io/components/text-fields/android#using-text-fields)

【讨论】:

  • 感谢您的信息,但这不是我要问的。这些代码打开下拉菜单。但我想在编辑文本上自动完成文本作为提示,如上图。 AutoComplete -- AutoC 必须是文本并且“omplete”必须是提示
  • 哦,对不起,我误解了你的句子(“但自动完成文本视图建议下拉菜单上的项目而不是 editText 作为提示。”)。我认为自己无法在键盘上设置自动完成结果,但如果您只希望提示起作用,您可以添加一个 textWatcher 并根据可能的输入列表检查用户输入,如果用户输入匹配列表项之一的开头,然后相应地设置提示
  • @FurkanPaşaoğlu 我编辑了我的答案。现在是你需要的吗
  • 谢谢@IntoVoid。很棒的算法,但有一个问题。当我在editText中输入文本时,提示将消失。当我删除文本时,提示将可见。所以我不能同时显示文字和提示
  • @FurkanPaşaoğlu 这是真的。您可以在其后面添加另一个 textView 并将提示文本设置到其中。您可以为此使用RelativeLayout
猜你喜欢
  • 2021-06-05
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 2011-09-14
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多