如何在用户 finished/paused 打字时使用 TextWatcher?

How to use TextWatcher when user finished/paused typing?

我遇到了一个优化问题:我想做一个动态的 AutoCompleteTextView,所以我对 class 实施了 "TextWatcher"。我希望每次用户键入并暂停或完成键入时,我启动我的数据库请求以获取自动完成列表,所以我的问题是:

我怎么知道用户 finished/paused 何时在 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" class 中,我得到以下论文:

@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仅供测试。

onTextChanged() 是您可以写下逻辑的主要方法。 如果您愿意等待用户输入 2 个或更多单词,您可以编写如下所示的 onTextChanged() 方法:

@Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {

 if(count > 4){
     new Handler().postDelayed(new Runnable(){
          public void run(){
               //write db insertion logic here...
        }},700);
   }
}

基本上你等待 700 毫秒然后执行一些操作以确保用户可能已经停止输入..

看看这是否有帮助!!

P.S: 下面阿里用timer的回答也是OP

问题的正确解决方法
    Timer timer;

    @Override
    public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {

    timer = new Timer();
    timer.schedule(new TimerTask()
    {
        @Override
        public void run()
        {
            MainActivity.activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    timer.cancel();
                    //do something

                }
            });
        }

    }, 1000);//after 1 second do whatever you want

}

@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");
}

以下是使用 Kotlin 和协程的方法: 首先在您的 Activity 或 Fragment 中定义一个作业来执行此操作,如下所示:

private var textChangeCountDownJob: Job = Job()

然后像这样添加一个文本观察器:

editText.doAfterTextChanged {
            textChangeCountDownJob.cancel()
            textChangeCountDownJob = lifecycleScope.launch {
                delay(500) // for half a second delay, as an example
                search(it.toString()) // doing your logic here
            }
        }