本文介紹了android如何將EditText光標設置到其文本的末尾的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
用戶必須輸入他的手機號碼,手機號碼必須是10個號碼,我使用TextWatcher
來做到這一點,像這樣
The user has to enter his mobile number, the mobile number has to be 10 numbers, I used TextWatcher
to do that, like this
et_mobile.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@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
if (et_mobile.getText().toString().length() > 10) {
et_mobile.setText(et_mobile.getText().toString()
.subSequence(0, 10));
tv_mobileError.setText("Just 10 Number");
}else{
tv_mobileError.setText("*");
}
}
});
但問題是當用戶輸入第11個數(shù)字時,edittext的光標從文本的開頭開始,我希望它仍然在末尾,怎么辦?
but the problem is when the user enters the 11th number, the cursor of the edittext starts from the beginning of the text, I want it to still at the end, how?
推薦答案
你有兩個選擇,都應該工作:
You have two options, both should work:
一)
editText.setText("Your text");
editText.setSelection(editText.getText().length());
b)
editText.setText("");
editText.append("Your text");
這篇關(guān)于android如何將EditText光標設置到其文本的末尾的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!