私はあなたの助けが必要です。EditTextフィールドがあります。これは、リスト内の多くのアイテムを検索するための検索フィールドとして機能します。現在、TextWatcherのafterTextChanged(Editable s)メソッドを使用していますが、これは私には完璧ではありません。高速入力および消去後の次の検索プロセスには、ユーザーが入力したすべてのテキストが含まれない場合があります。その理由は検索プロセスが長いため、短くすることはできません。私の場合、知っておく必要があります。wnenユーザーは入力をすべて終了しますが、afterTextChanged()はすべての符号の変更を処理します。どんなアイデアでもありがたいです。ありがとう!
7216 次
3 に答える
7
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);
}
});
入力が終了したかどうかを知る唯一の方法は、ユーザーがエンターまたは検索ボタンなどを押すことです。次のコードを使用して、そのイベントをリッスンできます。
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;
}
});
レイアウト ファイルにを必ず追加android:imeOptions="actionSearch"
してください。EditText
于 2012-06-05T16:26:38.473 に答える
1
必要なのは TextWatcher です
http://developer.android.com/reference/android/text/TextWatcher.html
于 2012-06-05T15:38:21.263 に答える
0
私が通常それを行う方法は、使用することですonFocusChange
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// Do your thing here
}
}
});
これには、ユーザーが edittext フィールドから離れなければならないという欠点が 1 つあります。
于 2012-06-05T16:31:20.563 に答える