Android EditText中文本更改时自动添加千位分隔符

Android EditText中文本更改时自动添加千位分隔符,第1张

概述我想要完成同样的任务有很多答案,甚至在我的案例中找不到任何有用的解决方案 我见过 Question 1 – 问题在于添加逗号计数“.” (DOT)也作为数值. Question 2 – 问题是它在文本更改时没有显示它的结果 Question 3 – 问题是它在第15个字母后连续添加0. 还有更多问题和答案, 但是我找不到任何符合千分离器一般要求的东西. 是否有任何有用的提示可以帮助我顺利添加千位 我想要完成同样的任务有很多答案,甚至在我的案例中找不到任何有用的解决方案

@H_502_8@

我见过@H_502_8@

Question 1 – 问题在于添加逗号计数“.” (DOT)也作为数值.@H_502_8@

Question 2 – 问题是它在文本被更改时没有显示它的结果@H_502_8@

Question 3 – 问题是它在第15个字母后连续添加0.@H_502_8@

还有更多问题和答案,@H_502_8@

但是我找不到任何符合千分离器一般要求的东西.@H_502_8@

是否有任何有用的提示可以帮助我顺利添加千位分隔符,因为文本已更改并且处理方式不同
小数点后的数字如计算器?@H_502_8@

我会感激任何帮助.@H_502_8@解决方法 我遇到了同样的问题,并为完成任务进行了大量研究以获得正确的结果.所以我终于解决了我们正在搜索的问题,并且我正在为您提供我的代码,这些代码将帮助您过多并满足您的要求.

@H_502_8@

你可以直接在我们自己的类中复制我的代码,它是完全经过测试的@H_502_8@

以下代码的详细说明@H_502_8@

>在EditText中将千位分隔符作为文本更改.
>在按下句点(.)时自动添加0.在第一位.
>在Beginning处忽略0输入.@H_502_8@

类名:NumberTextWatcherForThousand实现TextWatcher@H_502_8@

@H_502_8@

import androID.text.Editable;import androID.text.TextWatcher;import androID.Widget.EditText;import java.util.StringTokenizer;/** * Created by Shreekrishna on 12/14/2014. */public class NumberTextWatcherForThousand implements TextWatcher {    EditText editText;    public NumberTextWatcherForThousand(EditText editText) {        this.editText = editText;    }    @OverrIDe    public voID beforeTextChanged(CharSequence s,int start,int count,int after) {    }    @OverrIDe    public voID onTextChanged(CharSequence s,int before,int count) {    }    @OverrIDe    public voID afterTextChanged(Editable s) {        try        {            editText.removeTextChangedListener(this);            String value = editText.getText().toString();            if (value != null && !value.equals(""))            {                if(value.startsWith(".")){                    editText.setText("0.");                }                if(value.startsWith("0") && !value.startsWith("0.")){                    editText.setText("");                }                String str = editText.getText().toString().replaceAll(",","");                if (!value.equals(""))                editText.setText(getDecimalFormattedString(str));                editText.setSelection(editText.getText().toString().length());            }            editText.addTextChangedListener(this);            return;        }        catch (Exception ex)        {            ex.printstacktrace();            editText.addTextChangedListener(this);        }    }    public static String getDecimalFormattedString(String value)    {        StringTokenizer lst = new StringTokenizer(value,".");        String str1 = value;        String str2 = "";        if (lst.countTokens() > 1)        {            str1 = lst.nextToken();            str2 = lst.nextToken();        }        String str3 = "";        int i = 0;        int j = -1 + str1.length();        if (str1.charat( -1 + str1.length()) == '.')        {            j--;            str3 = ".";        }        for (int k = j;; k--)        {            if (k < 0)            {                if (str2.length() > 0)                    str3 = str3 + "." + str2;                return str3;            }            if (i == 3)            {                str3 = "," + str3;                i = 0;            }            str3 = str1.charat(k) + str3;            i++;        }    }    public static String trimCommaOfString(String string) {//        String returnString;        if(string.contains(",")){            return string.replace(","");}        else {            return string;        }    }}

在EditText上使用此类,如下所示@H_502_8@

@H_502_8@

editText.addTextChangedListener(new NumberTextWatcherForThousand(editText));

将输入作为纯文本输入为Double@H_502_8@

像这样使用同一个类的trimCommaOfString方法@H_502_8@

@H_502_8@

NumberTextWatcherForThousand.trimCommaOfString(editText.getText().toString());
总结

以上是内存溢出为你收集整理的Android EditText中文本更改时自动添加千位分隔符全部内容,希望文章能够帮你解决Android EditText中文本更改时自动添加千位分隔符所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1122729.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存