Android disable space only for Edittext

2019-03-24 15:03发布

In my android application I need to disABLE Spacebar only. But I didn't find a solution for this problem. I need to disable space bar and when user enter space should not work, special characters, letters, digits and all other should work. What I tried is,

etPass.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.length() > 0 && str.contains(" "))
                {
                    etPass.setError("Space is not allowed");
                    etPass.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

            }
        });

But the problem here is once space comes whole text is deleting. I removed

etPass.setText("");

this line, so at that time error message is showing, but at that time user can still able to type space. But what I need is user shouldn't able to type the space.

10条回答
2楼-- · 2019-03-24 15:27

Try this

Use android:digits don't include " "(space in it)

<EditText
    android:inputType="number"
    android:digits="0123456789.abcdefghijklmnl....."// write character that you want to allow
/>
查看更多
Root(大扎)
3楼-- · 2019-03-24 15:27

instead of etPass.setText(""); just remove space from EditText data.

etPass.setText(etPass.getText().toString().trim());
etPass.setSelection(autoComplete.getText().length());

so your IF condition will be as follows :

if(str.length() > 0 && str.contains(" "))
{
    etPass.setError("Space is not allowed");
    etPass.setText(etPass.getText().toString().trim());
    etPass.setSelection(etPass.getText().length());
}
查看更多
看我几分像从前
4楼-- · 2019-03-24 15:29

In afterTextChanged method put the following code:

  public void afterTextChanged(Editable s) {

        String str = etPass.getText().toString();
        if(str.length() > 0 && str.contains(" "))
        {
            etPass.setText(etPass.getText().toString().replaceAll(" ",""));
            etPass.setSelection(etPass.getText().length());
        }
    }
查看更多
ら.Afraid
5楼-- · 2019-03-24 15:33

her is solution work for me :

android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
android:inputType="textFilter"

Add it into edit text in xml file

查看更多
孤傲高冷的网名
6楼-- · 2019-03-24 15:33

To disable space use

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (editclicked) {
        if (keyCode == KeyEvent.KEYCODE_SPACE) {
            return false
        }
    } else {
        super.onKeyDown(keyCode, event);
    }
}
查看更多
何必那么认真
7楼-- · 2019-03-24 15:36

Why don't you think about a character filter .. Here is a sample code snippet.

/* To restrict Space Bar in Keyboard */
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;
    }

};
input.setFilters(new InputFilter[] { filter });
查看更多
登录 后发表回答