首页 > 解决方案 > 如何使用 inputType = none 清除 AutoCompleteTextView

问题描述

我的 UI 中需要一个下拉菜单。按照我在material.io上找到的说明,我将 AutoCompleteTextView 与inputType="none". 我需要在按下按钮时重置它并再次显示占位符,但如果我使用setText("")它会显示一个空的选择,我想真正清除 AutoCompleteTextView 并再次显示占位符。我怎样才能实现它?

标签: androidkotlinmaterial-components-android

解决方案


试试这个

java中:

 button.setOnClickListener(v->{
            autoCompleteTextView.setText(null);
            //autoCompleteTextView.setText("");// or you can use this
            autoCompleteTextView.setFocusable(false);
        });

这对于Kotlin来说:

button.setOnClickListener { v: View? ->
            autoCompleteTextView.setText(null)
            autoCompleteTextView.isFocusable = false
        }

推荐阅读