AutoCompleteTextView устанавливает текст в наиболее вероятный элемент - PullRequest
0 голосов
/ 14 сентября 2018

Я использую по умолчанию AutoCompleteTextView , который я создаю в моем layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <AutoCompleteTextView
            android:id="@+id/autoComplete"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_weight="1"
            android:ems="10"
            android:hint="title"
            android:imeOptions="actionNext"
            android:singleLine="true"/>

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:gravity="center"
            android:inputType="numberDecimal"
            android:imeOptions="actionDone"
            android:singleLine="true"/>
</RelativeLayout>

Чем я инициализирую AutoCompleteTextView и добавляю адаптер. В моем случае я использую IME_ACTION_NEXT в качестве события для установки текста AutoCompleteTextView .

MainActivity.java :

public class MainActivity extends AppCompatActivity {
    private ArrayAdapter<String> adapter_autocomplete_tags;
    String[] tags = new String[]{"food", "restaurant", "drink"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        adapter_autocomplete_tags = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item, tags);

        final AutoCompleteTextView autocomplete = (AutoCompleteTextView) dialog_layout.findViewById(R.id.autoComplete);
        autocomplete.setThreshold(1);
        autocomplete.setAdapter(adapter_autocomplete_tags);
        autocomplete.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_NEXT){
                    //here I want to set the AutoCompleteTextView´s Text to the most likely value
                }
                return false;
            }
        });
    }
}

Моя цель состоит в том, чтобы, если я наберу "f" и выполнил IME_ACTION_NEXT, текст AutoCompleteTextView будет установлен в "food" (чтобы установить текст в первое значение раскрывающегося списка).

Я уже пытался использовать:

autocomplete.performCompletion(); //does nothing

И

CharSequence s = autocomplete.getCompletionHint(); //returns null
autocomplete.setText(s);

Конечно, я мог бы воссоздать алгоритм проверки AutoCompleteTextView , но я хочу использовать его самостоятельно.

Edit: Я решил воссоздать алгоритм AutoCompleteTextView.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...