一旦用户从数字十进制键盘 android Edittext 输入 2 位数字,自动设置小数点 (.)
Automatically set decimal point (.) once user enters 2 digits from number decimal keyboard android Edittext
一旦用户在 android Edit-text
中从数字十进制键盘输入 2 位数字,自动设置小数点 (.)
创建 class:-
public class DecimalDigitsInputFilter implements InputFilter
{
Pattern pattern;
public DecimalDigitsInputFilter(int digitsBeforeDecimal, int digitsAfterDecimal)
{
pattern = Pattern.compile("(([1-9]{1}[0-9]{0," + (digitsBeforeDecimal - 1) + "})?||[0]{1})((\.[0-9]{0," + digitsAfterDecimal + "})?)||(\.)?");
}
@Override public CharSequence filter(CharSequence source, int sourceStart, int sourceEnd, Spanned destination, int destinationStart, int destinationEnd)
{
// Remove the string out of a destination that is to be replaced.
String newString = destination.toString().substring(0, destinationStart) + destination.toString().substring(destinationEnd, destination.toString().length());
// Add the new string in.
newString = newString.substring(0, destinationStart) + source.toString() + newString.substring(destinationStart, newString.length());
// Now check if the new string is valid.
Matcher matcher = pattern.matcher(newString);
if(matcher.matches())
{
// Returning null indicates that the input is valid.
return null;
}
// Returning the empty string indicates the input is invalid.
return "";
}
}
现在像这样设置你的编辑文本:
edittext.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(2, 2)});
使用TextWatcher
:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() == 2)
editText.append('.');
}
});
您可以使用 TextWatcher 来监听用户输入的文本,然后验证长度,然后设置输入文本的 edittext 文本 +"。"
一旦用户在 android Edit-text
中从数字十进制键盘输入 2 位数字,自动设置小数点 (.)创建 class:-
public class DecimalDigitsInputFilter implements InputFilter
{
Pattern pattern;
public DecimalDigitsInputFilter(int digitsBeforeDecimal, int digitsAfterDecimal)
{
pattern = Pattern.compile("(([1-9]{1}[0-9]{0," + (digitsBeforeDecimal - 1) + "})?||[0]{1})((\.[0-9]{0," + digitsAfterDecimal + "})?)||(\.)?");
}
@Override public CharSequence filter(CharSequence source, int sourceStart, int sourceEnd, Spanned destination, int destinationStart, int destinationEnd)
{
// Remove the string out of a destination that is to be replaced.
String newString = destination.toString().substring(0, destinationStart) + destination.toString().substring(destinationEnd, destination.toString().length());
// Add the new string in.
newString = newString.substring(0, destinationStart) + source.toString() + newString.substring(destinationStart, newString.length());
// Now check if the new string is valid.
Matcher matcher = pattern.matcher(newString);
if(matcher.matches())
{
// Returning null indicates that the input is valid.
return null;
}
// Returning the empty string indicates the input is invalid.
return "";
}
}
现在像这样设置你的编辑文本:
edittext.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(2, 2)});
使用TextWatcher
:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() == 2)
editText.append('.');
}
});
您可以使用 TextWatcher 来监听用户输入的文本,然后验证长度,然后设置输入文本的 edittext 文本 +"。"