輸入金額專用EditText

2021-10-12 07:25:11 字數 2049 閱讀 6370

public class amountedittext extends edittext

public amountedittext(context context, attributeset attrs)

public amountedittext(context context, attributeset attrs, int defstyleattr)

private void init(context context)

@override

public void setfilters(inputfilter filters) ;

super.setfilters(filters);

}}

public class amountfilter implements inputfilter 

/*** @param source 新輸入的字串

* @param start 新輸入的字串起始下標,一般為0

* @param end 新輸入的字串終點下標,一般為source長度-1

* @param dest 輸入之前文字框內容

* @param dstart 原內容起始座標,一般為0

* @param dend 原內容終點座標,一般為dest長度-1

* @return 輸入內容

*/@override

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

spanned dest, int dstart, int dend)

//如果是0開頭,則下一位必須是點

if (desttext.startswith(zero))

//0和點之間不能輸入任何數字

else if (desttext.contains(pointer))

}else if (!desttext.contains(pointer)

&& dstart != 0 && desttext.length() != 1)

}//不能將0放在字串的第一位

if (!textutils.isempty(desttext) && dstart == 0

&& textutils.equals(zero,sourcetext)

&& !desttext.startswith(pointer))

//第一位不能輸入「.」

if (textutils.isempty(desttext) && textutils.equals(sourcetext,pointer))

//如果第一位為「.」,則點號後面不能繼續輸入

if (desttext.startswith(pointer) && dstart != 0)

//保留pointer_after_length位小數

if (!desttext.contains(pointer) && textutils.equals(pointer,sourcetext))

}else if (desttext.contains(pointer))

}string result = dest.subsequence(dstart, dend) + sourcetext;

return result;

}}

應用示例:

最後最好再驗證一次輸入是否正確

/**

* 輸入內容是否符合規則

* @return

*/public static boolean isconformrules(string result)else if (result.contains("."))else if (result.startswith("0"))else if (textutils.equals("0.",result))

}}else if (!result.contains("."))

}return true;

}

仿毒藥輸入打賞金額EditText

毒藥打賞時,需要輸入打賞金額,當然打賞金額不可以低於0.01 對於這樣的edittext元件 android 系統並沒有提供簡單的方法。需求 1.不可輸入非數字型別 2.使用者不可以輸入小於0.01的數字 textwatcher 監聽 最小單位 分 3.第乙個字元輸入點 edittext 中顯示 0...

EditText限制文字輸入

private final textwatcher mtextwatcher new textwatcher public void ontextchanged charsequence s,int start,int before,int count public void aftertextch...

edittext輸入型別總結

總結一下edit 輸入型別總結 android inputtype none 隨便吧 android inputtype text 文字型別,多為大寫 小寫和數字符號 android inputtype textcapcharacters 字母大寫 android inputtype textcap...