Android 首选项中的语言更改,显示对话框并重新启动应用程序
在我的应用程序中,我有一个设置片段,这个片段实现了一个Android 首选项中的语言更改,显示对话框并重新启动应用程序,android,android-fragments,dialog,settings,preference,Android,Android Fragments,Dialog,Settings,Preference,在我的应用程序中,我有一个设置片段,这个片段实现了一个onPreferenceChangeListerner接口,并且列表器仅用于特定的首选项(是一个ListPreference)。在setting fragment中,我可以更改应用程序的语言,现在我想向用户显示一个对话框,提醒他们如果他们更改了语言,应用程序将重新启动。用户可以选择确认或取消 public boolean onPreferenceChange(Preference preference, Object newValue) {
onPreferenceChangeListerner
接口,并且列表器仅用于特定的首选项(是一个ListPreference)。在setting fragment中,我可以更改应用程序的语言,现在我想向用户显示一个对话框,提醒他们如果他们更改了语言,应用程序将重新启动。用户可以选择确认或取消
public boolean onPreferenceChange(Preference preference, Object newValue) {
if(preference.getKey().equals("pref_language")){
showLanguageChangeDialog();
}
return true;
private void showLanguageChangeDialog(final String newValue){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getString(R.string.string_language_change_dialog_title))
.setMessage(getString(R.string.string_language_change_dialog_message))
.setPositiveButton(R.string.string_dialog_confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Resources resources = SettingsFragment.this.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
switch (newValue){
case "English":
config.locale = Locale.ENGLISH;
break;
case "German":
config.locale = Locale.GERMAN;
break;
case "Chinese":
config.locale = Locale.SIMPLIFIED_CHINESE;
break;
default:
break;
}
resources.updateConfiguration(config, dm);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
SettingsFragment.this.restartApp();
return null;
}
}.execute();
}
})
.setNegativeButton(R.string.string_dialog_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create().show();
}
private void restartApp() {
Intent i = getActivity().getPackageManager().getLaunchIntentForPackage(getActivity().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
getActivity().finish();
}
public boolean onPreferenceChange(首选项首选项,对象newValue){
if(preference.getKey().equals(“pref_语言”)){
showLanguageChangeDialog();
}
返回true;
私有void showLanguageChangeDialog(最终字符串newValue){
AlertDialog.Builder=新建AlertDialog.Builder(getActivity());
builder.setTitle(getString(R.string.string\u语言\u更改\u对话框\u标题))
.setMessage(getString(R.string.string\u语言\u更改\u对话框\u消息))
.setPositiveButton(R.string.string_对话框_确认,新建对话框接口.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
Resources=SettingsFragment.this.getResources();
DisplayMetrics dm=resources.getDisplayMetrics();
Configuration config=resources.getConfiguration();
开关(新值){
案例“英语”:
config.locale=locale.ENGLISH;
打破
“德语”一案:
config.locale=locale.derman;
打破
案例“中文”:
config.locale=locale.SIMPLIFIED\u中文;
打破
违约:
打破
}
updateConfiguration(配置,dm);
新建异步任务(){
@凌驾
受保护的Void doInBackground(Void…参数){
setingsFragment.this.restartApp();
返回null;
}
}.execute();
}
})
.setNegativeButton(R.string.string_dialog_cancel,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
}
})
.create().show();
}
私有void restartApp(){
Intent i=getActivity().getPackageManager().getLaunchIntentForPackage(getActivity().getPackageName());
i、 添加标志(意图、标志、活动、清除、顶部);
i、 addFlags(意图、标志、活动、新任务);
星触觉(i);
getActivity().finish();
}
点击对话框中的“确认”重新启动应用程序工作正常,但现在我面临一些问题:
onPreferenceChange
返回true,则首选项将被更新,如果返回false,则不会更新。但如何返回false取决于用户的操作感谢您的帮助!!!使用intent重新启动应用程序没有用,只需重新创建当前活动或片段。请参阅下面的链接-->谢谢,但在我的应用程序中,我需要获取一些数据,具体取决于语言,其他配置也应该更改。因此,重新启动应用程序是必要的。