Android Localization problem: Not all items in the layout update properly when switching locales

前端 未结 4 940
北海茫月
北海茫月 2021-02-07 18:27

Here\'s the problem: When I have an activity running in the background, and I switch locales, and I switch back to the application, everything updates... EXCEPT checkboxes and r

4条回答
  •  粉色の甜心
    2021-02-07 19:18

    The cause of the problem is that CompoundButton.onSaveInstanceState() calls setFreezesText(true) and thus saves&restores the text.

    A simple solution is using a subclass like this:

    public class CheckBoxNoPersistentText extends CheckBox {
    
        public CheckBoxNoPersistentText(final Context context) {
            super(context);
        }
    
        public CheckBoxNoPersistentText(final Context context, final AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CheckBoxNoPersistentText(final Context context, final AttributeSet attrs, final int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public void onRestoreInstanceState(final Parcelable state) {
    
            final CharSequence text = getText(); // the text has been resolved anew
    
            super.onRestoreInstanceState(state); // this restores the old text
    
            setText(text); // this overwrites the restored text with the newly resolved text
    
        }
    }
    

提交回复
热议问题