我试图根据在我的应用程序上单击的按钮更改 sum 的值,但 sum 的值没有改变。如何根据用户选择的按钮单击来更改 sum 的值?谢谢你。这是我在下面附上的代码。任何帮助将不胜感激。package com.example.admin.project3;import android.annotation.SuppressLint;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.RadioButton;import android.widget.SeekBar;import android.widget.TextView;public class Project3 extends AppCompatActivity {private TextView textView;@SuppressLint("SetTextI18n")@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_project3); SeekBar seekBar = findViewById(R.id.mySeekbar); textView = findViewById(R.id.mySeekBarNumber); TextView myPrice = findViewById(R.id.myPrice); RadioButton thickButton = findViewById(R.id.Thick); RadioButton soggyButton = findViewById(R.id.Soggy); RadioButton deliveryButton = findViewById(R.id.Deliver); CheckBox anch = findViewById(R.id.Anchovies); CheckBox pine = findViewById(R.id.Pineapple); CheckBox garlic = findViewById(R.id.Garlic); CheckBox okra = findViewById(R.id.Okra); double myInches = Double.parseDouble(textView.getText().toString()); double sum = 0; if(thickButton.isActivated()) sum += 2.50; if(soggyButton.isActivated()) sum += 5.00; if(deliveryButton.isActivated()) sum += 3.00; if(anch.isChecked()) sum += .05*myInches; if(pine.isChecked()) sum += .05*myInches; if(garlic.isChecked()) sum += .05*myInches; if(okra.isChecked()) sum += .05*myInches; sum += myInches*.05; String Price = Double.toString(sum); myPrice.setText(Price); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @SuppressLint("SetTextI18n") @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { textView.setText(progress + " in"); } });}}
1 回答
慕运维8079593
TA贡献1876条经验 获得超5个赞
您正在检查按钮是否已“激活”,以及是否在 onCreate() 中检查了 CheckBoxes 和 RadioButtons,
这意味着此测试只会在活动开始后进行。
相反,setOnClickListener()
在您的按钮、setOnCheckedChangeListener()
RadioButtons 和 CheckBoxes 上使用,然后,每当发生更改时 - 更改总和并设置文本。
ps
变量不应该以大写字母开头,
所以String Price
改为String price
.
添加回答
举报
0/150
提交
取消