布局文件中写入

<CheckBox
    android:id="@+id/mcBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false"
    android:text="是"
    android:textSize="25sp"/>

Java文件中写入,使MainActivity知道此选项是否被选中

public class MainActivity extends Activity{
    private CheckBox checkBox;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        init();
        //设置监听器
       checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {
               //通过判断ischecked来判断控件是否被选中
               if (ischecked){
                   //如果被下选中,获取其文本内容并打印
                   String text = checkBox.getText().toString();
                   Log.i("tag",text);
               }
               else if (ischecked==false){
                   Log.i("tag","否");
               }
           }
       });
    }
    public void init(){
        checkBox = findViewById(R.id.mcBox);
    }
}

最后效果:
CheckBox

相关文章:

  • 2022-12-23
  • 2021-07-18
  • 2021-12-31
  • 2021-11-17
  • 2021-05-20
  • 2021-08-03
  • 2021-08-12
  • 2021-09-13
猜你喜欢
  • 2022-01-10
  • 2021-06-24
  • 2022-02-27
  • 2021-11-07
相关资源
相似解决方案