我试图创建一个应用程序,用户可以选择计算机的部件,应用程序实时更新价格。我现在拥有它的方式,当选择某些东西时,它会覆盖所选其他任何东西的价格并用它自己替换它。这就是我所拥有的:
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;
}
}
});
}
}
如何将所选项目的价格加在一起?
答案 0 :(得分:0)
您可以使用append(CharSequence text)附加新值,而不是替换上一个值。例如:price.append("$" + ram32g);
答案 1 :(得分:0)
为了实现您的目标,您首先需要创建2 TextViews
。第一个TextView
,文本设置为" $"。第二个将包含该值(默认情况下它将为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()));
}
});