【问题标题】:How to wait for a few seconds between text changed in Android?如何在Android中更改文本之间等待几秒钟?
【发布时间】:2020-08-07 18:05:59
【问题描述】:

我正在使用MaterialChipsInput(尽管我使用的是什么组件并不重要)。我有以下代码:

chipsInput.addChipsListener(new ChipsInput.ChipsListener() {
    @Override
    public void onChipAdded(ChipInterface chip, int newSize) {}

    @Override
    public void onChipRemoved(ChipInterface chip, int newSize) {}

    @Override
    public void onTextChanged(CharSequence text) {
        if (text != null && text.toString().contains(",") && text.toString().length() > 1) {
            final String tag = Utils.capitalizeFully(text.toString().replaceAll(",","").trim());
            if (!(tag.isEmpty())) {
                chipsInput.addChip(tag, null);
            }
        }
    }
});

基本上,每次用户输入逗号时,它都会向chipsInput 添加标签。问题是用户必须以逗号结尾才能将其添加为芯片。我想添加一个 5 秒的计时器,如果 5 秒后没有变化,它将将该芯片添加到chipsInput。最简单的方法是什么?

【问题讨论】:

  • Thread.sleep(5000) in try catch
  • @AniketSahrawat 这行不通,因为onTextChanged 将再次被调用,而您将再次睡觉。稍微改了一下标题,这样更容易理解。
  • 这需要在不同的线程中执行并检查输入。但是,如果您不熟悉并发,请不要这样做,因为这样做弊大于利。
  • 我确实了解线程是如何工作的。问题是ChipsInput 是第三方,所以我不是处理onTextChanged 执行的人。我重写了该方法,并且不知何故我需要关注用户的更改。
  • 使用runnableHandler

标签: java android


【解决方案1】:

它的onTextChanged 是从芯片的EditTextonTextChanged 调用的,正如在库中声明的here 一样,您不能做太多事情。所以,我在这里指出一个使用CountDownTimer 的解决方案。

为什么要使用倒数计时器?因为如果用户在 5 秒内再次键入,您可以将时间重置回 0。您也可以使用Handler 执行此操作,然后像在this answer 中所做的那样再次重置它。

  1. mCountDownTimer 声明为全局变量:

    CountDownTimer mCountDownTimer;
    
  2. countDownFunction() 声明为:

    private void countDownFunction(String chipText) {
        if(mCountDownTimer != null) 
            myCountDownTimer.cancel(); // Cancels the CountDown
    
        mCountDownTimer = new CountDownTimer(5000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //Called after every delay of the above second parameter as a ticking count down
                //Current delay is 1 second as 1000 passed above
            }
    
            @Override
            public void onFinish() {
                //This will only be called on successful five second delay
                chipsInput.addChip(chipText, null); //chipText is the parameter passed to this function
                //You may want to clear the Chip `EditText` here
            }
        };
        mCountDownTimer.start(); // Restarts the CountDown
     }
    
  3. onTextChanged 调用您的countDownFunction() 并将文本传递给它:

     @Override
     public void onTextChanged(CharSequence text) {
         if (text != null && text.toString().length() > 1) {
             if(text.toString().contains(",")){
                 final String tag = Utils.capitalizeFully(text.toString().replaceAll(",","").trim());
                 if (!(tag.isEmpty())) {
                     chipsInput.addChip(tag, null);
                 }
             }
             else
                 countDownFunction(Utils.capitalizeFully(text.toString().trim()));
         }
     }
    

现在,为了改进这一点,您可以使用AsyncTask 或带有runnable 线程的处理程序。有很多方法可以做到这一点,有很多方法可以处理异步操作以确保应用程序顺利运行。

但是,这给了你一个想法——这里发生的事情是每次输入一个没有逗号的文本,,这个函数被调用,如果之前没有启动,它要么开始倒计时,要么重新启动它,因为@倒计时的 987654339@ 只有在超过 5 秒时才会被调用,然后它会添加芯片。

编辑:现在,计时器的重置会更好。你还是可以看看this question

【讨论】:

  • 你太棒了:)
猜你喜欢
  • 1970-01-01
  • 2019-01-07
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
相关资源
最近更新 更多