【发布时间】:2015-10-16 09:29:18
【问题描述】:
我遇到了一个优化问题:我想做一个动态的 AutoCompleteTextView,所以我在我的类中实现了“TextWatcher”。我希望每次用户输入并暂停或完成输入时,我都会启动我的数据库请求以获取自动完成列表,所以我的问题是:
我如何知道用户何时完成/暂停在 AutoCompleteTextView 中输入?
实际上,每次用户输入字符时,我的代码都会发出一个请求。因此,对于“Hello”,它将向我的数据库发送 4 个请求,但我需要它在用户完成输入时发送一个包含完整世界“Hello”的唯一请求。
这可能吗?
这是我实际代码的一部分:
public class MainActivity extends AppCompatActivity implements TextWatcher {
public final static String CLUB_NAME = "com.mycompany.myfirstapp.FAV_CLUB";
List<devaddict.footbarcom.modelPOJO.Object> list;
Button searching;
AutoCompleteTextView searchbar_club;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searching = (Button)findViewById(R.id.buttonseek);
searchbar_club = (AutoCompleteTextView) findViewById(R.id.searchbar_club);
searchbar_club.addTextChangedListener(this);
searchbar_club.setThreshold(3);
}
然后,在同一个“MainActivity”类中,我得到了这些:
@Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
System.out.println("TEXT CHANGED BY onTextChanged");
}
@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
}
@Override
public void afterTextChanged(final Editable s) {
//System.out.println("TEXT CHANGED BY afterTextChanged");
}
我认为“afterTextChanged”方法是我真正想要的,但我看不出它和 onTextChanged 有什么区别。
System.out 仅用于测试。
【问题讨论】:
-
您是否要等到用户在输入第一个单词后输入第一个空格?
-
这可能是一个很好的解决方案@Droidwala,但有时用户只需键入“PSG”之类的首字母缩略词。
标签: android retrofit autocompletetextview textwatcher