Java 需要一个int变量来更新,只需按下一个按钮,并将输出返回到文本视图,包括£;符号

Java 需要一个int变量来更新,只需按下一个按钮,并将输出返回到文本视图,包括£;符号,java,android,android-studio,Java,Android,Android Studio,我有一个基本的工作ie,在点击按钮后一次更新textview,但是我需要用一个符号来更新它。一次点击=1英镑,两次点击=2英镑 btnClick.setOnClickListener(new View.OnClickListener() { //Setting up what happens when button btnClick is clicked, below code is executed @Override public void

我有一个基本的工作ie,在点击按钮后一次更新textview,但是我需要用一个符号来更新它。一次点击=1英镑,两次点击=2英镑

 btnClick.setOnClickListener(new View.OnClickListener() {
        //Setting up what happens when button btnClick is clicked, below code is executed
        @Override
        public void onClick(View v) {
            String countValue=(txtCount.getText().toString());
            // Creating new variable (countValue) with the value of txtCount and converting to string
            int intCountValue=Integer.parseInt(countValue);
            intCountValue++;
            //Creating new integer variable (intCountValue) with number value parsed from string variable (countValue) plus 1
            txtCount.setText("£"+String.valueOf(intCountValue));
            //Updates the text in txtCount to the new string value of (intCountValue)

        }
    });
    btnReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String countValue=(txtCount.getText().toString());
            // Creating new variable (countValue) with the value of txtCount and converting to string
            int intCountValue=Integer.parseInt(countValue);
            //Creating new integer variable (intCountValue) with number value parsed from string variable (countValue) plus 1
            txtCount.setText("£"+String.valueOf(intCountValue));
            //Updates the text in txtCount to the new string value of (intCountValue)
            txtCount.setText("£"+String.valueOf(0));
试试这个

txtCount.setText("£"+String.valueOf(intCountValue));

将符号附加到文本中,如 setText(“£”+字符串.valueOf(intCountValue))


您的计数值应该是一个属性,并使用格式化程序来显示它

intCountValue++;
txtCount.setText(String.format("£%d", intCountValue));
事实上,该属性应该是模型的一部分,文本标签应该侦听其更改以显示它。使用javafx的属性,它可以是:

txtCount.textProperty().bind(Bindings.format("£%d", model.countProperty());

(其中textCount是一个标签)

。。。您的问题是?如何在输出中实现符号?当我尝试时,我得到一个错误,告诉我£0是一个无效的整数。将符号添加到您的文本中,如
setText(“£”+String.valueOf(intCountValue))
通过我的启动屏幕崩溃,粘贴您的代码,并以黄色突出显示不要将显示的文本与setText连接起来,请使用带有占位符的资源字符串。使用“$”创建资源字符串。然后用R.string.nameOfDollarString获取它,但尽管如此,它还能工作吗?工作得很好,救生员非常感谢。我真的可以厚颜无耻地问你是否可以解释一下代码中发生了什么。我对这个很陌生,真的很想了解它!再次感谢您,绑定只是一种简单的方式,可以轻松地实现模式观察者,因为javafx属性可以为您实现它。事实上,每次countProperty值更改时,都会应用您提供的格式计算textCount。
txtCount.textProperty().bind(Bindings.format("£%d", model.countProperty());