如何仅选择自动完成的textview建议

问题描述 投票:0回答:1

我正在制作一个用户autocompleteTextview的应用程序。当用户通过addTextChangedListener键入任何内容时,建议来自服务器,但很多时候用户可能不是选择建议并直接键入并移动但我希望该用户只选择建议的项目。

android autocompletetextview
1个回答
1
投票

产量

enter image description here

点击删除按钮后

enter image description here

我已经在我以前的项目中实现了这一点,我已经为您尝试创建了这个:

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user31.demosforstack.AutocomplateTextVIew">

    <AutoCompleteTextView
        android:id="@+id/autoComplateText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search here" />

</LinearLayout>

JAVA文件

package com.example.user31.demosforstack;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import java.util.ArrayList;

public class AutocomplateTextVIew extends AppCompatActivity {

    AutoCompleteTextView autoComplateText;
    ArrayAdapter arrayAdapter;
    ArrayList<String> arrayList;
    boolean isSelect = false;

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

        arrayList = new ArrayList();
        for (int i = 0; i < 10; i++) {
            arrayList.add("item " + i);
        }

        autoComplateText = findViewById(R.id.autoComplateText);
        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, arrayList);
        autoComplateText.setAdapter(arrayAdapter);
        autoComplateText.setThreshold(2);

        autoComplateText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String item = (String) parent.getAdapter().getItem(position);
                isSelect = true;

                autoComplateText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.remove, 0);

                Log.e("Item: ", item);
                autoComplateText.setInputType(InputType.TYPE_NULL);

                ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(autoComplateText.getWindowToken(), 0);

            }
        });

        autoComplateText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int DRAWABLE_RIGHT = 2;

                if (event.getAction() == MotionEvent.ACTION_UP) {

                    if (autoComplateText != null){
                        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(autoComplateText.getWindowToken(), 0);

                        if (event.getRawX() >= (autoComplateText.getRight() - autoComplateText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                            autoComplateText.setText("");
                            autoComplateText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                            isSelect = false;
                            return true;
                        }
                    }



                }

                return false;
            }
        });


    }
}
© www.soinside.com 2019 - 2024. All rights reserved.