本文介紹了android如何將EditText光標(biāo)設(shè)置到其文本的末尾的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
限時(shí)送ChatGPT賬號(hào)..
用戶必須輸入他的手機(jī)號(hào)碼,手機(jī)號(hào)碼必須是10個(gè)號(hào)碼,我使用TextWatcher
來(lái)做到這一點(diǎn),像這樣
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("*");
}
}
});
但問(wèn)題是當(dāng)用戶輸入第11個(gè)數(shù)字時(shí),edittext的光標(biāo)從文本的開(kāi)頭開(kāi)始,我希望它仍然在末尾,怎么辦?
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?
推薦答案
你有兩個(gè)選擇,都應(yīng)該工作:
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光標(biāo)設(shè)置到其文本的末尾的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!