简体   繁体   中英

AutoCompleteTextView Item selection trigger event

I want an error on TextInputLayout to be disabled when an item is selected from AutoCompleteTextView

This is googles documentation on AdapterView.OnItemClickListener. https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener

In my fragment onCreateView method i have

View view = inflater.inflate(R.layout.fragment_currency_picker, container, false);

TextInputLayout mtextInputLayout= view.findViewById(R.id.currencyTIL);

List<String> currenciesAvailable = Arrays.asList(getResources().getStringArray(R.array.currencies_array));

ArrayAdapter adapter = new ArrayAdapter(getContext(), R.layout.dropdown_currency_item, currenciesAvailable);

AutoCompleteTextView autoComplete = view.findViewById(R.id.AutoCompTxtView);
    
autoComplete.setAdapter(adapter);

autoComplete.setOnItemSelectedListener(this);

In my fragment I've also implemented

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Log.d("TEST", "XYZ");
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

I can't seem to trigger onItemSelected. Ultimately I want to call

mtextInputLayout.setErrorEnabled(false);

Use the setOnItemClickListener listener:

autoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        //do something...
    }
});

Since you are using a Material Component Theme the AutoCompleteTextView is replaced at runtime by the MaterialAutoCompleteTextView .

As you can check in the code when an item in the Popup is selected the OnItemClickListener interface is called.

When you click on an item on the adapter it is not a Selection but a click event

use this to remove the Error when the items are clicked by adding mtextInputLayout.setError(null) on the click listener:

autoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mtextInputLayout.setError(null);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM