在EditText android中禁用SPACE键

时间:2016-07-28 10:32:55

标签: android android-edittext keyevent keycode

我创建了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" />

在这个edittext中我不想按SPACE键但是当我按下SPACE键时它就像BACKSPACE键一样工作。意味着每次按两次按下一个字符。

6 个答案:

答案 0 :(得分:4)

InputFilter上设置EditText。 请检查以下答案,它为我工作。

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 });

答案 1 :(得分:3)

只需在edittext中留出空格,然后用空格

替换空格
    @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
    }
}

答案 2 :(得分:1)

   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) {
                }
            });

答案 3 :(得分:0)

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 });

答案 4 :(得分:0)

    passwordEditText.filters = arrayOf(
        InputFilter { source, start, end, _, _, _ ->
            for (i in start until end) {
                if (Character.isWhitespace(source[i])) {
                    return@InputFilter ""
                }
            }
            null
        })

答案 5 :(得分:0)

Is Group By