我试图根据我的应用程序上单击的按钮来更改总和的值,但是总和的值没有改变。
如何根据用户对按钮单击的选择来改变总和的值?谢谢。
这是我下面的代码。任何帮助将不胜感激。
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) {
}
});
}
}
答案 0 :(得分:0)
您正在检查按钮是否已“激活”,以及是否在onCreate()中选中了CheckBoxes和RadioButtons,
这意味着一旦活动开始,该测试将仅进行。
相反,请在按钮上使用setOnClickListener()
,然后使用setOnCheckedChangeListener()
用于RadioButtons和CheckBoxes,然后每当发生更改时-更改总和并设置文本。
p.s
变量开头不应以大写字母命名,
因此将String Price
更改为String price
。