我正在为学校项目制作应用程序如何使用保存功能以编程方式更改语言当用户退出应用程序并再次打开它时,语言仍然是用户选择的语言。
公共类设置扩展Activity实现View.OnClickListener {
private Typeface ttf;
Spinner spinnerctrl;
Locale myLocale;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_einstellungen);
//Like Button
//Schrift & OnClickListener
ttf = Typeface.createFromAsset(getAssets(), "schrift.ttf");
((TextView)findViewById(R.id.settings_title)).setTypeface(ttf);
((TextView)findViewById(R.id.appinfo)).setTypeface(ttf);
findViewById(R.id.appinfo).setOnClickListener(this);
((TextView)findViewById(R.id.settings_back)).setTypeface(ttf);
((TextView)findViewById(R.id.settings_rate)).setTypeface(ttf);
findViewById(R.id.settings_back).setOnClickListener(this);
//Spinner Language
spinnerctrl = (Spinner) findViewById(R.id.spinner);
spinnerctrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 1) {
setLocale("ar");
} else if (pos == 2) {
setLocale("en");
} else if (pos == 3) {
setLocale("de");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, Settings.class);
startActivity(refresh);
finish();
}
@Override
public void onBackPressed() {
finish();
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.appinfo) {
showAppinfo();
} else if(view.getId()==R.id.settings_back) {
finish();
}
}
private void showAppinfo() {
final Dialog dialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.dialog_appinfo);
((TextView)(dialog.findViewById(R.id.appinfo_herrausgeber))).setTypeface(ttf);
((TextView)(dialog.findViewById(R.id.appinfo_name))).setTypeface(ttf);
((TextView)(dialog.findViewById(R.id.appinfo_version))).setTypeface(ttf);
((TextView)(dialog.findViewById(R.id.appinfo_developer))).setTypeface(ttf);
((TextView)(dialog.findViewById(R.id.appinfo_back))).setTypeface(ttf);
((TextView)(dialog.findViewById(R.id.appinfo_version_number))).setTypeface(ttf);
dialog.findViewById(R.id.appinfo_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
return;
}
});
dialog.show();
}
public void bewertung_internet (View view) {
goToUrl("https://play.google.com/store/apps/details?id=de.developer.sixtysix.hiddenandroid");
}
private void goToUrl (String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
守则。但是在另一个活动中。我通过点击MainActivity中的按钮进入活动,所以我需要一个脚本来保存在设置活动中并在主活动的开始时加载。
Thanks谢谢。添
答案 0 :(得分:0)
存储语言
SharedPreferences settings = getSharedPreferences(appName,0);
settings.getString("key", "defaultvalue");
获取语言
void print_digits(int n) {
// In case n is negative, print the leading '-'
// and transform n to a non-negative number
if(n < 0) {
printf("-");
n = -n;
}
if(n/10) {
print_digits(n/10);
}
printf("%d", n%10);
}