Android的本地化问题:不是所有的布局更新项目正常进行切换时的语言环境 [英] Android Localization problem: Not all items in the layout update properly when switching locales

查看:174
本文介绍了Android的本地化问题:不是所有的布局更新项目正常进行切换时的语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的问题:当我有在后台运行的活动,我切换语言环境,而我切换回应用程序,一切都更新......除了复选框,单选按钮,有一个机器人:id属性设置

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 radio buttons that have an "android:id" attribute set.

如果该复选框和单选按钮不具备的机器人:id属性,然后他们更新确定。其他字段不存在这个问题,他们是否有一个。机器人:id属性或不

If the checkboxes and radio buttons don't have the "android:id" attribute, then they update OK. Other fields don't have this problem, whether they have an "android:id" attribute or not.

什么是确保一切都在我的跑步活动更新时的语言环境改变时,最好的方法是什么?

What is the best way to make sure everything in my running activity is updated whenever the locale is changed?

重现步骤:

1)创建一个Hello,Android的在Eclipse项目。 2)在主要布局,定义了两个复选框:

1) Create a "Hello, Android" project in Eclipse. 2) In the main layout, define two checkboxes:

<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>

3)创建两个strings.xml中:一是在值,一个在值-ES

3) Create two strings.xml: one under "values" and one under "values-es".

4)创建的值以下字符串:

4) Create the following string under "values":

<string name="checkbox">English</string>

5)根据值-ES

5) Create the following string under "values-es"

<string name="checkbox">español</string>

6)将设备设置为英语

6) Set device to "English"

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

7) Run the application on the emulator or any device (tested on HTC G1).

8)观察。两个复选框说英语。

8) Observe. Both checkboxes say "English".

9)preSS家返回菜单并离开应用程序在后台运行。

9) Press "Home" to return to the menu and leave the application running in the background.

10)进入设置。切换语言为西班牙语

10) Go to the settings. Switch the language to "español"

11)preSS并持有家。返回到应用程序。

11) Press and hold "Home". Return to the application.

预期的结果:

两个复选框说:西班牙语

Both checkboxes say "español"

实际结果:

第一个复选框说:英语

二框写着西班牙语

看来,与机器人:ID复选框属性没有更新,因为它应该。没有的复选框。机器人:id属性工作正常

It appears that the checkbox with an "android:id" attribute is not updating as it should. The checkbox without the "android:id" attribute is working as expected.

推荐答案

该问题的原因是, CompoundButton.onSaveInstanceState()电话 setFreezesText (真)从而节省与放大器;恢复文本

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

    }
}

这篇关于Android的本地化问题:不是所有的布局更新项目正常进行切换时的语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆