Java 如何制作';双倍';与'对应;整数';

Java 如何制作';双倍';与'对应;整数';,java,android,string,integer,double,Java,Android,String,Integer,Double,我最近在我的应用程序中添加了一个方法,可以自动将文本视图的格式设置为从“50000”到“50000”,这绝对是完美的。现在我遇到的问题是,在我的应用程序中,有多个按钮函数可以添加或删除该文本视图中的特定数量,因此,让我们只说textview=“5000”,当您单击按钮时,它会删除“1000” 问题是,它强制关闭应用程序,因为从技术上讲,textview不再是一个整数,而是一个字符串。这是代码和错误 //formats the textview to show commas dou

我最近在我的应用程序中添加了一个方法,可以自动将文本视图的格式设置为从“50000”到“50000”,这绝对是完美的。现在我遇到的问题是,在我的应用程序中,有多个按钮函数可以添加或删除该文本视图中的特定数量,因此,让我们只说textview=“5000”,当您单击按钮时,它会删除“1000” 问题是,它强制关闭应用程序,因为从技术上讲,textview不再是一个整数,而是一个字符串。这是代码和错误

    //formats the textview to show commas
    double number = Double.parseDouble(textView1.getText().toString());
    DecimalFormat formatter = new DecimalFormat("###,###,###");
    textView1.setText(formatter.format(number));


    Button btnremove1000 = (Button) findViewById(R.id.btnremove1000);
    btnremove1000.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView textView1 = (TextView) findViewById(R.id.textView1);

            int amount = Integer.parseInteger(textView1.getText()
                    .toString()) - 1000;
            textView1.setText(String.valueOf(amount));

            Toast msg = Toast.makeText(MainScreen.this,
                    "1,000 removed!", Toast.LENGTH_SHORT);
            msg.show();
        }
    });

现在,我如何才能做到这一点,使我仍然可以显示逗号,但添加和删除值

我唯一能想到的就是删除逗号,添加/删除一个值,然后重新格式化以再次显示逗号?

替换此:

int amount = Integer.parseInteger(textView1.getText().toString()) - 1000;
与:

一行:

int amount = Integer.parseInteger(
                    textView1.getText().toString().replace(",", "")) - 1000;
编辑:按照Eng.Fouad的建议,使用
replace()
代替
replaceAll()

替换以下内容:

int amount = Integer.parseInteger(textView1.getText().toString()) - 1000;
与:

一行:

int amount = Integer.parseInteger(
                    textView1.getText().toString().replace(",", "")) - 1000;

编辑:按照Eng.Fouad的建议,使用
replace()
代替
replaceAll()

使用
DecimalFormat.parse()
方法,使用与最初格式化字符串相同的
DecimalFormatter

使用
DecimalFormat.parse()
方法,首先使用与设置字符串格式相同的
DecimalFormatter

一般来说,不应将数据存储在视图中

尽量把它分开

int amount = 5000000;

textView.setText(formatter.format(amount));

button.setOnClickListener(new OnclickListener(){
    public void onClick(View view){
        amount -= 1000;
        textView.setText(formatter.format(amount));
    }
});

在这里,数据存储在视图之外,易于管理。数据只能向一个方向进入视图。

一般来说,您不应该将数据存储在视图中

尽量把它分开

int amount = 5000000;

textView.setText(formatter.format(amount));

button.setOnClickListener(new OnclickListener(){
    public void onClick(View view){
        amount -= 1000;
        textView.setText(formatter.format(amount));
    }
});

在这里,数据存储在视图之外,易于管理。数据只向一个方向进入视图。

替换
int-amount=Integer.parseInteger(textView1.getText().toString())-1000此行包含以下代码:-

String strText=textView1.getText().reaplace(“,”,”)
int-amount=Integer.parseInteger(strText)-1000


还有一件事你不需要指定格式,比如##############。如果你用##########就足够了

Replace
int-amount=Integer.parseInteger(textView1.getText().toString())-1000此行包含以下代码:-

String strText=textView1.getText().reaplace(“,”,”)
int-amount=Integer.parseInteger(strText)-1000


还有一件事你不需要指定格式,比如##############。如果你用##########就足够了

删除字符串中的所有逗号就可以了。除非您正在添加其他文本。删除所有逗号形式的字符串就可以了。除非您正在添加其他文本。请使用
replace()
而不是
replaceAll()
。后者仅与正则表达式一起使用。这两个方法的名称具有误导性,它们应该将它们命名为
replaceAll()
replaceRegex()
@Eng.Fouad
这两个方法的名称具有误导性,它们应该将它们命名为replaceAll()和replaceRegex()
。有道理。顺便问一下,使用“
”、“
作为正则表达式匹配所有逗号是否错误?这就是我想要的
replaceAll(“,”,”)
@coreywillams不客气。老实说,我会同意EJP的答案。使用
replace()
而不是
replaceAll()
。后者仅与正则表达式一起使用。这两个方法的名称具有误导性,它们应该将它们命名为
replaceAll()
replaceRegex()
@Eng.Fouad
这两个方法的名称具有误导性,它们应该将它们命名为replaceAll()和replaceRegex()
。有道理。顺便问一下,使用“
”、“
作为正则表达式匹配所有逗号是否错误?这就是我想要的
replaceAll(“,”,”)
@coreywillams不客气。老实说,我会同意EJP的答案。