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

huangapple go评论64阅读模式
英文:

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

问题

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

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` 中。最简单的方法是什么?

  [1]: https://github.com/pchmn/MaterialChipsInput
英文:

I'm using MaterialChipsInput (although it does not matter what is the component I'm using). 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. 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. What would be the easiest way to do it?

答案1

得分: 2

它的onTextChanged方法是从Chip的EditTextonTextChanged方法中调用的,如此处所声明的链接所示,这是库中无法更改的。因此,这里我提出了一种使用CountDownTimer的解决方案。

为什么要使用CountDownTimer?因为如果用户在5秒内再次输入,您可以将时间重置为0。您也可以使用Handler来完成,然后再次重置它,就像在这个答案中所做的那样。

  1. 声明mCountDownTimer为全局变量:
CountDownTimer mCountDownTimer;
  1. 声明countDownFunction()如下:
private void countDownFunction(String chipText) {
    if(mCountDownTimer != null) 
        mCountDownTimer.cancel(); // 取消计时器

    mCountDownTimer = new CountDownTimer(5000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            // 每次以上面第二个参数的延迟调用后执行
            // 当前延迟是1秒,因为上面传递了1000
        }

        @Override
        public void onFinish() {
            // 只有在成功的五秒延迟后才会调用此方法
            chipsInput.addChip(chipText, null); // chipText是传递给此函数的参数
            // 您可能想在这里清除Chip `EditText`
        }
    };
    mCountDownTimer.start(); // 重新启动计时器
}
  1. 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线程的Handler。有许多方法可以处理异步操作,以确保应用程序平稳运行。

但是,这个解决方案给您一个想法 - 每次输入文本时,如果没有逗号,,则调用此函数,它要么启动计时器,如果之前未启动,要么重新启动计时器,因此只有在超过5秒的时间后,计时器的onFinish()方法才会被调用,然后它将添加Chip。

编辑: 现在,重置计时器将工作得更好。您仍然可以查看这个问题

英文:

Its onTextChanged is called from onTextChanged of the Chip's EditText as declared here in the library to which you cannot do much. So, here's I'm pointing out a solution using CountDownTimer.

Why a CountDownTimer? Because You can reset the time back to 0 if user types again under 5 seconds. You can also do it using a Handler and reset it again as done in this answer.

  1. Declare mCountDownTimer a global variable as :

    CountDownTimer mCountDownTimer;
    
  2. Declare countDownFunction() as :

    private void countDownFunction(String chipText) {
        if(mCountDownTimer != null) 
            mCountDownTimer.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 :

     @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. 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.

Edit : Now, the resetting of the timer will work better. You can still look at this question.

huangapple
  • 本文由 发表于 2020年8月8日 02:05:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63307103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定