Android Space Bar Is Not Working

2019-06-22 08:37发布

In my Android project I have restricted Edittext to allow only Alphanumeric characters. I have using below code snippet to achieve this

    <EditText
            android:id="@+id/edt_text"
            android:layout_width="140dp"      
            android:layout_height="wrap_content"                                   
            android:layout_margin="5dp"                                                 
            android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
            android:ems="10"
            android:imeOptions="actionNext"
            android:maxLength="8"
            android:padding="5dp"
            android:singleLine="true" />

But while using this code if I tab the Space Bar showing in soft keypad it is acting as a BackSpacebutton and removing the characters in EditText. Anyone please help me to solve this issue.

4条回答
▲ chillily
2楼-- · 2019-06-22 09:12

I know it's very too late, but the problem is in the input digit restriction you have set.

sample

查看更多
别忘想泡老子
3楼-- · 2019-06-22 09:16

You can also handle this Programetically.

mEditText1.addTextChangedListener(new TextWatcher() {
        @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(" ")) {
                mEditText1.getText().toString().trim();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

I hope it may help you. :)

查看更多
放荡不羁爱自由
4楼-- · 2019-06-22 09:22

How about allowing the spacebar to create a space, and just remove it programmatically?

查看更多
Deceive 欺骗
5楼-- · 2019-06-22 09:34

use this RegEx it will return true if it is alphanumeric else it will return false.

public boolean isAlphaNumeric(String s){
    String pattern= "^[a-zA-Z0-9]*$";
        if(s.matches(pattern)){
            return true;
        }
        return false;   
}
查看更多
登录 后发表回答