Android 不带符号的货币输入编辑框

Android 不带符号的货币输入编辑框,android,regex,editbox,currency-formatting,Android,Regex,Editbox,Currency Formatting,我正在尝试为我的android应用程序创建一个正则表达式,以便货币的格式与此问题的顶部答案相同: public void onTextChanged(CharSequence s, int start, int before, int count) { if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$")) { String userInput= ""

我正在尝试为我的android应用程序创建一个正则表达式,以便货币的格式与此问题的顶部答案相同:

public void onTextChanged(CharSequence s, int start,

        int before, int count) {
    if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))
    {
        String userInput= ""+s.toString().replaceAll("[^\\d]", "");
        StringBuilder cashAmountBuilder = new StringBuilder(userInput);

        while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
            cashAmountBuilder.deleteCharAt(0);
        }
        while (cashAmountBuilder.length() < 3) {
            cashAmountBuilder.insert(0, '0');
        }
        cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
        cashAmountBuilder.insert(0, '$');

        cashAmountEdit.setText(cashAmountBuilder.toString());
    }

}
public void onTextChanged(字符序列,int start,
前整数,整数计数){
如果(!s.toString().matches(“^\\$(\\d{1,3}(\\,\\d{3})*\\(\\d+)(\\\.\\d{2})?$”)
{
字符串userInput=“”+s.toString().replaceAll(“[^\\d]”,“”);
StringBuilder cashAmountBuilder=新的StringBuilder(用户输入);
while(cashAmountBuilder.length()>3&&cashAmountBuilder.charAt(0)='0'){
cashAmountBuilder.deleteCharAt(0);
}
while(cashAmountBuilder.length()<3){
cashAmountBuilder.insert(0,'0');
}
插入(cashAmountBuilder.length()-2',”;
cashAmountBuilder.insert(0,“$”);
cashAmountEdit.setText(cashAmountBuilder.toString());
}
}

我希望能够使用与该示例相同的格式,只需在数字之前减去美元符号,但我真的不确定如何修改代码以实现它,或者是否有其他方法

编辑:经过更多的评论后,我发现这可能不是我的代码中的这一部分。我可以通过一个非常愚蠢的练习来实现这一点,方法是在正则表达式中的空白处更改美元符号,并在数字前输入一个空格,然后在需要值时对其进行修剪,但我找不到更好的解决方法

private TextWatcher billWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))
        {
            String userInput= ""+s.toString().replaceAll("[^\\d]", "");
            StringBuilder cashAmountBuilder = new StringBuilder(userInput);

            while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
                cashAmountBuilder.deleteCharAt(0);
            }
            while (cashAmountBuilder.length() < 3) {
                cashAmountBuilder.insert(0, '0');
            }
            cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
            cashAmountBuilder.insert(0, '$');     

            billBox.setText(cashAmountBuilder.toString());
            billBox.setTextKeepState(cashAmountBuilder.toString());
            Selection.setSelection(billBox.getText(), cashAmountBuilder.toString().length());
        }
    }
private TextWatcher billWatcher=new TextWatcher(){
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
public void onTextChanged(字符序列、int start、int before、int count){
如果(!s.toString().matches(“^\\$(\\d{1,3}(\\,\\d{3})*\\(\\d+)(\\\.\\d{2})?$”)
{
字符串userInput=“”+s.toString().replaceAll(“[^\\d]”,“”);
StringBuilder cashAmountBuilder=新的StringBuilder(用户输入);
while(cashAmountBuilder.length()>3&&cashAmountBuilder.charAt(0)='0'){
cashAmountBuilder.deleteCharAt(0);
}
while(cashAmountBuilder.length()<3){
cashAmountBuilder.insert(0,'0');
}
插入(cashAmountBuilder.length()-2',”;
cashAmountBuilder.insert(0,“$”);
setText(cashAmountBuilder.toString());
setTextKeepState(cashAmountBuilder.toString());
Selection.setSelection(billBox.getText(),cashAmountBuilder.toString().length());
}
}
盒子的XML

 <EditText
        android:id="@+id/billBox"
        android:layout_width="152dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/billText"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:digits="0123456789."
        android:gravity="right"
        android:inputType="numberDecimal" />

删除此行

cashAmountBuilder.insert(0, '$');

取出美元符号只需取出以下行:cashAmountBuilder.insert(0,“$”);。

您有:

然而,正则表达式有
\\$(\\d{1,3}…
),它转换为“美元符号后面跟着更多的数字。”

我认为系统很困惑,您要求在XML中显示美元符号,但禁止它


我会取出正则表达式的第一部分,并为用户输入美元符号。

我认为您试图做的事情太多了,系统已经可以为您做了。如果您在内部将货币值保持为双精度值,并且只在需要显示时格式化它,这会容易得多。请参阅此链接中的我的解决方案。它是啊,帮帮忙。

我的应用程序崩溃,我将从受此框影响的类中粘贴更多代码。添加了带美元符号的代码的工作版本。删除上述方法将在启动时完全崩溃应用程序。我已将此标记为正确,因为您提示我解决问题,我忘记删除\\以及regex开头的美元符号,问题已解决。谢谢!