2017-12-16 11 views
0

사용자가 autocompleteTextview 인 앱을 만들고 있습니다. 사용자가 아무것도 입력하지 않은 경우 서버에서 제안하는 내용이 addTextChangedListener 이지만 여러 번 사용자가 제안을 선택하여 직접 입력하거나 이동하지 않을 수도 있습니다. 하지만 사용자는 제안 된 항목 만 선택하면됩니다.자동 완성 텍스트 뷰 제안 만 선택하는 방법

+2

가능한 복제 [AutoCompleteTextView에 만 옵션을 제안 허용 (https://stackoverflow.com/questions/18467012/autocompletetextview-allow- only-suggested-options) –

+0

그게 작동하지 않습니다. 다른 ans을 의심하거나 자신의 글을 쓰십시오. – Kishor

+0

@Kishor가 내 대답을 확인 했습니까? –

답변

1

출력 제거 버튼을 클릭 한 후

enter image description here

enter image description here

난 내 이전 프로젝트에서이 같은 구현이 내가 당신의 시도에 대한 데모를 만들 수 있습니다이 :

,363,210

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; 
      } 
     }); 


    } 
}