日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android textwatcher监听文本框输入变化

發(fā)布時(shí)間:2023/12/9 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android textwatcher监听文本框输入变化 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

textwatcher 包含三個(gè)接口,分別對應(yīng)文本框輸入的三個(gè)狀態(tài),分別是輸入前、輸入中、輸入完成。

1.改變前 beforeTextChanged

這個(gè)方法會在輸入前調(diào)用,有四個(gè)參數(shù)分別是
CharSequence s: editview中原來的內(nèi)容
int start : 本次替換的起始位置
int count: 本次替換內(nèi)容的長度
int after: 新替換內(nèi)容的長度
注釋中說的很明白,這個(gè)方法被調(diào)用是用來通知你,原內(nèi)容s從start位置開始的count個(gè)字符將為被長度為after的新內(nèi)容替換,而不能在這個(gè)方法內(nèi)改變s的內(nèi)容。

/*** This method is called to notify you that, within <code>s</code>,* the <code>count</code> characters beginning at <code>start</code>* are about to be replaced by new text with length <code>after</code>.* It is an error to attempt to make changes to <code>s</code> from* this callback.*/public void beforeTextChanged(CharSequence s, int start,int count, int after);

2.改變中 onTextChanged

這個(gè)方法也有四個(gè)參數(shù),和上一個(gè)方法不同的是把a(bǔ)fter換成來before,但別的參數(shù)的意思也有略微的改動。
CharSequence s: 代表替換后的內(nèi)容。
int start:被替換的初始位置
int before:代表被替換內(nèi)容的長度,對應(yīng)上一個(gè)方法的count。
int count: 代表新內(nèi)容的長度
這個(gè)方法是用來通知你,原本start位置后的before個(gè)字符已經(jīng)被替換成count個(gè)新字符了。注意的是這個(gè)方法內(nèi)同樣不能對s進(jìn)行改變

/*** This method is called to notify you that, within <code>s</code>,* the <code>count</code> characters beginning at <code>start</code>* have just replaced old text that had length <code>before</code>.* It is an error to attempt to make changes to <code>s</code> from* this callback.*/public void onTextChanged(CharSequence s, int start, int before, int count);

3.改變后 afterTextChanged

這個(gè)方法只有一個(gè)參數(shù)。
Editable s 文本改變之后的內(nèi)容
這個(gè)方法是用來通知你,之前s已經(jīng)被修改了,在這個(gè)方法中你能夠?qū)進(jìn)行改動,但是需要注意的是別陷入死循環(huán),因?yàn)槊看胃膭佣紩俅握{(diào)用這個(gè)方法進(jìn)行遞歸。這個(gè)方法沒有像之前的方法一樣有多個(gè)參數(shù)來告訴你它改動的位置和長度,是因?yàn)榕c此同時(shí)可能有別的afterTextChanged也改動了,就會使得原來的信息告訴你也沒用,因?yàn)楹芸赡芤呀?jīng)變化了。如果一定想要知道,可以在onTextChanged()中用setSpan()進(jìn)行標(biāo)志結(jié)束的位置。
需要的注意的是,此時(shí)雖然獲取到了新內(nèi)容,但是還沒更新到UI上。

/*** This method is called to notify you that, somewhere within* <code>s</code>, the text has been changed.* It is legitimate to make further changes to <code>s</code> from* this callback, 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.* (You are not told where the change took place because other* afterTextChanged() methods may already have made other changes* and invalidated the offsets. But if you need to know here,* you can use {@link Spannable#setSpan} in {@link #onTextChanged}* to mark your place and then look up from here where the span* ended up.*/public void afterTextChanged(Editable s);

現(xiàn)在知道了這三個(gè)監(jiān)聽方法,我們就可以根據(jù)業(yè)務(wù)進(jìn)行自定義化的操作了。

總結(jié)

以上是生活随笔為你收集整理的Android textwatcher监听文本框输入变化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。