1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
List<String> list = new ArrayList<>();
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
CheckBox apple = findViewById(R.id.apple); CheckBox banana = findViewById(R.id.banana); CheckBox grape = findViewById(R.id.grape); CheckBox orange = findViewById(R.id.orange); CheckBox peach = findViewById(R.id.peach);
apple.setOnCheckedChangeListener(this); banana.setOnCheckedChangeListener(this); grape.setOnCheckedChangeListener(this); orange.setOnCheckedChangeListener(this); peach.setOnCheckedChangeListener(this);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = " "; for (String s : list) { text = text + s + " "; }
Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show(); } }); }
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked){ list.add(buttonView.getText().toString()); }else{ list.remove(buttonView.getText().toString()); } } }
|