本文介绍了AutoCompleteTextView项目选择触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从AutoCompleteTextView中选择一个项目时,禁用TextInputLayout上的错误

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

这是Google在AdapterView.OnItemClickListener上的文档. https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener

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

在我的片段 onCreateView 方法中

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) {

}

我似乎无法触发onItemSelected.最终我想校准

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

mtextInputLayout.setErrorEnabled(false);

mtextInputLayout.setErrorEnabled(false);

推荐答案

当您单击适配器上的项目时,它不是选择项而是单击事件

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

通过在单击侦听器上添加 mtextInputLayout.setError(null)来单击项目时使用此错误来消除错误:

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

            }
        });

这篇关于AutoCompleteTextView项目选择触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 15:10
查看更多