Java Android-根据选择的单选按钮添加数字

Java Android-根据选择的单选按钮添加数字,java,android,Java,Android,我正在尝试制作一个应用程序,用户可以选择电脑部件,该应用程序会实时更新价格。我现在的做法是,当选择某个东西时,它会覆盖其他选择的东西的价格,并用它自己的价格替换它。这就是我所拥有的: package com.bigtooth.uacs.pcbuild; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget

我正在尝试制作一个应用程序,用户可以选择电脑部件,该应用程序会实时更新价格。我现在的做法是,当选择某个东西时,它会覆盖其他选择的东西的价格,并用它自己的价格替换它。这就是我所拥有的:

package com.bigtooth.uacs.pcbuild;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import static com.bigtooth.uacs.pcbuild.R.id.pc;
import static com.bigtooth.uacs.pcbuild.R.id.price;
import static com.bigtooth.uacs.pcbuild.R.id.ram;

public class BuyExperionActivity extends AppCompatActivity {

private RadioGroup radioGroupPc;
private RadioGroup radioGroupRam;
private RadioButton e5, e7, e9, ram8, ram16, ram32;
private TextView price;
private int ex5 = 550;
private int ex7 = 700;
private int ex9 = 1250;
private int ram8g = 0;
private int ram16g = 35;
private int ram32g = 70;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_buy_experion);

    e5 = (RadioButton)findViewById(R.id.e5);
    e7 = (RadioButton)findViewById(R.id.e7);
    e9 = (RadioButton)findViewById(R.id.e9);
    ram8 = (RadioButton)findViewById(R.id.ram8);
    ram16 = (RadioButton)findViewById(R.id.ram16);
    ram32 = (RadioButton)findViewById(R.id.ram32);
    radioGroupPc = (RadioGroup)findViewById(pc);
    price = (TextView)findViewById(R.id.price);

    price.setText("$0");

    final RadioGroup categoryGroup = (RadioGroup) findViewById(pc);
    categoryGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.e5:
                    price.setText("$" + ex5);
                    ram8.setChecked(true);
                    break;
                case R.id.e7:
                    price.setText("$" + ex7);
                    ram8.setChecked(true);
                    break;
                case R.id.e9:
                    price.setText("$" + ex9);
                    ram16.setChecked(true);
                    break;
            }
        }
    });

    RadioGroup categoryRam = (RadioGroup) findViewById(ram);
    categoryRam.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch(checkedId) {
                case R.id.ram8:
                    price.setText("$" + ram8g);
                    break;
                case R.id.ram16:
                    price.setText("$" + ram16g);
                    break;
                case R.id.ram32:
                    price.setText("$" + ram32g);
                    break;
            }
        }
    });
}
}

如何使所选项目的价格相加?

您可以使用附加新值,而不是替换前一个值。例如:
price.append(“$”+ram32g)

为了实现您正在尝试的功能,您首先需要创建2个
文本视图。第一个
文本视图将文本设置为“$”。第二个将包含该值(默认值为0)

接下来,您可以执行以下操作:

categoryRam.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int amount;
        switch(checkedId) {
            case R.id.ram8:
                amount = ram8g;
                break;
            case R.id.ram16:
                amount = ram16g;
                break;
            case R.id.ram32:
                amount = ram32g;
                break;
        }
            secondTextView.setText(amount + Integer.parseInt(secondTextView.getText()));
    }
});

它说在secondTextView.getText()中存在一些问题;错误说明了什么?将其更改为getText().toString()