尝试即时更改Android区域设置

时间:2012-06-15 21:29:56

标签: android locale

我熟悉Java,但只是学习Android编程。我很喜欢他们的Hello,LinearLayout示例中的颜色条,所以我一直在尝试将它扩展为我可以在我13个月大的时候使用的一个小程序。我有它显示七种颜色标签和四行。我想做的一件事(因为我是双语),是能够动态更改Locale显示语言,无需退出应用程序,加载新的语言环境,再次启动应用程序(这有效,但是这很乏味。因此,七个颜色条被标记为适合所选语言,但我希望这些颜色条可以通过程序中的单击进行更改,而不是退出程序。换句话说,我真的只想在应用范围内更改Locale,而不是整个手机。

我在这里找到了一些关于Locale更改的提示,但在这个实例中没有100%的工作。我所做的是将Spinner代码与布局结合起来。我正在尝试将所有内容保存在一个文件中(现在)只是为了让我知道变量和范围都在起作用(我试过来回传递事物就像在“官方”Android HelloSpinner代码中那样,并且没有做任何事情但是一团糟)。这是我到目前为止编写的HelloLinearLayout自定义版本的代码:

任何和所有建议表示赞赏!哦,我正在构建和测试Gingerbread,因为那就是我的HTC手机。

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>

2 个答案:

答案 0 :(得分:0)

 try updateConfiguration function to set new Locale........


  Resources res = context.getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(language_code.toLowerCase());
res.updateConfiguration(conf, dm);

取代spinner.setOnItemSelectedListener(null);

1 - public class HelloLinearLayoutActivity extends Activit implements Spinner.OnItemSelectedListener
2- spinner.setOnItemSelectedListener(this);

答案 1 :(得分:0)

以这种方式试试吧!请参阅下面的代码(注意setContentView不再位于onCreate方法中):

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