android settings 语言列表,使用偏好设置在运行时更改语言[android]

我读过几次official guide,还有很多其他内容,例如本网站上的问题,但我无法让我的工作100%

截至目前,如果我在列表首选项中更改语言,则活动(SettingsActivity)不会自行更新(例如20次中的1次,某种程度上它会更新,不知道为什么).按返回按钮后,我的MainActivity也没有更新.但是我所有其他活动始终显示正确的语言,并且如果我重新启动该应用程序,则可以回到SettingsActivity,与MainActivity相同.

相关零件:

主要活动:

public class MainActivity extends Activity {

public static final String KEY_PREF_LANGUAGE = "pref_language";

@Override

protected void onCreate(Bundle savedInstanceState) {

...

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

}

@Override

protected void onResume() {

super.onResume();

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

String languagePref_ID = sharedPref.getString(KEY_PREF_LANGUAGE, "2");

switch (languagePref_ID) {

case "1":

Locale localeEN = new Locale("en_US");

setLocale(localeEN);

break;

case "2":

Locale localeHU = new Locale("hu_HU");

setLocale(localeHU);

break;

}

}

public void setLocale(Locale locale) {

Locale.setDefault(locale);

Resources res = getResources();

DisplayMetrics dm = res.getDisplayMetrics();

Configuration conf = res.getConfiguration();

conf.locale = locale;

res.updateConfiguration(conf, dm);

//recreate();

//finish();

//startActivity(getIntent());

//if these are not commented, main activity wont show at start at all

}

SettingsActivity

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

public static final String KEY_PREF_LANGUAGE = "pref_language";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

getFragmentManager().beginTransaction()

.replace(android.R.id.content, new SettingsFragment())

.commit();

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

SharedPreferences.OnSharedPreferenceChangeListener listener =

new SharedPreferences.OnSharedPreferenceChangeListener() {

public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {

if (key.equals(KEY_PREF_LANGUAGE)) {

String languagePref_ID = prefs.getString(SettingsActivity.KEY_PREF_LANGUAGE, "");

switch (languagePref_ID) {

case "1":

Locale localeEN = new Locale("en_US");

setLocale(localeEN);

break;

case "2":

Locale localeHU = new Locale("hu_HU");

setLocale(localeHU);

break;

}

}

}

};

sharedPref.registerOnSharedPreferenceChangeListener(listener);

}

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

if (key.equals(KEY_PREF_LANGUAGE)) {

String languagePref_ID = sharedPreferences.getString(SettingsActivity.KEY_PREF_LANGUAGE, "2");

switch (languagePref_ID) {

case "1":

Locale localeEN = new Locale("en_US");

setLocale(localeEN);

break;

case "2":

Locale localeHU = new Locale("hu_HU");

setLocale(localeHU);

break;

}

}

//this doenst even get called but i need it for the implementation

}

public void setLocale(Locale locale) {

Locale.setDefault(locale);

Resources res = getResources();

DisplayMetrics dm = res.getDisplayMetrics();

Configuration conf = res.getConfiguration();

conf.locale = locale;

res.updateConfiguration(conf, dm);

recreate(); //tried like 4 ways to do this, nothing really worked

//finish();

//startActivity(getIntent());

}

}

SettingsFragment.java

public class SettingsFragment extends PreferenceFragment {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Load the preferences from an XML resource

addPreferencesFromResource(R.xml.preferences);

}

}

AndroidManifest.xml中

android:configChanges="locale|orientation" //added to main and settings activity

我为此感到疯狂,这是最佳的工作状态,但是我知道这里的活动生命周期管理远非最佳,当我尝试使用这些方法时,我通常无法正常工作.我该如何进行这项工作,使其实际上在所有活动的运行时更新?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值