Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permit only 1 character in EditText and always overwrite when user input it

I need to make an EditText which would accept only one character and only character (letter/alpha). And if user enters other char, it should replace existing one (like overwrite method of text input with 1 allowed symbol).

  1. I know how to set the max length of text in properties. But if I set it to 1, then no other char could be inserted before user deletes an existing one. But I want to replace existing char with that new entered one automatically without manual deleting. How to do that?

  2. I know how to set property to allow only digits in an EditText, but I can't figure out how to allow only chars. So the second question is how to allow only characters in EditText?

Currently I'm using an EditText with max text size=2 with the following code:

final EditText editLetter = (EditText)findViewById(R.id.editHouseLetter); 
editLetter.addTextChangedListener(new TextWatcher() {

    public void afterTextChanged(Editable s) {
        if (s!=null && s.length()>1){
            editLetter.setText(s.subSequence(1, s.length()));
            editLetter.setSelection(1);
        }
    }

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

    }

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

    }
});

The point is, when a user enters a second char, the first one should be deleted. Making text size to 2 allows user to enter another char after one has been already entered.

I don't really understand how it works but it does :). And additionally I had to point cursor in EditText to the last position because it always goes to beginning which makes unable to enter anything at all. Didn't get why is that either.

Main point of this solution is that it has a 2-chars sized EditText, but I want it 1-char sized. And it allows to enter anything besides the letters(chars/alpha) and I want nothing but chars.

Using the advice provided by sgarman and Character.isLetter() function, afterTextChanged method looks this way now:

public void afterTextChanged(Editable s) {
    int iLen=s.length();
    if (iLen>0 && !Character.isLetter((s.charAt(iLen-1)))){
        s.delete(iLen-1, iLen);
        return;
    } 
    if (iLen>1){
        s.delete(0, 1);
    } 
}

I've discovered that using Selection.setSelection is not required in this case. Now it has a filter allowing input of letters only. It's almost the answer that I want. The only one thing left is how to do the same with 1-symbol sized EditText?

like image 204
Stan Avatar asked Apr 05 '11 20:04

Stan


2 Answers

Try:

s.delete(0, 1);
Selection.setSelection(s, s.length());
like image 77
sgarman Avatar answered Oct 06 '22 05:10

sgarman


I wanted to do the same thing and I used your code in afterTextChanged(Editable s) and also set one filter on the editText like this.

editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(2)});

It worked as we wanted.

like image 42
Rize Liu Avatar answered Oct 06 '22 04:10

Rize Liu