使用带有按钮和开关的复选框

35g0bw71  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(430)

我试图用选中复选框的颜色名称来显示toast,但是当我单击按钮时,什么也没有发生,我试图使用linearlayout作为所有视图的父视图来获取选中复选框的id,首先,我尝试在主活动中将其创建为linearlayout,但当我选中一个复选框并单击按钮时,应用程序停止,然后我将其更改为视图并将其强制转换为linearlayout
这是我的密码

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. View view = (LinearLayout)findViewById(R.id.view);
  7. Button btn = findViewById(R.id.button1);
  8. CheckBox chk1 = findViewById(R.id.chk1);
  9. CheckBox chk2 = findViewById(R.id.chk2);
  10. CheckBox chk3 = findViewById(R.id.chk3);
  11. btn.setOnClickListener(v -> {
  12. switch (view.getId()) {
  13. case R.id.chk1:
  14. if (chk1.isChecked())
  15. Toast.makeText(MainActivity.this, "Green", Toast.LENGTH_LONG).show();
  16. break;
  17. case R.id.chk2:
  18. if (chk2.isChecked())
  19. Toast.makeText(MainActivity.this, "Orange", Toast.LENGTH_LONG).show();
  20. break;
  21. case R.id.chk3:
  22. if (chk3.isChecked())
  23. Toast.makeText(MainActivity.this, "Orange", Toast.LENGTH_LONG).show();
  24. break;
  25. default:
  26. }
  27. });
  28. }

布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity"
  9. android:id="@+id/view"
  10. >
  11. <TextView
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_marginTop="16dp"
  15. android:layout_gravity="center"
  16. android:text="@string/app_name"
  17. android:textColor="@color/black"
  18. android:textAlignment="center"
  19. android:textSize="24sp"
  20. />
  21. <CheckBox
  22. android:id="@+id/chk1"
  23. android:text="Green"
  24. android:textSize="33sp"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. >
  28. </CheckBox>
  29. <CheckBox
  30. android:id="@+id/chk2"
  31. android:text="Orange"
  32. android:textSize="33sp"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. >
  36. </CheckBox>
  37. <CheckBox
  38. android:id="@+id/chk3"
  39. android:text="Blue"
  40. android:textSize="33sp"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. >
  44. </CheckBox>
  45. <Button
  46. android:id="@+id/button1"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_marginTop="16dp"
  50. android:layout_gravity="center"
  51. android:text="@string/button1"
  52. android:textAllCaps="false"
  53. android:textSize="24sp"
  54. >
  55. </Button>
  56. </LinearLayout>
hrirmatl

hrirmatl1#

  1. btn.setOnClickListener(v -> {
  2. switch (view.getId()) {
  3. case R.id.chk1:
  4. if (chk1.isChecked())
  5. Toast.makeText(MainActivity.this, "Green", Toast.LENGTH_LONG).show();
  6. break;
  7. case R.id.chk2:
  8. if (chk2.isChecked())
  9. Toast.makeText(MainActivity.this, "Orange", Toast.LENGTH_LONG).show();
  10. break;
  11. case R.id.chk3:
  12. if (chk3.isChecked())
  13. Toast.makeText(MainActivity.this, "Orange", Toast.LENGTH_LONG).show();
  14. break;
  15. default:
  16. }
  17. });

根本原因
当用户点击 btn 巴顿,你检查一下 view 与一个 CheckBox 或者不是,但它总是错误的,因为它们是具有不同id的不同视图。这就是为什么在屏幕上看不到任何toast。
解决方案
不需要使用switch case,只需要使用if语句,就像这样。

  1. btn.setOnClickListener(v -> {
  2. StringBuilder color = new StringBuilder();
  3. if (chk1.isChecked()) {
  4. color.append(chk1.getText().toString()).append(" ");
  5. }
  6. if (chk2.isChecked()) {
  7. color.append(chk2.getText().toString()).append(" ");
  8. }
  9. if (chk3.isChecked()) {
  10. color.append(chk3.getText().toString());
  11. }
  12. Toast.makeText(MainActivity.this, color.toString().trim(), Toast.LENGTH_LONG).show();
  13. });

注:如果您想让用户一次选择一种颜色,请使用 RadioButton 相反 CheckBox .
更新:如果您真的想使用switch case和checkbox,那么这里有一个解决方案。

  1. public class MainActivity extends AppCompatActivity {
  2. private CheckBox latestCheckedCheckBox;
  3. private View.OnClickListener onCheckBoxClickedListener = new View.OnClickListener() {
  4. @Override
  5. public void onClick(View v) {
  6. CheckBox checkBox = (CheckBox) v;
  7. if (checkBox.isChecked()) {
  8. latestCheckedCheckBox = checkBox;
  9. }
  10. }
  11. };
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. View view = (LinearLayout) findViewById(R.id.view);
  17. Button btn = findViewById(R.id.button1);
  18. CheckBox chk1 = findViewById(R.id.chk1);
  19. CheckBox chk2 = findViewById(R.id.chk2);
  20. CheckBox chk3 = findViewById(R.id.chk3);
  21. chk1.setOnClickListener(onCheckBoxClickedListener);
  22. chk2.setOnClickListener(onCheckBoxClickedListener);
  23. chk3.setOnClickListener(onCheckBoxClickedListener);
  24. btn.setOnClickListener(v -> {
  25. if (latestCheckedCheckBox == null) {
  26. return;
  27. }
  28. String color = "";
  29. switch (latestCheckedCheckBox.getId()) {
  30. case R.id.chk1:
  31. if (chk1.isChecked()) {
  32. color = chk1.getText().toString();
  33. Toast.makeText(MainActivity.this, color, Toast.LENGTH_LONG).show();
  34. }
  35. break;
  36. case R.id.chk2:
  37. if (chk2.isChecked()) {
  38. color = chk2.getText().toString();
  39. Toast.makeText(MainActivity.this, color, Toast.LENGTH_LONG).show();
  40. }
  41. break;
  42. case R.id.chk3:
  43. if (chk3.isChecked()) {
  44. color = chk3.getText().toString();
  45. Toast.makeText(MainActivity.this, color, Toast.LENGTH_LONG).show();
  46. }
  47. break;
  48. default:
  49. break;
  50. }
  51. });
  52. }
  53. }
展开查看全部

相关问题