@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (v == etpw2) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
if (TextUtils.isEmpty(etpw2.getText().toString())) {
etpw2.setEnabled(false);
etpw2.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_blackborder));
etpw1.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_orangeborder));
etpw1.requestFocus();
etpw1.setEnabled(true);
}
}
} else if (v == etpw3) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
if (TextUtils.isEmpty(etpw3.getText().toString())) {
etpw3.setEnabled(false);
etpw3.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_blackborder));
etpw2.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_orangeborder));
etpw2.requestFocus();
etpw2.setEnabled(true);
}
}
} else if (v == etpw4) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
if (TextUtils.isEmpty(etpw4.getText().toString())) {
etpw4.setEnabled(false);
etpw4.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_blackborder));
etpw3.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_orangeborder));
etpw3.requestFocus();
etpw3.setEnabled(true);
}
}
} else if (v == etpw5) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
if (TextUtils.isEmpty(etpw5.getText().toString())) {
etpw5.setEnabled(false);
etpw5.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_blackborder));
etpw4.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_orangeborder));
etpw4.requestFocus();
etpw4.setEnabled(true);
}
}
} else if (v == etpw6) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
if (TextUtils.isEmpty(etpw6.getText().toString())) {
etpw6.setEnabled(false);
etpw6.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_blackborder));
etpw5.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_orangeborder));
etpw5.requestFocus();
etpw5.setEnabled(true);
}
}
}
return false;
}
我用它来删除单词,但是当我按下键盘擦除按钮时,键盘会自动关闭。
我希望每次都打开键盘。但是现在,当按下键盘的后退按钮时,它会隐藏键盘。
请帮我解决这个问题。
答案 0 :(得分:0)
具有可编辑控件的软键盘不会触发'OnKey'回调方法。
https://developer.android.com/reference/android/text/method/KeyListener
希望您设法获得用于文本字段的“ OnKey”回调方法。
如果禁用任何文本字段,它将使键盘关闭。因此,在下面的代码中删除“ etpw2.setEnabled(false);”行。因此,它会一直显示键盘。
if (v == etpw2) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
if (TextUtils.isEmpty(etpw2.getText().toString())) {
etpw2.setEnabled(false);
etpw2.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_blackborder));
etpw1.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_orangeborder));
etpw1.requestFocus();
etpw1.setEnabled(true);
}
}