Android EditText輸入最大值提示功能

2021-07-15 16:59:00 字數 740 閱讀 8064

有時,我們會對edittext新增最大長度限制,在edittext達到輸入上限時阻止繼續輸入並彈出提示。

這個問題看似簡單,但很難處理的完美。

由於edittext無法攔截軟體盤事件,常規的通過textwatcher,onkeylistener,等方法都會存在一定的問題。經過反覆試錯,終於發現以下方案:

public class lengthfilter implements inputfilter 

@override

public charsequence filter(charsequence source, int start, int end,

spanned dest, int dstart, int dend)

merrortoast.show();

return "";

} else if (keep >= end - start) else

merrortoast.show();

if (character.ishighsurrogate(source.charat(keep - 1)))

}return source.subsequence(start, keep);}}

private int mmax;

}

2.將這個inputfilter設定給對應的edittext:

medittext.setfilters(new inputfilter );

android edittext學習記錄

實現在edittext中輸入自定義表情,需要將放在drawable中的表情,嵌入到edittext中,這裡利用imagespan實現該功能。第一步 利用反射機制,得到資源的id,生成bitmap物件 field field r.drawable.class.getdeclaredfield imag...

Android EditText屬性總結

接下來老於就總結一下 edittext屬性吧!android inputtype none 這個就不要解釋了吧 android inputtype text 文字型別,多為大寫 小寫和數字符號 android inputtype textcapcharacters 字母大寫 android inpu...

Android EditText左右滑動

預設的edittext寫滿一行後會自動轉換到下一行,但是我們有時候需要的滿一行後左右滑動過去而不是換行,此時我們可以設定edittext 的屬性 android maxlines 1 即可,如果不可以,則加上 android singleline true android layout width ...