0
我在Android上的EditText
上使用OnKeyListener
來識別鍵盤的退格鍵(刪除)是否被按下,並自己處理刪除字符(這樣做是因爲它是一個字段與外部服務同步)。有人說要使用TextWatcher
並在其中一個textchanged事件中處理。雖然這可能是一個可行的解決方案。我仍然試圖用onKey
來做到這一點。當退格鍵盤按鈕單擊時,onKey不會觸發android
問題是僅在EditText
中沒有文本(空)且按下退格鍵時纔會觸發onKey
。如果存在文字,onKey
在刪除所有文字之前不會觸發。
任何人我缺少onKey
在退格的所有點擊中被觸發?
這是我的xml:
<EditText
android:id="@+id/trackpad_speller_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_margin="10dp"
android:layout_weight="0.8"
android:focusable="true"
android:focusableInTouchMode="true"
android:imeOptions="actionDone" />
,我設置的片段
this.spellerInput.setOnKeyListener(this);
我的安其
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
if (presenter.getSpellerType() == SpellerType.FREE_SPELLER) {
Log.d(TAG, "backspace pressed and ignored for FREE_SPELLER");
return false;
} else {
Log.d(TAG, "backspace pressed for SEARCH_SPELLER");
presenter.deleteChar();
}
}
return true;
}
任何幫助,將不勝感激聽衆!謝謝:d