简体   繁体   中英

CheckBox setChecked has no effect

I've read all the answers to this question on here and nothing has helped yet.

public class ServerConfigFragment extends Fragment {

  private CheckBox checkBox_HDMI = null;
  private Handler updateScreen;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  updateScreen = new Handler();

  checkBox_HDMI = (CheckBox) rootView.findViewById(R.id.checkBox_Server_Conf_HDMI);

  checkBox_HDMI.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            m_commsController.sendCmd(VideoStreamSettings.sendHdmiCommand(isChecked));
        }
    });

  final boolean hdmi = getHdmiSettingFromServer();

  updateScreen.post(new Runnable() {
            @Override
            public void run() {  
              checkBox_HDMI.setSelected(hdmi);
            }
          });
  }
}

So upon my app's initial launch it querys the server's hdmi status and I want to set my checkbox to match that. I can see from the log I'm getting the correct value but so far I haven't been able to get my checkbox to check on/off programmatically. I've tried with and without a Handler , hardcoding true and false into setChecked() , and placing the code in onResume . Any ideas?

Since you are using a check box and want to programmatically check the check box

Use this

checkBox_HDMI.setChecked(hdmi);

Instead of

checkBox_HDMI.setSelected(hdmi);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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