Android本地化问题:切换区域设置时,布局中并非所有项目都能正确更新

Kev*_*vin 13 layout android localization

这是问题所在:当我在后台运行一个活动,并切换区域设置,然后切换回应用程序时,所有内容都会更新... EXCEPT复选框和具有"android:id"属性集的单选按钮.

如果复选框和单选按钮没有"android:id"属性,则它们会更新OK.其他字段没有这个问题,是否具有"android:id"属性.

在更改语言环境时,确保运行活动中的所有内容都更新的最佳方法是什么?

重现步骤:

1)在Eclipse中创建一个"Hello,Android"项目.2)在主布局中,定义两个复选框:

<CheckBox android:text="@string/checkbox" android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<CheckBox android:text="@string/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
Run Code Online (Sandbox Code Playgroud)

3)创建两个strings.xml:一个在"values"下,一个在"values-es"下.

4)在"values"下创建以下字符串:

<string name="checkbox">English</string>
Run Code Online (Sandbox Code Playgroud)

5)在"values-es"下创建以下字符串

<string name="checkbox">español</string>
Run Code Online (Sandbox Code Playgroud)

6)将设备设置为"英语"

7)在仿真器或任何设备上运行应用程序(在HTC G1上测试).

8)观察.两个复选框都说"英语".

9)按"Home"返回菜单,让应用程序在后台运行.

10)转到设置.将语言切换为"español"

11)按住"Home".返回申请.

预期结果:

两个复选框都说"español"

实际结果:

第一个复选框说"英语"

第二个复选框说"español"

看来带有"android:id"属性的复选框没有按预期更新.没有"android:id"属性的复选框正在按预期工作.

小智 8

问题的原因是CompoundButton.onSaveInstanceState()调用setFreezesText(true),从而保存和恢复文本.

一个简单的解决方案是使用这样的子类:

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

    }
}
Run Code Online (Sandbox Code Playgroud)


Com*_*are 5

这是一个迷人的错误.我可以在我的Nexus One上重现它.

它似乎是在默认的实现中onSaveInstanceState().如果你将其重写为无操作(不链接到超类),问题就会消失.

默认情况下onSaveInstanceState()应该处理像复选框状态这样的东西,但是它们必须具有拙劣的功能,并且也在保存文本.

所以,你有几个解决方法:

  1. 覆盖onSaveInstanceState()并且不链接到超类.但是,这会消除您通常会获得的任何自动状态保存.
  2. onRestoreInstanceState()(...我认为...)中,在链接到超类之后,setText()使用正确的字符串资源调用受影响的小部件,将其重置为正确的值.

当我有机会时,我会尝试更多地跟进这个.我想检查源代码,并可能将此作为一个问题提交.


Man*_*ser 0

如果其中一些在内存中,它们就不会改变。如果您重新启动手机,重新安装应用程序或至少完全终止该应用程序,它将正常工作。