Java 单击按钮时,总和的值不会更改
我正在尝试根据我的应用程序上单击的按钮更改sum的值,但sum的值没有更改 如何根据用户选择的按钮点击来获取要更改的总和值?多谢各位 这是我的代码附在下面。任何帮助都将不胜感激Java 单击按钮时,总和的值不会更改,java,android,Java,Android,我正在尝试根据我的应用程序上单击的按钮更改sum的值,但sum的值没有更改 如何根据用户选择的按钮点击来获取要更改的总和值?多谢各位 这是我的代码附在下面。任何帮助都将不胜感激 package com.example.admin.project3; import android.annotation.SuppressLint; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import an
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")
@Override
protected 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");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}您正在检查按钮是否已“激活”,以及您的复选框和单选按钮是否已在onCreate()中选中,
这意味着该测试只在活动开始后进行 相反,在按钮上使用
setOnClickListener()
,以及setOnCheckedChangeListener()
对于单选按钮和复选框,然后,每当发生更改时,更改总和并设置文本 p、 s
变量不应以大写字母开头命名,
因此,将
String Price
更改为String Price
那么它应该是这样的:thickButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){});对于按钮,它是setOnClickListener
,请在这里阅读更多信息-谢谢我让它工作了!意识到我可以将我想要的一切添加到我的seekbar进度更改侦听器中。