我正在开发一个应该支持英语和阿拉伯语的Android应用程序。我已经完成了 一个。为英语和阿拉伯语创建值文件夹并更新使用的字符串。 湾为本地化创建了一个Application类 C。在Activity
中单击按钮实现代码请参阅我的申请文件
MyApp.java
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import java.util.Locale;
public class MyApp extends Application{
@Override
public void onCreate() {
super.onCreate();
}
public static void setLocaleAr (Context context){
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);
}
public static void setLocaleEn (Context context){
Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);
}
}
我的活动课使用按钮点击
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.SimpleAdapter;
import java.util.Locale;
public class SplashActivity extends Activity implements View.OnClickListener{
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
Button buttonEnglish,buttonArabic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
buttonEnglish =(Button) findViewById(R.id.buttonEnglish);
buttonArabic =(Button) findViewById(R.id.buttonArabic);
buttonEnglish.setOnClickListener(this);
buttonArabic.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.buttonEnglish:
MyApp.setLocaleEn(SplashActivity.this);
setLocale("en");
break;
case R.id.buttonArabic: {
//buttonArabic.setBackground(getDrawable(R.color.white));
MyApp.setLocaleAr(SplashActivity.this);
setLocale("ar");
break;
}
}
}
}
答案 0 :(得分:3)
/**Hey Just forget everything and just implement the below code Snipeet
I am sure it will work for you**/
Inside your build.gradle file
defaultConfig
{
resConfigs "en", "ar"
}
And inside your activity:
@Override
public void onClick(View view)
{
switch(view.getId())
{
case R.id.buttonEnglish:
updateLanguage("en");
break;
case R.id.buttonArabic:
updateLanguage("ar");
break;
}
}
@Override
public void recreate()
{
if (android.os.Build.VERSION.SDK_INT >= 14)
{
super.recreate();
}
else
{
startActivity(getIntent());
finish();
}
}
private void updateLanguage(String language)
{
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",language);
editor.apply();
recreate();
}
答案 1 :(得分:1)
您需要重新创建活动。
您可以从Splash Activity类本身更新语言环境,使用此方法更新语言环境
//input: local values like "en","fr", "it"
public void setLocale(String locale) {
// Update Locale in App Configuration
getResources().getConfiguration().setLocale(new Locale(locale));
getResources().updateConfiguration(getResources().getConfiguration(), getResources().getDisplayMetrics());
// Restart Activity
finish();
overridePendingTransition(0, 0);
startActivity(getIntent());
overridePendingTransition(0, 0);
}