Android-change Edittext After Each Change
how can i add Char such as this dash '-' after each change in edtitext for example if the user enter A then the text in edittext will be A- then the user will complete and enter Ch
Solution 1:
You are having infinite loop as described in Android Doc
but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively.
So all you have to do is just imposing a condition to avoid infinite loop. For example,
name.addTextChangedListener(new TextWatcher() {
publicvoidafterTextChanged(Editable s) {
if(s.charAt(s.length()-1)!='-'){
s.append("-");
}
}
publicvoidbeforeTextChanged(CharSequence s, int start, int count,
int after) {
}
publicvoidonTextChanged(CharSequence s, int start, int before,
int count) {
}
});
Solution 2:
Add a TextWatcher, using addTextChangedListener().
Solution 3:
Append the - char in beforeTextChagned
name = (EditText)findViewById(R.id.editText1);
name.addTextChangedListener(new TextWatcher() {
publicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
name.setText(s+"-");
}
publicvoidafterTextChanged(Editable s){}
publicvoidonTextChanged(CharSequence s, int start, int before, int count){}
}
Solution 4:
name = (EditText)findViewById(R.id.editText1);
name.addTextChangedListener(new TextWatcher(){
publicvoidafterTextChanged(Editable s) {
try{
name.setText(s.toString()+"-");
}catch(exception e)
{
e.printStackTrace();
}
}
publicvoidbeforeTextChanged(CharSequence s, int start, int count, int after){}
publicvoidonTextChanged(CharSequence s, int start, int before, int count){
}
Post a Comment for "Android-change Edittext After Each Change"