2011-04-25 124 views
2

嘿傢伙,我想知道如何管理Android應用程序中的延遲,例如,我有一個重寫的方法onTextChanged()。與此相關,我想設置一個類似.5秒的延遲,以便確定用戶在自動填充文本框中輸入的內容。如果用戶掛起/停止輸入.5秒,我想要在我的代碼中執行特定的方法或實現(即我自己的自動填充文本框中自己的過濾方案/邏輯,以減少我的應用程序中的資源使用情況,謝謝)。在Android應用程序管理延遲

這裏是我的示例代碼:

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

} 

和例如,用戶輸入「林斯頓」(像「倫敦」字符串),每個字母打字小於0.5秒,我想所有以前onTextChanged ()方法調用(鍵入「L」,「o」和「n」的方法調用)將被忽略,並且只有在鍵入最後一個字母時的onTextChanged()纔會被授予執行。

我該怎麼做,請幫我:(。

回答

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

任何細節?我是新android開發。所以我的代碼中的任何特定的代碼實現將不勝感激。 hehe:D – MarkJ 2011-04-25 08:20:52

+0

我在答案中添加了一些實現細節。 – Michael 2011-04-25 10:06:37

+0

到目前爲止它工作正常,但使用Handler時會遇到更多問題,例如適配器的異常行爲。 :(也許使用Handler對於不在UI中的後端線程是有好處的。呵呵:) – MarkJ 2011-04-26 04:39:32

0

我正在寫代碼運行自動完成。

第一個onTextChanged將被調用。

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

現在我們需要一個Handler。

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

只是複製並粘貼我的代碼。它應該解決你的問題。請享用。

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

}