【问题标题】:EditText OnKeyListener not workingEditText OnKeyListener 不起作用
【发布时间】:2023-03-05 22:04:02
【问题描述】:

我知道this/similar question 之前有人问过,但给出的解决方案对我不起作用,所以我再问一次。 我尝试了该答案中给出的解决方案,但我的 OnKeyListener 仍然从未在某些设备上被调用,尤其是那些具有股票操作系统的设备。当editText中没有字符时,我需要检测是否按下软键盘的del键。这是我的代码;

EditText et = (EditText) findViewById(R.id.et);
    et.setOnKeyListener(new EditText.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            Log.d("OnKeyListener", keyCode + " character(code) to send");
            return false;
        }
    });

【问题讨论】:

标签: android android-edittext keylistener


【解决方案1】:

终于通过TextWatcher实现了这个功能解决了我自己。主要障碍是,即使EditText 中没有字符,或者至少最终用户认为那里没有字符,我也需要检测退格键。第一件事无法实现,但我做了后一个。以下是详细的解决方案。

首先,我总是在editText 中保留一个空格“ ”字符。

editText.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        if(cs.toString().length() == 0)
            editText.setText(" ");
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }

    @Override
    public void afterTextChanged(Editable arg0) { }

});

然后我自定义了EditText 来通知我每次光标位置的变化。这个目的是通过覆盖EditTextonSelectionChanged 方法来实现的。我自定义的EditText 是这样的。

public class SelectionEnabledEditText extends EditText {
    public SelectionEnabledEditText(Context context) {
        super(context);
    }

    public SelectionEnabledEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SelectionEnabledEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);

        if(onSelectionChangeListener != null)
            onSelectionChangeListener.onSelectionChanged(selStart, selEnd);
    }

    public static interface OnSelectionChangeListener{
        public void onSelectionChanged(int selStart, int selEnd);
    }

    private  OnSelectionChangeListener onSelectionChangeListener;

    public void setOnSelectionChangeListener(OnSelectionChangeListener onSelectionChangeListener) {
        this.onSelectionChangeListener = onSelectionChangeListener;
    }
}

最后,在我的活动中,我正在监听光标位置更改事件并在editText 中重置我的光标位置,如果它在必要的空格字符开始处,即在第 0 个索引处,像这样;

editText.setOnSelectionChangeListener(new SelectionEnabledEditText.OnSelectionChangeListener() {
    @Override
    public void onSelectionChanged(int selStart, int selEnd) {
        if (selEnd == 0) {
            if (editText.getText().toString().length() == 0)
                editText.setText(" ");

            editText.setSelection(1);
        }
    }
});

希望这对类似情况有所帮助。欢迎提出改进/优化建议。

【讨论】:

  • 请注意,如果您使用设计支持库,您必须扩展 AppCompatEditText 而不是 EditText。原因是支持库会自动将布局中的所有 EditText 对象替换为应用程序兼容版本。它无法检测到您的自定义类并且不会替换它;这可能会导致各种怪异。
  • 这是一个老问题,但我想建议您一个解决方案。 stackoverflow.com/questions/18581636/…
【解决方案2】:

文档指出,按键事件只会传播给硬件击键,而不是软件。

http://developer.android.com/reference/android/view/View.OnKeyListener.html

实际上不鼓励设备制造商通过按键侦听器传播软键盘事件,尽管完全取决于制造商是否尊重这一点或实际上平等对待软键盘和硬键盘。

从 Android 4.2.2 开始,Android 系统本身将完全不支持软键盘的按键事件,因此即使是制造商也无法选择自己的方式。

所以这里唯一万无一失的选择是实现自己的 IME(软键盘),并自己处理击键。

TextWatcher 主要用于替换关键监听器,然而 editText.setText(...);也会触发 TextWatcher 事件,所以如果一个人只对键入的键感兴趣,那么可能 TextWatcher 也不是一个解决方案。

在使用带有 AutocomleteTextView 或 EditText 的 TextWatcher 时请小心。不要在 TextWatcher 事件中修改 AutocompleteTextView / EditText 内容中的文本,否则您很可能会陷入无限事件/监听循环。

【讨论】:

  • '从Android 4.2.2开始,Android系统本身将完全不支持软键盘的按键事件,因此即使是制造商也无法选择自己的方式。'我不这么认为,因为它适用于搭载 Android 4.3 的 Xperia Z1。
  • 看来你是对的。我遇到了这个问题。您能否展示一个实现“您自己的 IME(软键盘)并自己处理击键”的示例?谢谢。
  • 这个问题的完美答案!
【解决方案3】:

基于OnKeyListenerdocumentation,似乎只为硬件键盘调用回调。

当硬件按键事件被调度到这个视图时,回调的接口定义。将在将键事件提供给视图之前调用回调。这仅对硬件键盘有用;软件输入法没有义务触发这个监听器。

【讨论】:

    【解决方案4】:

    EditText 有一个属性:android:imeOptions

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/main_editor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionGo"
        />
    

    我注意到对于值:actionGoactionNonenormalactionSearch onKeyListener 被调用。

    附:它甚至可以在没有指定该属性的情况下工作。!

    【讨论】:

      【解决方案5】:

      试试这样的:

      public boolean onKey(View v, int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_DEL) {
              //Log key
              Log.d("OnKeyListener", keyCode + " character(code) to send");
          }       
          return false;
      }
      

      【讨论】:

      • 正如其他帖子中提到的,此方法仅适用于硬件键盘。正如其他人也提到的那样,您可以找到它确实适用于某些型号的软键盘手机的案例,但这绝对不是一个全面的解决方案。
      猜你喜欢
      • 2012-02-17
      • 2011-05-15
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多