最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開(kāi)發(fā)EditText禁止輸入監(jiān)聽(tīng)及InputFilter字符過(guò)濾

 更新時(shí)間:2022年06月10日 17:06:48   作者:芝麻粒兒  
這篇文章主要為大家介紹了Android開(kāi)發(fā)EditText禁止輸入監(jiān)聽(tīng)及InputFilter字符過(guò)濾示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

??監(jiān)聽(tīng)事件

setOnEditorActionListener:軟鍵盤(pán)回車(chē)監(jiān)聽(tīng)事件 

testEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        Log.e("TAG", "onEditorAction: 點(diǎn)擊了回車(chē)按鈕");
        return false;
    }
});

Kotlin代碼

testEditText.setOnEditorActionListener(OnEditorActionListener { v, actionId, event ->
    Log.e("TAG", "onEditorAction: 點(diǎn)擊了回車(chē)按鈕")
    false
})

addTextChangedListener:文本變化監(jiān)聽(tīng)事件,里面有三個(gè)回調(diào)函數(shù)

beforeTextChanged(CharSequence s, int start, int count, int after)

參數(shù)一代表輸入的字符,參數(shù)二代表當(dāng)前光標(biāo)所在EditText整個(gè)字符串的位置,參數(shù)三一般為0,參數(shù)四代表一次性輸入了幾個(gè)字符,主要是中文狀態(tài)或直接粘貼上去的字符(數(shù)字或符號(hào)或英文都是點(diǎn)擊一個(gè)就顯示上去了,所以該值為1,中文一般都是打幾個(gè)字顯示上去)

onTextChanged(CharSequence s, int start, int before, int count)

基本同上面的說(shuō)明

afterTextChanged(Editable s)

參數(shù)為修改后的字符

testEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        //參數(shù)1代表輸入的
        Log.e("TAG", "beforeTextChanged: 輸入前(內(nèi)容變化前)的監(jiān)聽(tīng)回調(diào)"+s.toString()+"==="+start+"==="+count+"==="+after);
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.e("TAG", "beforeTextChanged: 輸入中(內(nèi)容變化中)的監(jiān)聽(tīng)回調(diào)"+s.toString()+"==="+start+"==="+before+"==="+count);
    }
    @Override
    public void afterTextChanged(Editable s) {
        Log.e("TAG", "beforeTextChanged: 輸入后(內(nèi)容變化后)的監(jiān)聽(tīng)回調(diào)"+s.toString());
    }
});

Kotlin代碼

testEditText.addTextChangedListener(object : TextWatcher {
    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
        //參數(shù)1代表輸入的
        Log.e("TAG", "beforeTextChanged: 輸入前(內(nèi)容變化前)的監(jiān)聽(tīng)回調(diào)$s===$start===$count===$after")
    }
    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        Log.e("TAG", "beforeTextChanged: 輸入中(內(nèi)容變化中)的監(jiān)聽(tīng)回調(diào)$s===$start===$before===$count")
    }
    override fun afterTextChanged(s: Editable) {
        Log.e("TAG", "beforeTextChanged: 輸入后(內(nèi)容變化后)的監(jiān)聽(tīng)回調(diào)$s")
    }
})

setOnFocusChangeListener:是否獲取焦點(diǎn)的監(jiān)聽(tīng)

testEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
      Log.e("TAG", "onFocusChange: 是否獲取焦點(diǎn):hasFocus:為true表示獲取焦點(diǎn),為false表示未獲取");
    }
});

Kotlin代碼

testEditText.setOnFocusChangeListener(OnFocusChangeListener { v, hasFocus ->
    Log.e("TAG", "onFocusChange: 是否獲取焦點(diǎn):hasFocus:為true表示獲取焦點(diǎn),為false表示未獲取")
 })

??InputFilter

字符過(guò)濾在項(xiàng)目中也是經(jīng)常會(huì)遇到的業(yè)務(wù)功能(比如限制輸入小數(shù)點(diǎn)后兩位,比如僅限制中文輸入,比如不能輸入特殊字符,再比如WOCAO等敏感詞屏蔽)。

有的同學(xué)要說(shuō)了,【android:inputType】不就是做這個(gè)的嗎,確實(shí),但是為了兼容大多數(shù)人,必須要有取舍,因此也就有了局限性。

系統(tǒng)內(nèi)置了兩個(gè)過(guò)濾:new InputFilter.AllCaps()和new InputFilter.LengthFilter(int max)

AllCaps為全部自動(dòng)轉(zhuǎn)換為大寫(xiě),LengthFilter為限制字符長(zhǎng)度最大為幾。

我們【Ctrl+左鍵】快捷鍵點(diǎn)進(jìn)去看遠(yuǎn)嗎,他們是繼承的【InputFilter】,所以我們也能繼承繼而實(shí)現(xiàn)自己的過(guò)濾規(guī)則。

InputFilter custemInputFiter = new InputFilter() {
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        //source 新輸入的字符串
        //start 新輸入的字符串起始下標(biāo),一般為0
        //end 新輸入的字符串終點(diǎn)下標(biāo),一般為source長(zhǎng)度-1
        //dest 輸入之前文本框內(nèi)容
        //dstart 原內(nèi)容起始坐標(biāo),一般為0
        //dend 原內(nèi)容終點(diǎn)坐標(biāo),一般為dest長(zhǎng)度-1
        if (source.toString().equals("芝麻粒兒")) {
            //此示例:輸入的如果是【芝麻粒兒】,則直接返回null,頁(yè)面上表現(xiàn)為不顯示
            return null;
        }
        Log.e("TAG", "filter: 自定義的過(guò)濾規(guī)則");
        return null;
    }
};
//傳遞的參數(shù)是數(shù)組,也就是可以有多個(gè)過(guò)濾規(guī)則
testEditText.setFilters(new InputFilter[]{
        custemInputFiter,
        new InputFilter.LengthFilter(6),
        new InputFilter.AllCaps()});

Kotlin代碼

val custemInputFiter = InputFilter { source, start, end, dest, dstart, dend -> //source 新輸入的字符串
    //start 新輸入的字符串起始下標(biāo),一般為0
    //end 新輸入的字符串終點(diǎn)下標(biāo),一般為source長(zhǎng)度-1
    //dest 輸入之前文本框內(nèi)容
    //dstart 原內(nèi)容起始坐標(biāo),一般為0
    //dend 原內(nèi)容終點(diǎn)坐標(biāo),一般為dest長(zhǎng)度-1
    if (source.toString() == "芝麻粒兒") {
        //此示例:輸入的如果是【芝麻粒兒】,則直接返回null,頁(yè)面上表現(xiàn)為不顯示
        return@InputFilter null
    }
    Log.e("TAG", "filter: 自定義的過(guò)濾規(guī)則")
    null
}
//傳遞的參數(shù)是數(shù)組,也就是可以有多個(gè)過(guò)濾規(guī)則
testEditText.setFilters(
    arrayOf(
        custemInputFiter,
        LengthFilter(6),
        AllCaps()
    )
)

以上就是Android開(kāi)發(fā)EditText禁止輸入監(jiān)聽(tīng)及InputFilter字符過(guò)濾的詳細(xì)內(nèi)容,更多關(guān)于Android EditText監(jiān)聽(tīng)I(yíng)nputFilter的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

房山区| 南雄市| 连云港市| 凉城县| 玉环县| 措美县| 乐昌市| 铁岭市| 普宁市| 河间市| 临清市| 微山县| 澄城县| 九龙县| 泌阳县| 宁都县| 大城县| 绥棱县| 永泰县| 黔南| 鹿泉市| 疏勒县| 茂名市| 如东县| 同江市| 定西市| 高碑店市| 大兴区| 丽江市| 福安市| 阜康市| 永春县| 松原市| 临汾市| 永年县| 界首市| 西宁市| 望江县| 九龙城区| 灵寿县| 扬中市|