简体   繁体   English

在 android 工作室中使用 CheckBox 设置 TableRow 的可见性

[英]setVisibility of TableRow using CheckBox in android studio

I am currently working on android studio, but I cannot understand why my code does not work.我目前正在 android 工作室工作,但我不明白为什么我的代码不起作用。

    if (paidCheck.isChecked()) {
        paidRow.setVisibility(View.VISIBLE);
    } else {
        paidRow.setVisibility(View.GONE);
    }

As you can see, variables end with Checks are CheckBoxes, and end with Rows are TableRows.如您所见,以 Checks 结尾的变量是 CheckBoxes,以 Rows 结尾的变量是 TableRows。 My problem is even if I tap the CheckBoxes, TableRows do not appear in the app.我的问题是即使我点击 CheckBoxes,TableRows 也不会出现在应用程序中。 Is there anything I did wrong?是不是我做错了什么?

The logic you have implemented gets called once whenever your activity or Fragment loads up, it does not gets called whenever you check or uncheck again.每当您的活动或 Fragment 加载时,您已实现的逻辑就会被调用一次,每当您再次选中或取消选中时,它都不会被调用。

What you need is a CheckedChangeListener which will be as follows您需要的是一个CheckedChangeListener ,如下所示

paidCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              if(isChecked){
               // do somtheing when is checked
               paidRow.setVisibility(View.VISIBLE);
                 }else{
                       paidRow.setVisibility(View.GONE);
                 }

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM