英文:
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的EditText
的onTextChanged
方法中调用的,如此处所声明的链接所示,这是库中无法更改的。因此,这里我提出了一种使用CountDownTimer
的解决方案。
为什么要使用CountDownTimer?因为如果用户在5秒内再次输入,您可以将时间重置为0。您也可以使用Handler
来完成,然后再次重置它,就像在这个答案中所做的那样。
- 声明
mCountDownTimer
为全局变量:
CountDownTimer mCountDownTimer;
- 声明
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(); // 重新启动计时器
}
- 从
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.
-
Declare
mCountDownTimer
a global variable as :CountDownTimer mCountDownTimer;
-
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 }
-
Call your
countDownFunction()
from theonTextChanged
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论