gpt4 book ai didi

Android 拦截来 self 自己的应用程序的软击键 - 退格问题

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:42 25 4
gpt4 key购买 nike

我有一个应用程序需要反射(reflect)在我拥有的某个 Fragment 中进行的每个击键。到目前为止,我所做的是将 TextWatcher 附加到 EditText 并覆盖 afterTextChanged 并拉出最后一个字符(EditText 将被隐藏,因此一次只能输入 1 个字符)。

为了处理退格键,我重写了 EditText 中的 onKeyDown 并使用涉及 Jellybean 及以上设备的自定义 BaseInputConnection 的解决方法(将生成的 KEYCODE_DEL 传递给 onKeyDown - 在 SO 的某处找到了这个解决方案)。

我遇到的问题是,当 EditText 为空时,不会生成任何 KEYCODE_DEL 事件,所以我无法检测到退格键(即使它不会什么都不做)。我想做的是在创建 EditText 时添加一个字符,当我在 afterTextChanged 中检测到 EditText是空的,所以如果我在输入另一个字符之前按下退格键,它将删除该填充字符,然后用另一个填充字符重新填充它。

但是,“填充字符”永远不会被删除。例如,我在实例化时用“a”填充 EditText。如果我按退格键,什么也不会发生。 “a”没有从 EditText 中删除。

有人知道这里发生了什么吗?

状态变量

private static volatile boolean backspacePressed = false;
private static volatile boolean ignoreTextChange = false;

TextWatcher

private TextWatcher textWatcher = new TextWatcher() {       
@Override
public void afterTextChanged(Editable s) {
if(backspacePressed) {
Logger.i("BKSPC");
backspacePressed = false;
if(et.length() <= 1) {
ignoreTextChange = true;
et.setText("b");
et.setSelection(1);
}
return;
}

if(ignoreTextChange) {
ignoreTextChange = false;
return;
}
else {
Logger.i("" + s.charAt(s.length() - 1));
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
};

自定义EditText

public class InterceptTextView extends EditText {

public InterceptTextView(Context context) {
super(context);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent key) {
if(keyCode == KeyEvent.KEYCODE_DEL && key.getAction() == KeyEvent.ACTION_DOWN) {
Logger.i("BCKSPC");
}
return false;
}

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
outAttrs.actionLabel = null;
outAttrs.inputType = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;

BaseInputConnection connection = new BaseInputConnection(this, false) {

@Override
public boolean deleteSurroundingText (int beforeLength, int afterLength) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
/* In Jelly Bean, they don't send key events for delete.
* Instead, they send beforeLength = 1, afterLength = 0.
* So, we'll just simulate what it used to do. */
if (beforeLength == 0 || beforeLength == 1 && afterLength == 0) {
sendDownUpKeyEventForBackwardCompatibility(KeyEvent.KEYCODE_DEL);
backspacePressed = true;
return true;
}
}
return super.deleteSurroundingText(beforeLength, afterLength);
}

private void sendDownUpKeyEventForBackwardCompatibility (final int code) {
final long eventTime = SystemClock.uptimeMillis();
super.sendKeyEvent(new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, code, 0, 0,
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE));
super.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime, KeyEvent.ACTION_UP, code, 0, 0,
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE));
}
};
return connection;
}

}

我的 FragmentonCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
et = new InterceptTextView(getActivity());
et.addTextChangedListener(textWatcher);
ignoreTextChange = true;
et.setText("a");
et.setSelection(1);
return et;
}

最佳答案

这是我的做法。在我的 InputConnection 中,我重写了 getTextBeforeCursor 以始终返回 "" 以便 IME 始终认为它至少可以删除一个字符。

我还将连接的输入类型设置为 InputType.TYPE_NULL,以便传递关键事件(来自 Google 的官方解决方法)。

我的自定义 EditText:

public class InterceptTextView extends EditText {
public interface OnBackspacePressListener {
public void onBackspacePressed();
}

private OnBackspacePressListener backspaceListener;


public InterceptTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setText(" ");
}

public void setOnBackspacePressListener(OnBackspacePressListener backspaceListener) {
this.backspaceListener = backspaceListener;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DEL) {
if(backspaceListener != null) {
backspaceListener.onBackspacePressed();
}
}
return super.onKeyDown(keyCode, event);
}

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
outAttrs.actionLabel = null;
outAttrs.inputType = InputType.TYPE_NULL;

BaseInputConnection connection = new BaseInputConnection(this, false) {

@Override
public String getTextBeforeCursor(int ignore, int ignore2) {
return " ";
}
};
return connection;
}
}

我的 fragment :

public class KeyFragment extends Fragment {
private static volatile boolean backspacePressed = false;

private InterceptTextView et;

private TextWatcher textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if(backspacePressed) {
backspacePressed = false;
return;
}
else {
Logger.i("" + s.charAt(s.length() - 1));
}
}

@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 View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.keyboard, null);
et = (InterceptTextView) v.findViewById(R.id.keyboard_input_sink);
et.setOnBackspacePressListener(new OnBackspacePressListener() {

@Override
public void onBackspacePressed() {
backspacePressed = true;
Logger.i("BCKSPC");
}

});
et.addTextChangedListener(textWatcher);
return v;
}
}

关于Android 拦截来 self 自己的应用程序的软击键 - 退格问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20614896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com