Java 当“if(is checked=true)”时,如何在复选框中显示文本
我有多个复选框,如果按下,我希望显示每个复选框的文本 如图所示 当我点击汉堡包三明治复选框时,我需要显示文本“汉堡包三明治” 但是,当没有按下该框时,我不想显示任何文本 使用EditText中输入的数量Java 当“if(is checked=true)”时,如何在复选框中显示文本,java,android,Java,Android,我有多个复选框,如果按下,我希望显示每个复选框的文本 如图所示 当我点击汉堡包三明治复选框时,我需要显示文本“汉堡包三明治” 但是,当没有按下该框时,我不想显示任何文本 使用EditText中输入的数量 对于复选框上的其余复选框也是如此,请在文本视图中设置一个将更新文本的侦听器 下面是一个带有两个复选框的示例: public class ShoppingActivity extends Activity { CheckBox checkBox1, checkBox2; Text
对于复选框上的其余复选框也是如此,请在文本视图中设置一个将更新文本的侦听器 下面是一个带有两个复选框的示例:
public class ShoppingActivity extends Activity {
CheckBox checkBox1, checkBox2;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping);
// The checkbox for Hamburger
checkBox1 = findViewById(R.id.checkBox1);
// The checkbox for Cheese Burger
checkBox2 = findViewById(R.id.checkBox2);
// The textView where you display the selected things
textView = findViewById(R.id.textView);
// Add listeners to your checkboxes to tell them to update the text view when they are clicked
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateTextView();
}
});
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateTextView();
}
});
}
// This updates the TextView depending on what is checked
private void updateTextView() {
String text = "";
if(checkBox1.isChecked()) {
text += "Hamburger\n";
}
if(checkBox2.isChecked()) {
text += "Cheese Burger\n";
}
textView.setText(text);
}
}
谢谢,但是我在哪里可以包含我想要显示的文本呢?我还是一个初学者,所以如果你愿意,我需要一个详细的解释allow@NouranKatana事实上我想我误解了你的问题,你只有1条文本,对吗?你想把所有检查过的东西都写进去吗?对吗?事实上,我有多个复选框,每个复选框都有一个编辑文本。单击复选框时,我希望显示复选框名称及其编辑文本中输入的值。例如,我有一盒汉堡包三明治,我想在选择汉堡包三明治时显示一个文本,其中包含所选三明治的名称以及所需的编号,通过选择所需的复选框,我知道在完成后按账单上的按钮后,我要显示的文本会出现在屏幕上。@Bentaye.非常感谢先生,他工作顺利,非常出色!如果我的回答解决了你的问题,请投票并接受。