嘿傢伙,我想知道如何管理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()纔會被授予執行。
我該怎麼做,請幫我:(。
任何細節?我是新android開發。所以我的代碼中的任何特定的代碼實現將不勝感激。 hehe:D – MarkJ 2011-04-25 08:20:52
我在答案中添加了一些實現細節。 – Michael 2011-04-25 10:06:37
到目前爲止它工作正常,但使用Handler時會遇到更多問題,例如適配器的異常行爲。 :(也許使用Handler對於不在UI中的後端線程是有好處的。呵呵:) – MarkJ 2011-04-26 04:39:32