how can I create clean Button in AutoCompleteTextView

后端 未结 3 666
轻奢々
轻奢々 2021-01-14 01:32

How can I create clean Button in AutoCompleteTextView, when I click clear button I want to clear all text in AutoCompleteTextView, see in picture

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-14 01:59

    Try this:

    activity_main.xml:

    
    
        
    
        

    MainActivity.java:

    public class MainActivity extends AppCompatActivity {
    
        AutoCompleteTextView text;
        Button clear;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            text = (AutoCompleteTextView) findViewById(R.id.text);
    
            clear = (Button) findViewById(R.id.clear);
    
            clear.setVisibility(View.INVISIBLE);
    
            //close button visibility for manual typing
            text.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void afterTextChanged(Editable s) {
                    //do nothing
                }
    
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    //do nothing
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(s.length() != 0) {
                        clear.setVisibility(View.VISIBLE);
                    } else {
                        clear.setVisibility(View.GONE);
                    }
                }
            });
    
            //sample array
            String[] array = {"alpha", "beta", "cupcake", "donut", "eclair", "froyo", "gingerbread", "honeycomb", "icecreamsandwich", "jellybean", "kitkat", "lollipop", "marshmallow"};
    
            ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,array);
    
            text.setThreshold(1);       //will start working from first character
    
            text.setAdapter(adapter);   //setting the adapter data into the AutoCompleteTextView
    
            //Shows drop down list on touch
            text.setOnTouchListener(new View.OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event){
                    text.showDropDown();
                    return false;
                }
            });
    
            //close button visibility for autocomplete text view selection
            text.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
                @Override
                public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
                    clear.setVisibility(View.VISIBLE);
                }
    
                @Override
                public void onNothingSelected(AdapterView arg0) {
                    clear.setVisibility(View.GONE);
                }
    
            });
    
        }
    
        public void clear(View view) {
            text.setText("");
            clear.setVisibility(View.GONE);
        }
    }
    

提交回复
热议问题