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).
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?
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 inEditText
?
Currently I'm using an EditText
with max text size=2 with the following code:
final EditText editLetter = (EditText开发者_Go百科)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
?
Add the following line to your layout XML file android:maxLength="1"
Following is an example of adding it to an editText
element:
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLength="1" >
I faced the same problem and found this more straightforward solution. Please refer to this blog for more details.
Edit: Please note that my answer is for defining the max number of characters, however, it does not overwrite upon user input.
Try:
s.delete(0, 1);
Selection.setSelection(s, s.length());
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.
精彩评论