gpt4 book ai didi

安卓 : How to prevent users from entering numbers with more than 3 decimal places

转载 作者:行者123 更新时间:2023-12-02 15:05:00 27 4
gpt4 key购买 nike

我的应用程序中有一个 EditText,我不希望用户在其中输入小数点后三位以上的数字。这是因为在将存储我从手机发送的数据的 SQL Server 数据库中具有以下数据类型:

numeric(15, 3)

你知道我该怎么做吗?

我已经设置了这些值,但它们只能部分帮助我:

android:maxLength="15"
android:lines="1"
android:inputType="numberDecimal"

编辑

这是我尝试过的:

            mQuantityEditText.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
String str = mQuantityEditText.getText().toString();
DecimalFormat format=(DecimalFormat) DecimalFormat.getInstance();
DecimalFormatSymbols symbols=format.getDecimalFormatSymbols();
char sep=symbols.getDecimalSeparator();


int indexOFdec = str.indexOf(sep);

if(indexOFdec >=0) {
if(str.substring(indexOFdec,str.length()-1).length() >3)
{
s.replace(0, s.length(),str.substring(0, str.length()-1));
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {


}
});

它之所以有效,是因为它只允许 3 位小数,但我仍然不确定如何控制最大位数以适应 numeric(15,3)

提前致谢。

最佳答案

For the limited number of character, you can add a Android API filter :

InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(8);
editEntryView.setFilters(FilterArray);

For the numeric editText, there is also a obscur solution :

DigitsKeyListener MyDigitKeyListener = new DigitsKeyListener(true, true); 
editEntryView.setKeyListener( MyDigitKeyListener );

关于安卓 : How to prevent users from entering numbers with more than 3 decimal places,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26424498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com