简体   繁体   中英

How d I reset checkboxes to unchecked when my screen orientation has changed in android?

I have checkboxes that can be checked. However if these are checked I would like to have them all unchecked if the screen orientation has changed. How can I go about doing something like that?

The onCreate() callback method will be called with savedInstanceState object so you can check if it's not null then uncheck all the checkboxes.

@Override protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   ....
   if(savedInstanceState != null) {
      // uncheck all of the checkboxes.
   }
}

Try this,

//step 1 use this methode that loops al layout looking for checkboxes and reset them void unCheckAllCheckBoxs(ViewGroup youRootView) {

    for (int i = youRootView.getChildCount() - 1; i >= 0; i--) {
        final View child = youRootView.getChildAt(i);
        if (child instanceof ViewGroup) {
            unCheckAllCheckBoxs((ViewGroup) child);
        } else {
            if ((child != null) && (child instanceof CheckBox)) {

                CheckBox checkBox = (CheckBox) child;
                checkBox.setSelected(false);
            }
        }
    }

}

//step 2 overrige onConfigurationChanged methode to detect orientation changing and call the previous pethode inside passing that parent you root layout that contains all the checkbox as parameter.

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        unCheckAllCheckBoxs((ViewGroup) findViewById(R.id.root));
    }

let me now if it works .

I partially solved the problem by overriding onConfigurationChanged by checking if the orientation changed and if so checkbox.toggle() . Only problem that still persists is that now when the orientation changes and checkboxes are clicked they don't set the visibility of the objects in my code as opposed to before the configuration change.

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