Android AutoCompleteTextView 只适用于第一个词?

Android AutoCompleteTextView only works for the first word?

我正在尝试使用自动完成文本,但 运行 遇到了一个小问题。

当我尝试输入国家/地区中的第一个词时,它工作正常, 但是如果我尝试输入任何其他内容然后其中一个国家没有任何反应,例如:"ger"(将给我德国)和:"hello ger"(不会给德)

如果我只单击一次回车并从第二行开始,也会发生同样的事情,它只适用于第一个单词和第一行。此外,如果我确实将它用作第一个单词并尝试再次使用它,例如 Germany(来自自动完成),然后键入 ger,没有会弹出。

知道如何让它适用于文本的其余部分或如何控制它(将其设置为在所选内容或行中的位置之后或之后吗?

代码:

 AutoCompleteTextView et1;
    private static final String[] COUNTRIES = new String[] {
            "Belgium", "France", "Italy", "Germany", "Spain"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        et1=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
        et1.setAdapter(adapter);
        et1.setThreshold(0);
    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.asaf1.workersreports.Main3Activity">

    <AutoCompleteTextView
        android:text=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/autoCompleteTextView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:textAlignment="viewEnd"
        android:gravity="right|top"/>
</RelativeLayout>

提高您的阈值以获得建议... 如果要在输入 1st character 后搜索,则

et1.setThreshold(1);

如果要在2nd character输入后搜索,

et1.setThreshold(2);

您必须创建自定义 AutoCompleteTextView。在 Adapter 中实施过滤器 class 并覆盖 performFilterring() 方法。在这种方法中,您必须编写一些与结果匹配的代码。在您的情况下,您必须检查输入字符串是否包含您的字符串列表之一。

检查使用自定义适配器的链接: Custom AutoCompleteTextView behaviorAutoCompleteTextView backed by CursorLoader