[英]Detect delete button in soft keyboard
我有兩個 EditText(每個只接受一個字符),我想處理這兩個字段,就像我只有一個一樣。
當用戶在第一個字符中寫入字符時,我使用 TextWatcher 在第二個字符中設置焦點,但我不知道如何做相反的事情。
如果用戶按下第二個 EditText 中的刪除按鈕(此 EditText 為空),我想將焦點移動到第一個 EditText 並刪除那里的字符。
問題是當用戶嘗試刪除空字段時 TextWatcher 不起作用(因為實際上沒有任何變化)。 而且 onKeyDown 事件只適用於硬鍵盤,所以我不知道如何處理這個問題......
謝謝!
剛剛檢查了該問題的代碼(實際上來自提供的問題並由Labeeb P回答)和測試項目,只需對布局進行兩次編輯,它似乎工作得很好 - 即使編輯是,我也能夠收到刪除空。
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;
}
});
似乎應該使EditText的android文檔更加清晰,或者至少提供EditText的任何指南 - 提供軟鍵盤交互,因為有許多典型的應該由幾乎每個開發人員解決。
更新:似乎這種方式不適用於最新(至少在4.1之后)Android版本。 這個答案似乎適用於4.1之后的版本。
我偶然發現的一個更簡單的解決方案是使用InputFilter 。 InputFilter的filter()
方法似乎報告所有軟鍵盤事件 - 即使EditText的值沒有改變的事件。
因此,要解決您的具體情況,請構造一個輸入過濾器並進行相應設置
private InputFilter filter = (charSequence, start, end, dest, dStart, dEnd) -> {
if (end == 0 || dStart < dEnd) {
// backspace was pressed! handle accordingly
}
return charSequence;
};
...
myEditText.setFilters(new InputFilter[] { filter });
可以使用end
, dStart
和dEnd
來評估退格事件。 如果刪除了一個字符, dStart
將始終小於dEnd
。 如果EditText
為空,您仍然可以通過檢查end
== 0來評估退格鍵。
請注意,批量刪除也將在此if
語句中捕獲,因此您可能希望使用filter()
進行一些額外的檢查。 另請注意,如果您使用計算機鍵盤在模擬器中鍵入EditTexts,則可能會出現意外結果。 最好點擊軟件按鈕進行測試。
import java.util.Random;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import android.widget.EditText;
/**
* Created by mkallingal on 4/25/2016.
*/
public class CustomEditText extends EditText {
private Random r = new Random();
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context) {
super(context);
}
public void setRandomBackgroundColor() {
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new ZanyInputConnection(super.onCreateInputConnection(outAttrs),
true);
}
private class ZanyInputConnection extends InputConnectionWrapper {
public ZanyInputConnection(InputConnection target, boolean mutable) {
super(target, mutable);
}
@Override
public boolean sendKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
CustomEditText.this.setRandomBackgroundColor();
// Un-comment if you wish to cancel the backspace:
// return false;
}
return super.sendKeyEvent(event);
}
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
// magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
if (beforeLength == 1 && afterLength == 0) {
// backspace
return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
&& sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
}
return super.deleteSurroundingText(beforeLength, afterLength);
}
}
}
現在您可以在Activity中使用它,如下所示:
final CustomEditText editText = new CustomEditText(cxt);
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
String _text= editText.getText().toString();
if(StringUtils.isBlank(_text))
//editText is now empty
}
}
return false;
}
});
我通過重寫EditText
來實現它,以便訪問包含deleteSurroundingText
方法的InputConnection
對象。 它有助於檢測刪除(退格)事件。 請看一下我在那里提供的解決方案: Android - 無法捕獲退格/刪除按下軟件。 鍵盤
此解決方案適用於hardKeyboard和softKeyboard。
package com.bikash.layout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnKeyListener, TextWatcher {
EditText otp1, otp2, otp3, otp4, otp5, otp6;
private boolean canDelete = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
otp1 = findViewById(R.id.otp1);
otp2 = findViewById(R.id.otp2);
otp3 = findViewById(R.id.otp3);
otp4 = findViewById(R.id.otp4);
otp5 = findViewById(R.id.otp5);
otp6 = findViewById(R.id.otp6);
otp1.addTextChangedListener(this);
otp2.addTextChangedListener(this);
otp3.addTextChangedListener(this);
otp4.addTextChangedListener(this);
otp5.addTextChangedListener(this);
otp6.addTextChangedListener(this);
otp1.setOnKeyListener(this);
otp2.setOnKeyListener(this);
otp3.setOnKeyListener(this);
otp4.setOnKeyListener(this);
otp5.setOnKeyListener(this);
otp6.setOnKeyListener(this);
otp1.requestFocus();
}
@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) {
if (otp1.isFocused()) {
if (otp1.getText().toString().length() == 1) {
otp2.requestFocus();
}
}
if (otp2.isFocused()) {
if (otp2.getText().toString().length() == 1) {
otp3.requestFocus();
}
}
if (otp3.isFocused()) {
if (otp3.getText().toString().length() == 1) {
otp4.requestFocus();
}
}
if (otp4.isFocused()) {
if (otp4.getText().toString().length() == 1) {
otp5.requestFocus();
}
}
if (otp5.isFocused()) {
if (otp5.getText().toString().length() == 1) {
otp6.requestFocus();
}
}
if (otp6.isFocused()) {
if (otp6.getText().toString().length() == 1) {
}
}
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (v.getId()) {
case R.id.otp2: {
if (keyCode == KeyEvent.KEYCODE_DEL) {
otp1.setText("");
otp1.requestFocus();
}
break;
}
case R.id.otp3: {
if (keyCode == KeyEvent.KEYCODE_DEL) {
otp2.setText("");
otp2.requestFocus();
}
break;
}case R.id.otp4: {
if (keyCode == KeyEvent.KEYCODE_DEL) {
otp3.setText("");
otp3.requestFocus();
}
break;
}case R.id.otp5: {
if (keyCode == KeyEvent.KEYCODE_DEL) {
otp4.setText("");
otp4.requestFocus();
}
break;
}case R.id.otp6: {
if (keyCode == KeyEvent.KEYCODE_DEL) {
otp5.setText("");
otp5.requestFocus();
}
break;
}
}
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.