I have a TextWatcher on an EditText, in the method afterTextChanged, I add characters to the EditText then I move the cursor to the end of EditText for continue adding text, but I have problems with that.
Like this:
public void afterTextChanged(Editable s) {
if(edittext.getText().length()==2){
// append dot to edittext
edittext.append(".");
// move cursor at end position in EditText
edittext.setSelection(edittext.getText().length());
}
}
In android 4.0v or superior, the cursor stay before the "." , and In 2.2v works fine, but in both I can't delete the characters.
Anyone with the same problem ?
Grettings
You can do something like this to avoid delete problem...
public class MainActivity extends Activity {
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edittext=(EditText)findViewById(R.id.editText1);
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable ed) {
// TODO Auto-generated method stub
if(edittext.getText().length()==2 && count < 3){
// append dot to edittext
edittext.append(".");
// move cursor at end position in EditText
edittext.setSelection(edittext.getText().length());
}
count=edittext.getText().length();
}
});
}
Your code looks fine...
But, if you delete a character, text length is == 2 again and your code will automatically add a '.' char again - so it looks like deleting is not possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With