当没有项目匹配AutocompleteTextView的建议列表时,我想要一条消息。如果我使用addOnTextChangeListener,那么它会一直显示消息,用户键入一个char,但只有在找不到匹配项时才需要它。所以请告诉我如何做到这一点,列表来自Web服务。感谢你。
searchEdit = (AutoCompleteTextView)findViewById(R.id.searchEdit);
searchEdit.setCursorVisible(false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, menuItems);
searchEdit.setAdapter(adapter);
searchEdit.addTextChangedListener(this);
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!searchEdit.isPerformingCompletion()) {
Toast.makeText(getApplicationContext(), "No Item Found", 2000).show();
return;
}
}
答案 0 :(得分:2)
也许我的答案迟了,但我已经用这种方式解决了问题:
searchEdit.addTextChangedListener(watcher);
TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(s.length() > 2) {
if (!searchEdit.isPopupShowing()) {
Toast.makeText(getApplicationContext(),"No results", Toast.LENGTH_SHORT).show();
return;
}
}
}
};
答案 1 :(得分:0)
试试这个:你把toast进入afterTextChanged()函数
searchEdit = (AutoCompleteTextView)findViewById(R.id.searchEdit);
searchEdit.setCursorVisible(false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, menuItems);
searchEdit.setAdapter(adapter);
searchEdit.addTextChangedListener(this);
@Override
public void afterTextChanged(Editable arg0) {
if (!searchEdit.isPerformingCompletion()) {
Toast.makeText(getApplicationContext(), "No Item Found", 2000).show();
return;
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
答案 2 :(得分:0)
autosearch.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
autosearch.getLayoutParams().width = JNConstants.WIDTH_35;
rel_layer.setVisibility(View.VISIBLE);
} else {
if(!autosearch.isPerformingCompletion()){
if( jnApp.getSearch_result().size()==0)
{
Log.v("mytag", "search_results_none");
Toast t = Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.search_results_none), Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
return;
}
}
autosearch.getLayoutParams().width = JNConstants.WIDTH_15;
}
}
});
答案 3 :(得分:0)
刚刚碰到同样的问题。敬酒是蹩脚的,显示相同的下拉适配器比建议更酷,但只有一个项目告诉“没有建议”或类似的东西。例如,这就是谷歌地图应用程序的工作方式。
此解决方案有两个时刻。首先,如果您已经拥有手机上的所有项目,则只需更换适配器过滤器即可。在我的情况下,每个用户输入都会产生Http请求,返回建议列表或空列表。所以在这里我是如何做到的
List<AddressItem> cities = event.getCities();
if(cities.isEmpty()) {
AddressItem placeholder = AddressItem.placeholder(getContext());
cities.add(placeholder);
}
AddressItemAdapter addressItemAdapter = new AddressItemAdapter(getContext(), R.layout.simple_text_view, cities);
autoCompleteCity.setAdapter(addressItemAdapter);
autoCompleteCity.showDropDown();
progressCity.setVisibility(View.GONE);
这是占位符项的静态方法占位符()。 第二个时刻是这个项目不应该是可点击的。所以你需要在你的适配器方法中检查它的id ...
@Override
public boolean isEnabled(int position) {
return items.get(position).getId == INVALID_ID;
}
要使其正常工作,您的适配器应在此方法中添加正确的ID
public long getItemId(int position) {
return mItems.get(position).getId();
}
你占位符的id字段应该等于INVALID_ID。 我希望我的解释能引导你进行博大比用户体验,而不是这些祝酒。