【问题标题】:Disable SPACE key in EditText android在EditText android中禁用SPACE键
【发布时间】:2016-07-28 10:32:54
【问题描述】:

我创建了带有以下内容的 EditText。

<EditText
        android:id="@+id/et_regis_num"
        android:maxLines="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:digits="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        android:hint="@string/txt_reg_num"
        android:inputType="textCapCharacters"
        android:maxLength="10" />

在此编辑文本中,我不想按 SPACE 键,但是当我按 SPACE 键时,它就像 BACKSPACE 键一样工作。表示每按两次删除一个字符。

【问题讨论】:

  • 没有得到你的问题。这是(空格作为退格)当前行为还是预期行为?
  • 你可以像这篇文章stackoverflow.com/questions/33993041/…中提到的那样设置输入过滤器
  • @IshitaSinha 同意
  • 我在模拟器(Nexus API 19)上进行了测试,但没有发生
  • @MujammilAhamed 我也试过了,但仍然面临同样的问题。

标签: android android-edittext keyevent keycode


【解决方案1】:

EditText 上设置InputFilter。 请检查下面的答案,它对我有用。

InputFilter filter = new InputFilter() {
    public CharSequence filter(CharSequence source, int start, int end,
        Spanned dest, int dstart, int dend) {
        for (int i = start; i < end; i++) {
            if (Character.isWhitespace(source.charAt(i))) {
                return "";
            }
        }
        return null;
    }
};

edtTxt.setFilters(new InputFilter[] { filter });

【讨论】:

  • 这种方法有非常奇怪的副作用(如果为此 EditText 启用了建议) - 敲击空格会触发退格等。在 EditText 末尾输入的第一个空格确实被过滤掉了,但是下一个空格敲击的工作方式就像退格擦除输入的文本一样,这很奇怪。为了避免这种副作用,您可以使用 inputType="textVisiblePassword" - 使用这种解决方法,它会起作用。
【解决方案2】:

只在你的编辑文本中留出空间并用空替换空间,

    @Override
    public void afterTextChanged(Editable s) {
    String result = s.toString().replaceAll(" ", "");
    if (!s.toString().equals(result)) {
         ed.setText(result);
         ed.setSelection(result.length());
         // alert the user
    }
}

【讨论】:

  • 如果我已经提到过 android:digits,你试过了吗?所以这里不考虑空间。
  • 你试过在xml中添加这样的android:digits="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  • 我试过这样 和 afterTextChanged
  • 我试过这个,当我按两次空格键它只删除最后一个字符
【解决方案3】:
   EditText editPassword = findViewById(R.id.et_Rpassword);
            editPassword.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) {
                    if(charSequence.length() > 0){
                    if((charSequence.charAt(i)+"").equalsIgnoreCase(" ")){
                        Toast.makeText(RegisterActivity.this, "you'r can't enter Space", Toast.LENGTH_LONG).show();
                        String oldPass = editPassword.getText().toString();
                        editPassword.setText(oldPass.replace(" ",""));
                    }
                }}
                @Override
                public void afterTextChanged(Editable editable) {
                }
            });

【讨论】:

  • 仅代码的答案被认为是低质量的:请务必说明您的代码的作用以及它如何解决问题。如果您可以在帖子中添加更多信息,它将帮助提问者和未来的读者。见Explaining entirely code-based answers
【解决方案4】:
private InputFilter filter = new InputFilter() {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {


        if(source.equals(" ")){
            int startSelection=editTextView.getSelectionStart();
            int endSelection=editTextView.getSelectionEnd();
            editTextView.setText(editTextView.getText().toString().trim());
            editTextView.setSelection(startSelection,endSelection);

        }

        return null;
    }
};

editTextView.setFilters(new InputFilter[] { filter });

【讨论】:

    【解决方案5】:
        passwordEditText.filters = arrayOf(
            InputFilter { source, start, end, _, _, _ ->
                for (i in start until end) {
                    if (Character.isWhitespace(source[i])) {
                        return@InputFilter ""
                    }
                }
                null
            })
    

    【讨论】:

      【解决方案6】:
       edittxt.addTextChangedListener(new TextWatcher() {
      
                  @Override
                  public void onTextChanged(CharSequence s, int start, int before, int count) {
                      // TODO Auto-generated method stub
                      String str = s.toString();
                      if (str.equalsIgnoreCase(" ")||str.equalsIgnoreCase("  ")) {
                          // orderdata.setError("Space is not allowed");
                          edittxt.setText("");
                      }
                  }
      
                  @Override
                  public void beforeTextChanged(CharSequence s, int start, int count,
                                                int after) {
                      // TODO Auto-generated method stub
      
                  }
      
                  @Override
                  public void afterTextChanged(Editable s) {
                      // TODO Auto-generated method stub
      
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 2011-05-16
        • 2012-09-12
        • 2016-11-23
        • 2012-05-25
        • 2015-11-05
        • 2011-08-18
        • 2018-02-11
        • 2019-12-14
        • 1970-01-01
        相关资源
        最近更新 更多