简体   繁体   English

Android:如果x秒钟内未更改EditText,则调用函数

[英]Android: Call function if EditText has not been changed in x seconds

I have this app I'm working on, which sends requests to a server whenever text changes in the EditText. 我正在使用此应用程序,每当EditText中的文本更改时,该应用程序都会将请求发送到服务器。 And it works fine as it is. 它可以正常工作。

The problem is, that if I send a request every time the edittext changes, it will be a lot of requests in the end. 问题是,如果每次edittext更改时我发送一个请求,最终都会有很多请求。 And I don't want to use a "send" button. 而且我不想使用“发送”按钮。

I want something the works something like this: 我想要一些像这样的作品:

If the user types in the edittext, some timer goes on. 如果用户键入edittext,则计时器将继续运行。 And if the timer reaches x seconds, it executes the request. 并且如果计时器达到x秒,它将执行请求。 But the timer resets if the user types again. 但是如果用户再次键入,计时器将重置。

In this way, the request is only sent, once the user has stopped typing. 这样,仅在用户停止键入后才发送请求。

You might know some better way, and I'd be glad to hear it. 您可能知道更好的方法,我很高兴听到它。

Thanks in advance :D 提前致谢

One method would be to use myEditText.addTextChangedListener(...) and use a TextWatcher to know when the text changes. 一种方法是使用myEditText.addTextChangedListener(...)并使用TextWatcher知道文本何时更改。

Then in the onTextChanged() method you can post a delayed runnable using a Handler to send the current text to your server if they don't type anything further. 然后,在onTextChanged()方法中,您可以使用Handler发布延迟运行的消息,以将当前文本发送到您的服务器(如果他们没有进一步输入任何内容)。

Here is what this roughly looks like: 这大致是这样的:

private Handler handler = new Handler();
private Runnable postToServerRunnable = new Runnable() {
    @Override
    public void run() {
        // TODO: PUT CODE HERE TO HANDLE CURRENT VALUE OF EDIT TEXT AND SEND TO SERVER
    }
};

private TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void afterTextChanged(Editable arg0) {}

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // remove existing callback (timer reset)
        handler.removeCallbacks(postToServerRunnable);
        // 500 millisecond delay. Change to whatever delay you want.
        handler.postDelayed(postToServerRunnable, 500);
    }
};

There is this is method called EditText.edittext.addtextchangedlistener . 这就是称为EditText.edittext.addtextchangedlistener方法。 add this listener to your edittext and override the methods like this. 将此侦听器添加到您的edittext中,并覆盖此类方法。

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

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
         called after the The complete Text is changed// this is what i think you should use
        }

I hope this is helpful . 我希望这是有帮助的 。 ThankYou. 谢谢。

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

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