试图改变Android的语言环境动态 [英] Trying to change Android Locale on the fly

查看:161
本文介绍了试图改变Android的语言环境动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉Java,但只是学习的Andr​​oid编程。我喜欢的颜色条在他们你好,例如LinearLayout中颇有几分,所以我一直在试图扩展到一个小程序,我可以了,13个月大的使用。我有它显示与标签和四行七种颜色。有一件事我想要做的(因为我是双语),是能够改变区域设置显示语言对飞,不退出应用程序,加载新的语言环境,并再次启动应用程序(它的工作原理,但它是乏味)。因此,七彩色条标有适用于所选的语言,但我想这是不是退出程序能够与程序中点击修改。换句话说,我真的只是想给应用程序,而不是整个手机的范围内更改语言环境。

我发现一对夫妇在这里区域设置更改提示,但没有在这种情况下有相当100%的作品。我做了什么是微调code与布局相结合。我试图保持在一个文件中的一切(现在)只是让我知道变量和范围都在努力(我曾经尝试过的东西来回就像在官方的Andr​​oid HelloSpinner code和,使得不过是一个烂摊子)。下面是我的codeD至今为HelloLinearLayout我的定制版本:

任何和所有的意见AP preciated!哦,我构建和测试针对姜饼,因为那是我的HTC手机运行。

包net.buellnet.hellolinearlayout;进口java.util.Locale中;进口android.app.Activity;< BR />
进口android.content.res.Configuration;< BR />
进口android.os.Bundle;< BR />
进口android.view.View;
进口android.widget.AdapterView;< BR />
进口android.widget.ArrayAdapter;< BR />
进口android.widget.Spinner;< BR />
进口android.widget.Toast;< BR />公共类HelloLinearLayoutActivity扩展活动{< BR />
    / **当第一次创建活动调用。 * /< BR />
    @越权LT; BR />
    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        微调微调=(微调)findViewById(R.id.spinner);
        ArrayAdapter<&CharSequence的GT;适配器= ArrayAdapter.createFromResource(
                对此,R.array.lang_array,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        spinner.setAdapter(适配器);
        spinner.setHapticFeedbackEnabled(真);
        spinner.setOnItemSelectedListener(NULL);
    }           公共无效onItemSelected(适配器视图<>母公司,观景,INT POS,长I​​D){                String语言= parent.getItemAtPosition(POS)的ToString();
                配置NEWCONFIG =新配置();                Toast.makeText(parent.getContext()的语言是+
                          parent.getItemAtPosition(POS)的ToString(),Toast.LENGTH_LONG).show();< BR />
/ *我把上面的行只是为了看看如果控制是工作。我可以改变的语言,但吐司行从来没有弹出,也没有语言环境不断发生变化。 * /                如果(language.equals(英语)){
                    newConfig.locale = Locale.ENGLISH;
                    super.onConfigurationChanged(NEWCONFIG);                    Locale.setDefault(newConfig.locale);
                    。getBaseContext()getResources()updateConfiguration(NEWCONFIG,getResources()getDisplayMetrics());
                }
                否则,如果(language.equals(法国)){
                    newConfig.locale = Locale.FRENCH;
                    super.onConfigurationChanged(NEWCONFIG);                    Locale.setDefault(newConfig.locale);
                    。getBaseContext()getResources()updateConfiguration(NEWCONFIG,getResources()getDisplayMetrics());
                }
                否则,如果(language.equals(德国)){
                    newConfig.locale = Locale.GERMAN;
                    super.onConfigurationChanged(NEWCONFIG);                    Locale.setDefault(newConfig.locale);
                    。getBaseContext()getResources()updateConfiguration(NEWCONFIG,getResources()getDisplayMetrics());
                }
                否则,如果(language.equals(意大利)){
                    newConfig.locale = Locale.ITALIAN;
                    super.onConfigurationChanged(NEWCONFIG);                    Locale.setDefault(newConfig.locale);
                    。getBaseContext()getResources()updateConfiguration(NEWCONFIG,getResources()getDisplayMetrics());
                }
                否则,如果(language.equals(葡萄牙)){
                    newConfig.locale =新的语言环境(PT);
                    super.onConfigurationChanged(NEWCONFIG);                    Locale.setDefault(newConfig.locale);
                    。getBaseContext()getResources()updateConfiguration(NEWCONFIG,getResources()getDisplayMetrics());
                }
                否则,如果(language.equals(西班牙)){
                    newConfig.locale =新的语言环境(ES);
                    super.onConfigurationChanged(NEWCONFIG);                    Locale.setDefault(newConfig.locale);
                    。getBaseContext()getResources()updateConfiguration(NEWCONFIG,getResources()getDisplayMetrics());
                }
            }            公共无效onNothingSelected(适配器视图父){
                  // 没做什么。
            }
}
< / code>


解决方案

尝试这种方式!请参见下面code(注意的setContentView不是onCreate方法里面了):

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    doUIStuff();
    // otherThanUILogicGoesHere
}@覆盖
公共无效onResume(){
    super.onResume();
    如果(JustModifiedLocale){
        doUIStuff();
    }
}无效doUIStuff(){
    的setContentView(R.layout.my_layout);
    //其他UI_related必要的初始化去这里
}

I'm familiar with Java, but just learning Android Programming. I liked the color bars in their Hello, LinearLayout example quite a bit, so I've been trying to expand that into a little program I can use with my 13 month old. I have it displaying seven colors with labels and four rows. One thing I'd like to do (since I'm bilingual), is to be able to change the Locale display language on the fly, without quitting the app, loading a new locale, and starting the app again (which works, but it's tedious). So the seven color bars are labelled appropriate to the selected language, but I would like those to be able to change with a click within the program, instead of exiting the program. In other words, I really only want to change Locale within the scope of the app, NOT the entire phone.

I've found a couple of tips on Locale changes here, but nothing that quite 100% works in this instance. What I've done is to combine Spinner code with the layout. I'm trying to keep everything in one file (for now) just so that I know the variables and scopes are all working (I had tried passing things back and forth just as in the "official" Android HelloSpinner code, and that made nothing but a mess). Here's what I have coded so far for my custom version of HelloLinearLayout:

Any and all advice appreciated! Oh and I'm building and testing against Gingerbread, since that's what my HTC phone runs.

package net.buellnet.hellolinearlayout;

import java.util.Locale;

import android.app.Activity;<br/>
import android.content.res.Configuration;<br/>
import android.os.Bundle;<br/>
import android.view.View;
import android.widget.AdapterView;<br/>
import android.widget.ArrayAdapter;<br/>
import android.widget.Spinner;<br/>
import android.widget.Toast;<br/>

public class HelloLinearLayoutActivity extends Activity {<br/>
    /** Called when the activity is first created. */<br/>
    @Override<br/>
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.lang_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);
        spinner.setHapticFeedbackEnabled(true);
        spinner.setOnItemSelectedListener(null);
    }

           public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

                String language = parent.getItemAtPosition(pos).toString();
                Configuration newConfig = new Configuration();

                Toast.makeText(parent.getContext(), "The language is " +
                          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();<br/>
/* I threw the above line in just to see if the control was working. I can change the "language" but the Toast line never pops up, nor does the locale ever change. */

                if (language.equals("English")) {
                    newConfig.locale = Locale.ENGLISH;
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
                else if (language.equals("French")) {
                    newConfig.locale = Locale.FRENCH;
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
                else if (language.equals("German")) {
                    newConfig.locale = Locale.GERMAN;
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
                else if (language.equals("Italian")){
                    newConfig.locale = Locale.ITALIAN;
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
                else if (language.equals("Portuguese")) {
                    newConfig.locale = new Locale("pt");
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
                else if (language.equals("Spanish")) {
                    newConfig.locale = new Locale("es");
                    super.onConfigurationChanged(newConfig);

                    Locale.setDefault(newConfig.locale);
                    getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
                }
            }

            public void onNothingSelected(AdapterView parent) {
                  // Do nothing.
            }
}
</code>

解决方案

Try it this way! See below code (notice that setContentView is not inside the onCreate method anymore):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    doUIStuff();
    //otherThanUILogicGoesHere
}

@Override
public void onResume() {
    super.onResume();
    if (JustModifiedLocale){
        doUIStuff();
    }
}

void doUIStuff(){
    setContentView(R.layout.my_layout);
    //other UI_related necessary initializations go here
}

这篇关于试图改变Android的语言环境动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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