简体   繁体   English

如何在 Android 中更改的文本之间等待几秒钟?

[英]How to wait for a few seconds between text changed in Android?

I'm using MaterialChipsInput (although it does not matter what is the component I'm using).我正在使用MaterialChipsInput (尽管我使用的是什么组件并不重要)。 I have the following code:我有以下代码:

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

Basically, it add tags to chipsInput every time user enters a comma.基本上,每次用户输入逗号时,它都会将标签添加到chipsInput The problem is that the user will have to end with a comma in order to add it as a chip.问题是用户必须以逗号结尾才能将其添加为芯片。 I would like to add a timer for 5 seconds and if there is no changes after those 5 seconds, it will add that chip to the chipsInput .我想添加一个 5 秒的计时器,如果在 5 秒后没有任何变化,它将将该芯片添加到chipsInput中。 What would be the easiest way to do it?最简单的方法是什么?

Its onTextChanged is called from onTextChanged of the Chip's EditText as declared here in the library to which you cannot do much.它的onTextChanged是从 Chip 的EditTextonTextChanged调用的,正如在库中声明那样,您无法做太多事情。 So, here's I'm pointing out a solution using CountDownTimer .所以,这里我要指出一个使用CountDownTimer的解决方案。

Why a CountDownTimer?为什么要使用倒数计时器? Because You can reset the time back to 0 if user types again under 5 seconds.因为如果用户在 5 秒内再次键入,您可以将时间重置回 0。 You can also do it using a Handler and reset it again as done in this answer .您也可以使用Handler并再次重置它,如本答案中所做的那样。

  1. Declare mCountDownTimer a global variable as: mCountDownTimer声明为全局变量:

     CountDownTimer mCountDownTimer;
  2. Declare countDownFunction() as: 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. Call your countDownFunction() from the onTextChanged and pass the text to it as: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())); } }

Now, to improve this, you can use AsyncTask or Handler with runnable thread.现在,为了改善这一点,您可以将AsyncTask或 Handler 与runnable线程一起使用。 There are many ways to do this, there are many ways to handle the async operation to ensure smooth running app.有很多方法可以做到这一点,有很多方法可以处理异步操作以确保应用程序顺利运行。

But, this gives you the idea - What's happening here is every time a text is entered without comma , , this function is called and it either starts the count down if not previously started or restarts it because of which the onFinish() of the count down will only be called if it passes the 5 second time and then it will add the chip.但是,这给了你一个想法——这里发生的事情是每次输入没有逗号的文本, ,这个 function 被调用,如果之前没有启动,它要么开始倒计时,要么重新启动它,因为计数的onFinish() down 只有在超过 5 秒时才会被调用,然后它会添加筹码。

Edit: Now, the resetting of the timer will work better.编辑:现在,计时器的重置会更好。 You can still look at this question .你仍然可以看看这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM