Ale*_*pin 1 android android-edittext
我需要你的帮助.我有EditText字段,它充当搜索字段,用于搜索列表中的许多项目.现在我使用TextWatcher的afterTextChanged(Editable s)方法,但它对我来说并不完美.快速输入和擦除后的某些时候,下一个搜索过程不涉及用户输入的所有文本.原因是在漫长的搜索过程中,我不能缩短它.在我的情况下,我需要知道,wnen用户完全输入他的输入,但afterTextChanged()处理每个符号更改.我会很感激任何想法.谢谢!
我猜你正在使用a,TextWatcher因为你想进行实时搜索.在这种情况下,您无法知道用户何时完成输入,但您可以限制搜索的频率.
这是一些示例代码:
searchInput.addTextChangedListener(new TextWatcher()
{
Handler handler = new Handler();
Runnable delayedAction = null;
@Override
public void onTextChanged( CharSequence s, int start, int before, int count)
{}
@Override
public void beforeTextChanged( CharSequence s, int start, int count, int after)
{}
@Override
public void afterTextChanged( final Editable s)
{
//cancel the previous search if any
if (delayedAction != null)
{
handler.removeCallbacks(delayedAction);
}
//define a new search
delayedAction = new Runnable()
{
@Override
public void run()
{
//start your search
startSearch(s.toString());
}
};
//delay this new search by one second
handler.postDelayed(delayedAction, 1000);
}
});
Run Code Online (Sandbox Code Playgroud)
知道输入是否已经结束的唯一方法是用户按Enter或搜索按钮等.您可以使用以下代码监听该事件:
searchInput.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction( TextView v, int actionId, KeyEvent event)
{
switch (actionId)
{
case EditorInfo.IME_ACTION_SEARCH:
//get the input string and start the search
String searchString = v.getText().toString();
startSearch(searchString);
break;
default:
break;
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
只需确保添加android:imeOptions="actionSearch"到EditText布局文件中即可.
| 归档时间: |
|
| 查看次数: |
6845 次 |
| 最近记录: |