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

时间:2017-12-16 12:32:44

标签: android autocompletetextview

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

1 个答案:

答案 0 :(得分: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;
            }
        });


    }
}