在运行时改变Lang

时间:2017-04-03 23:15:42

标签: java android

这是我的改变郎功能

public static String currectLang = "distance";

public static void changeLang(Activity a)
{

    if(currectLang.equals("distance"))
    {
        currectLang ="iw";

    }
    else
    {
        currectLang = "distance";
    }
    Resources res = a.getResources();
    // Change locale settings in the app.
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.setLocale(new Locale(currectLang)); // API 17+ only.
    // Use conf.locale = new Locale(...) if targeting lower versions
    res.updateConfiguration(conf, dm);


}

}

这是MainActivity:

Button lang,caller,music,media;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //change lang Button
    lang = (Button)findViewById(R.id.btnLang);
    lang.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Func.changeLang(MainActivity.this);
            setContentView(R.layout.activity_main);
            MainActivity.this.recreate();
        }
    });

    //Move to Caller Activity Button
    caller = (Button)findViewById(R.id.btnCaller);
    caller.setOnClickListener(MainActivity.this);

    //Move to Caller Activity Button
    music = (Button)findViewById(R.id.btnMusic);
    music.setOnClickListener(MainActivity.this);

    //Move to Caller Activity Button
    media = (Button)findViewById(R.id.btnMedia);
    media.setOnClickListener(MainActivity.this);

}


@Override
public void onClick(View v) {
    int id = v.getId();
    Intent moveActivity = new Intent(MainActivity.this,CallerActivity.class);
    switch(id)
    {
        case R.id.btnCaller:
            moveActivity.setClass(MainActivity.this,CallerActivity.class);
            break;

        case R.id.btnMedia:
            moveActivity.setClass(MainActivity.this,CallerActivity.class);
            break;

        case R.id.btnMusic:
            moveActivity.setClass(MainActivity.this,CallerActivity.class);
            break;
    }
    startActivity(moveActivity);
}

public void onResume()
{
    super.onResume();
    Locale current = getResources().getConfiguration().locale;



}

}

我有第二个运行相同changelang函数的活动
问题是Activitys不会更新他们的lang,如果我在一个activty上使用该函数,在另一个中停留lang。我该如何解决?

0 个答案:

没有答案