Android - AutoCompleteTextView 在调用 setText 后不显示
Android - AutoCompleteTextView not showing after setText is called
我在使用 AutoCompleteTextView
时遇到了一个奇怪的问题。
我有一个 AutoCompleteTextView
可以在输入时显示城市建议。
通过 JSON
从远程服务器检索城市列表。当我使用软键盘或软键盘上的 Mic
按钮时,建议工作正常。 AutoCompleteTextView
确实显示了建议的城市。
但是,当我尝试使用 myAutoCompleteTextView.setText("Chi")
设置文本时遇到问题,自动完成不显示..
我也试过 myAutoCompleteTextView.append("Chi")
但还是不行..
适配器是有的,只是建议没有显示。
有什么建议吗?
谢谢。
是的,你是对的,AutocompleteTextview
中有一个错误,使用 setText("");
方法显示默认建议。
但是您可以通过添加更多代码行来实现此目的,如下所示。
autoText.postDelayed(new Runnable() {
@Override
public void run() {
autoText.showDropDown();
}
},500);
autoText.setText("chi");
autoText.setSelection(autoText.getText().length());
Biraj Zalavadia 的答案有效,但您必须在 Runnable 中写信给 "settext"。
像这样:
mACTextViewEmail.postDelayed(new Runnable() {
@Override
public void run() {
mACTextViewEmail.showDropDown();
mACTextViewEmail.setText("My text is here");
mACTextViewEmail.setSelection(mACTextViewEmail.getText().length());
}
},500);
是由于过滤,
不需要任何额外的代码来管理它,我发现它以非常简单和有效的方式。
autoText.setText("Default Value here",false)
autoText.setSelection(autoText.text.count()) // kotlin
根据文档第二个参数,您可以传递以进行过滤。
boolean:如果为 false,则不会执行此调用的结果过滤。
我搜索了一下,发现这个解决方案非常有效
Look at this issue
fun AutoCompleteTextView.showDropdown(adapter: ArrayAdapter<String>?) {
if(!TextUtils.isEmpty(this.text.toString())){
adapter?.filter?.filter(null)
}
}
在kotlin语言中,可以使用这个扩展函数。
我在使用 AutoCompleteTextView
时遇到了一个奇怪的问题。
我有一个 AutoCompleteTextView
可以在输入时显示城市建议。
通过 JSON
从远程服务器检索城市列表。当我使用软键盘或软键盘上的 Mic
按钮时,建议工作正常。 AutoCompleteTextView
确实显示了建议的城市。
但是,当我尝试使用 myAutoCompleteTextView.setText("Chi")
设置文本时遇到问题,自动完成不显示..
我也试过 myAutoCompleteTextView.append("Chi")
但还是不行..
适配器是有的,只是建议没有显示。
有什么建议吗?
谢谢。
是的,你是对的,AutocompleteTextview
中有一个错误,使用 setText("");
方法显示默认建议。
但是您可以通过添加更多代码行来实现此目的,如下所示。
autoText.postDelayed(new Runnable() {
@Override
public void run() {
autoText.showDropDown();
}
},500);
autoText.setText("chi");
autoText.setSelection(autoText.getText().length());
Biraj Zalavadia 的答案有效,但您必须在 Runnable 中写信给 "settext"。 像这样:
mACTextViewEmail.postDelayed(new Runnable() {
@Override
public void run() {
mACTextViewEmail.showDropDown();
mACTextViewEmail.setText("My text is here");
mACTextViewEmail.setSelection(mACTextViewEmail.getText().length());
}
},500);
是由于过滤, 不需要任何额外的代码来管理它,我发现它以非常简单和有效的方式。
autoText.setText("Default Value here",false)
autoText.setSelection(autoText.text.count()) // kotlin
根据文档第二个参数,您可以传递以进行过滤。
boolean:如果为 false,则不会执行此调用的结果过滤。
我搜索了一下,发现这个解决方案非常有效 Look at this issue
fun AutoCompleteTextView.showDropdown(adapter: ArrayAdapter<String>?) {
if(!TextUtils.isEmpty(this.text.toString())){
adapter?.filter?.filter(null)
}
}
在kotlin语言中,可以使用这个扩展函数。