在Android应用程序中管理延迟

时间:2011-04-25 07:53:00

标签: android autocomplete

嘿伙计们,我想知道如何管理Android应用程序中的延迟,例如,我在onTextChanged()上有一个重写方法。与此相关,我想设置延迟,如.5秒,以便最终确定用户在自动填充文本框中输入的内容。如果用户在.5秒内挂起/停止输入,我想在我的代码中执行某个方法或实现(即我自己的自动填充文本框中的我自己的过滤方案/逻辑,只是为了减少我的应用程序中的资源使用,谢谢)。 / p>

这是我的示例代码:

protected AutoCompleteTextView autoCompleteView;
protected AutoCompleteAdapter suggsAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    countries = getResources().getStringArray(R.array.countries_array);    

    autoCompleteView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);  

    TextWatcher textChecker = new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

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

//here inside this overridden method, I wanted to create a delay in order to lessen the
//use of resources.

            String enteredText = autoCompleteView.getText().toString();             
            refreshList(enteredText);

        }
    };        


    autoCompleteView.addTextChangedListener(textChecker);

}

例如,用户键入“Lond”(对于像“伦敦”这样的字符串),每个字母键入的时间小于.5秒,我希望调用所有以前的onTextChanged()方法(键入“L”时调用方法, “o”和“n”)被忽略,只有输入最后一个字母时的onTextChanged()才会被执行。

我该怎么做,请帮帮我:(。

3 个答案:

答案 0 :(得分:4)

您可以创建Handler子类,并在触发Handler.sendEmptyMessageDelayed()时调用Handler.sendMessageDelayed()onTextChanged()方法。您可以使用Handler.removeMessages()从邮件队列中删除邮件。您应该使用Handler.handleMessage()方法处理邮件。因此,您的onTextChanged()方法将类似于:

mHandler.removeMessages(MESSAGE_TEXT_CHANGED);
mHandler.sendEmptyMessage(MESSAGE_TEXT_CHANGED, 500);

编辑:以下是代码示例。我还没有测试过,所以我不确定它是否有效。

private static final int AUTOCOMPLETE_DELAY = 500;
private static final int MESSAGE_TEXT_CHANGED = 0;

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (msg.what == MESSAGE_TEXT_CHANGED) {
            String enteredText = (String)msg.obj;
            refreshList(enteredText);
        }
    }
};

// ... your code here

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    String enteredText = autoCompleteView.getText().toString();             
    mHandler.removeMessages(MESSAGE_TEXT_CHANGED);
    final Message msg = Message.obtain(mHandler, MESSAGE_TEXT_CHANGED, enteredText);
    mHandler.sendMessageDelayed(msg, AUTOCOMPLETE_DELAY);
}

答案 1 :(得分:0)

我正在编写自动完成的运行代码。

首先会调用 onTextChanged

       public void onTextChanged(CharSequence s, int start, int before, int count) {
           String newText = s.toString();
           if(!newText.trim().equals(""))
            Autocompletes_Timer(newText);
       }

现在我们需要一个处理程序。

Hander handler = new Handler();
private void Autocompletes_Timer(final String newText) {

    // new text will be here. so if you type fast within 1 sec.
    // handler will be remover each time so that handler post delay also be remove.
    if(handler!= null)
        handler.removeCallbacksAndMessages(null);

    // new text will be in runnable with 1 sec delay. 
    handler.postDelayed(runnable(newText), 1000);
}

   // it will be start work after 1 sec.
   // if you stop the typing then it will complete work like as sending data at server.
   // if you continue typing , it will not complete work and each type will be removeCallbackAndMessage.
    private Runnable runnable(final String newText) {
    Runnable runnable = new Runnable() {

        @Override
        public void run() {

            Log.d("Autocompleted", newText);

            // call AysncTask here  
        }

    };
    return runnable;
}

只需复制并粘贴我的代码即可。应该解决你的问题。享受。

答案 2 :(得分:0)

使用CountDownTimer

protected AutoCompleteTextView autoCompleteView;
protected AutoCompleteAdapter suggsAdapter;

String TAG = "Timer";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    countries = getResources().getStringArray(R.array.countries_array);    

    autoCompleteView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);  

    TextWatcher textChecker = new TextWatcher() {

    CountDownTimer countDownTimer = new CountDownTimer(500,100) {
            @Override
            public void onTick(long millisUntilFinished) {
                Log.d(TAG, "addressTextWatcher.countDownTimer.onTick() -> Tick: " + millisUntilFinished);
            }

            @Override
            public void onFinish() {
                Log.d(TAG, "addressTextWatcher.countDownTimer.onTick() -> Finish");

                String enteredText = autoCompleteView.getText().toString();             
                refreshList(enteredText);
            }
        };

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            countDownTimer.cancel();

            countDownTimer.start();
        }
    };        


    autoCompleteView.addTextChangedListener(textChecker);

}