复选框中的值在 Android 中不起作用
Values from checkboxes not working in Android
我做了一个测试应用。当一个或多个复选框被选中并单击一个按钮时,对应于复选框的数字应该相加并显示在 textView 中(最初显示 0)。
MainActivity.java :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
CheckBox rice = findViewById(R.id.rice);
boolean ateRice = rice.isChecked();
CheckBox egg = findViewById(R.id.egg);
boolean ateEgg = egg.isChecked();
CheckBox bread = findViewById(R.id.bread);
boolean ateBread = bread.isChecked();
int cal = countCal(ateRice, ateEgg, ateBread);
String calString = String.valueOf(cal);
Button calculate = findViewById(R.id.calculate);
calculate.setOnClickListener(view -> {
TextView numCal = findViewById(R.id.cal);
numCal.setText(calString);
});
} //onCreate
private int countCal(boolean ateRice, boolean ateEgg, boolean ateBread) {
int calories = 0;
if(ateRice){
calories = calories + 20;
}
if(ateEgg){
calories = calories + 50;
}
if(ateBread){
calories = calories + 10;
}
return calories;
} //countCal
}
但是点击按钮没有任何反应,textView一直显示0,怎么办?
(Here's how the app looks)
您在 OnCreate 中进行所有计算,在 Actvity 初始化时进行,而不是在更改发生时进行,这就是侦听器发挥作用的地方。
您需要将countCal计算放在监听器中:
calculate.setOnClickListener(view -> {
TextView numCal = findViewById(R.id.cal);
int cal = countCal(ateRice, ateEgg, ateBread);
String calString = String.valueOf(cal);
numCal.setText(calString);
});
您还需要一个复选框的侦听器,只有当项目是 checked/unchecked 时,才应更新值 (ateBread)。否则它将根据复选框的初始状态进行计算
例如:
rice.setOnCheckedChangeListener{ _, isChecked ->
ateRice = isChecked
}
我做了一个测试应用。当一个或多个复选框被选中并单击一个按钮时,对应于复选框的数字应该相加并显示在 textView 中(最初显示 0)。
MainActivity.java :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
CheckBox rice = findViewById(R.id.rice);
boolean ateRice = rice.isChecked();
CheckBox egg = findViewById(R.id.egg);
boolean ateEgg = egg.isChecked();
CheckBox bread = findViewById(R.id.bread);
boolean ateBread = bread.isChecked();
int cal = countCal(ateRice, ateEgg, ateBread);
String calString = String.valueOf(cal);
Button calculate = findViewById(R.id.calculate);
calculate.setOnClickListener(view -> {
TextView numCal = findViewById(R.id.cal);
numCal.setText(calString);
});
} //onCreate
private int countCal(boolean ateRice, boolean ateEgg, boolean ateBread) {
int calories = 0;
if(ateRice){
calories = calories + 20;
}
if(ateEgg){
calories = calories + 50;
}
if(ateBread){
calories = calories + 10;
}
return calories;
} //countCal
}
但是点击按钮没有任何反应,textView一直显示0,怎么办?
(Here's how the app looks)
您在 OnCreate 中进行所有计算,在 Actvity 初始化时进行,而不是在更改发生时进行,这就是侦听器发挥作用的地方。
您需要将countCal计算放在监听器中:
calculate.setOnClickListener(view -> {
TextView numCal = findViewById(R.id.cal);
int cal = countCal(ateRice, ateEgg, ateBread);
String calString = String.valueOf(cal);
numCal.setText(calString);
});
您还需要一个复选框的侦听器,只有当项目是 checked/unchecked 时,才应更新值 (ateBread)。否则它将根据复选框的初始状态进行计算 例如:
rice.setOnCheckedChangeListener{ _, isChecked ->
ateRice = isChecked
}