![](/img/trans.png)
[英]edittext are in the wrong place when softkeyboard are shown in android
[英]Android: Is there any way to identify when the keys are pressed/unpressed on an edittext softkeyboard?
我有一個自動完成的edittext字段,每次文本更改時都會向服務器發送查詢,但是如果用戶輸入大量文本然后按住“刪除”,我不想為刪除的每個字符發送查詢。 解決這個問題的一種方法是在輸入字符時緩存所有查詢,然后在刪除時回放緩存 - 但在我看來這似乎是不必要的復雜。 一種更簡單的方法是檢測keydown / keyup,然后僅在文本已更改但用戶也未觸摸鍵盤時才發送查詢。 這讓我很難知道如何從軟鍵盤中獲取鍵盤。
我已經嘗試過ontouchlistener(不會觸發)和onkeypressedlistener(也不會觸發)甚至覆蓋edittext中的onKeyPreIme(也不會觸發)。 有辦法做到這一點嗎?
解決此問題的方法是向查詢添加延遲,以便每次都不執行。 我的代碼看起來像這樣:
long lastPress = 0l;
@Override
public void onTextChanged(CharSequence s,
int start, int before, int count){
if(System.currentTimeMillis() - lastpress > 500){
lastPress= System.currentTimeMillis();
// insert query here
}
}
希望這可以幫助。
您無法直接檢測到它,但是您可以通過覆蓋Activity中的onMeasure並檢測它何時收縮並擴展超過某個閾值來找到間接方式,只要您在Manifest中定義android:windowSoftInputMode
為adjustResize
。
但根據我的經驗,這種方法並不是非常可靠。 您還應該考慮用戶可以在不關閉鍵盤的情況下離開您的活動。
這取決於您在刪除時需要采取的操作類型,但我遇到了類似情況,我需要將編輯后的文本保存到共享首選項。 我發現最好的解決方案是在每次刪除字符時保存它:它是我發現的唯一安全且一致的方法,並且性能損失不明顯。
更好地使用TextWatcher
1)創建一個TextWatcher
偵聽器對象:
private static long time = 0;
private final long TIME_DELAY = 700;
private final TextWatcher myTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
if(System.currentTimeMillis() - time > TIME_DELAY ){
time= System.currentTimeMillis();
// your codes
}
}
};
2)將此監聽器注冊到您的edittext
myEditText.addTextChangedListener(myTextWatcher);
如果您只想識別在軟鍵盤中按下的刪除鍵,那么試試這個..
final EditText edit1 = (EditText) findViewById(R.id.editText1);
edit1.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// You can identify which key pressed buy checking keyCode value
// with KeyEvent.KEYCODE_
if (keyCode == KeyEvent.KEYCODE_DEL) {
// this is for backspace
Log.e("IME_TEST", "DEL KEY");
}
return false;
}
});
我的最終解決方案是將Andrea的答案與Jelle的答案結合起來。 我記錄了上次在textwatcher中按下一個鍵並且僅在500毫秒后對其進行測試 - 為了確定它是最后一個按下的鍵:
private static long lastCharacterPress = 0l;
private Editable stringToSearchFor;
private Handler delayedCharacterCheck;
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
lastCharacterPress = System.currentTimeMillis();
stringToSearchFor = s;
if (s.length() > 2) {
delayedCharacterCheck.postDelayed(new Runnable() {
@Override
public void run() {
checkIfTimeHasPassed();
}
}, 500);
} else ...
}
});
然后我把這個方法:
private void checkIfTimeHasPassed() {
if (!TextUtils.isEmpty(stringToSearchFor) && edittext.getText().length() > 2 && System.currentTimeMillis() - lastCharacterPress > 499) {
// perform the actual query
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.