【问题标题】:How to capture event when user has entered a new character in a EditText?当用户在 EditText 中输入新字符时如何捕获事件?
【发布时间】:2015-07-26 10:49:55
【问题描述】:

我有一个仅限数字字符的 EditText,当用户输入数字和按下“。”时,我需要执行特殊操作。虚拟键盘上的字符。

numberEditText = new EditText(this);
numberEditText.setInputType(InputType.TYPE_CLASS_NUMBER);

我知道存在这个监听器:

numberEditText.setOnEditorActionListener(new OnEditorActionListener() {         
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {               
                return false;
            }
        });

但我不知道如何使用它来满足我的需求。

任何帮助都会被磨碎。

谢谢

【问题讨论】:

  • 你试过 textChagedListener 了吗?

标签: android android-edittext android-keypad


【解决方案1】:

就是这样-

editText.addTextChangedListener(new TextWatcher() {

       @Override
       public void afterTextChanged(Editable s) {}

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

       @Override    
       public void onTextChanged(CharSequence s, int start,
         int before, int count) {
          if(s.equals("."))
            editText.setText("");
       }
      });

【讨论】:

  • 嗨,如何知道最后输入的字符?
  • 它不起作用...如果我按“。”没有调用侦听器,因为 android 键盘不接受点作为编辑文本的第一个字符..
  • 好的,最后我使用这个答案结合这行代码实现了它:numberEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
【解决方案2】:

添加文本更改侦听器。

editText.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

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

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {

     //Do your stuff here

   }
  });

【讨论】:

  • 嗨,如何知道最后输入的字符?是否可以将这些方法之一中的编辑文本内容重置为空字符串?
  • 它不起作用...如果我按“。”没有调用侦听器,因为 android 键盘不接受点作为编辑文本的第一个字符..
  • 是吗?那么让我明天试一试,抱歉没有用“。”尝试。 ;会尝试更新你。
  • 您的 EditText 输入受数字限制,对吗?也许更改为正常设置,然后捕获文本更改的非数字。不知道好不好用,可以试试
【解决方案3】:

您可以像这样在编辑文本中使用文本观察器

editText.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

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

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {

     //Do your stuff here

   }
  });

【讨论】:

  • 它不起作用...如果我按“。”未调用侦听器,因为 android 键盘不接受点作为编辑文本的第一个字符..
【解决方案4】:

编辑:显然它不适用于 OP。

if(event.getKeyCode() == KEYCODE_NUMPAD_DOT) { ... }

可以工作。也许你必须在“KEYCODE_NUMPAD_DOT”前面添加一些东西

所有键码都可以在这里找到:http://developer.android.com/reference/android/view/KeyEvent.html

【讨论】:

  • if(event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_DOT) 不起作用,因为当我按“。”时没有调用 onEditorActionListener。
【解决方案5】:

您可以使用上面的代码块获取最后一个 inout 字符。

getSelectionStart() 获取光标位置。输入字符后,光标移到该字符的右侧。所以你可以通过(光标位置-1)获取最后输入的字符

edt.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            String text = charSequence.toString();

            if (text.equals("")) return;

            int index = edt.getSelectionStart() - 1;

            Character lastCharacter = text.charAt(index);

            Log.v("LAST_CHARACTER", "Index: " + index + " --> " + lastCharacter.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多