1

在我的应用程序中,我尝试通过单击按钮(英语/意大利语)来更改语言。我选择默认语言环境英语。当打开活动并按下意大利语按钮时,它发生了变化,但之后我无法选择英语。换句话说,语言只改变了一次。我做错了什么?

public class MainActivity extends DefaultActivity implements View.OnClickListener {
    String LOCALE_ITALIAN = "it";
    String LOCALE_ENGLISH = "en";
    Locale mLocale;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLocale = new Locale(LOCALE_ENGLISH);
        Locale.setDefault(mLocale);
        Configuration config = new Configuration();
        config.locale = mLocale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());

        setContentView(R.layout.main);
        findButton(R.id.english).setOnClickListener(this);
        findButton(R.id.italian).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.english:
                mLocale = new Locale(LOCALE_ENGLISH);
                Locale.setDefault(mLocale);
                Configuration config = new Configuration();
                config.locale = mLocale;
                getBaseContext().getResources().updateConfiguration(config,
                        getBaseContext().getResources().getDisplayMetrics());
                MainActivity.this.setContentView(R.layout.main);

                break;
            case R.id.italian:
                mLocale = new Locale(LOCALE_ITALIAN);
                Locale.setDefault(mLocale);
                Configuration config1 = new Configuration();
                config1.locale = mLocale;
                getBaseContext().getResources().updateConfiguration(config1,
                        getBaseContext().getResources().getDisplayMetrics());
                MainActivity.this.setContentView(R.layout.main);

                break;
        }
    }
}
4

1 回答 1

3

我运行您的代码并找到了解决方案。当您通过调用 setContentView 更新视图时,您必须再次调用按钮 onclick。我相信它会起作用的。

@Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.english:
                mLocale = new Locale(LOCALE_ENGLISH);
                Locale.setDefault(mLocale);
                Configuration config = new Configuration();
                config.locale = mLocale;
                getBaseContext().getResources().updateConfiguration(config,
                        getBaseContext().getResources().getDisplayMetrics());
                NonOrangeUserActivity.this.setContentView(R.layout.non_orange_user);
                findButton(R.id.english).setOnClickListener(this);
                findButton(R.id.italian).setOnClickListener(this);

                break;
            case R.id.italian:
                mLocale = new Locale(LOCALE_ITALIAN);
                Locale.setDefault(mLocale);
                Configuration config1 = new Configuration();
                config1.locale = mLocale;
                getBaseContext().getResources().updateConfiguration(config1,
                        getBaseContext().getResources().getDisplayMetrics());
                NonOrangeUserActivity.this.setContentView(R.layout.non_orange_user);
                findButton(R.id.english).setOnClickListener(this);
                findButton(R.id.italian).setOnClickListener(this);

                break;
        }
    }
于 2013-09-06T11:49:35.597 回答