我正在尝试在我的应用程序中实施多语言支持。
我的应用包含两个活动,即 MainActivity.class 和 NextActivity.class 。
当用户从 MainActivity.class 中选择任何语言选项时,我会将所选语言保存在sharedpreference
中并在下一页中获取sharedpreference
语言以更改语言类型。但无法更改语言类型。
任何人都可以帮我解决这个问题吗?谢谢你宝贵的时间!..
strings.xml(values-fr)
<resources>
<string name="app_name">myAPP</string>
<string name="action_settings">Settings</string>
<string name="hello">Bonjour !..</string>
</resources>
strings.xml(values-hi)
<resources>
<string name="app_name">myAPP</string>
<string name="action_settings">Settings</string>
<string name="hello">नमस्ते !..</string>
</resources>
MainActivity.java
public class MainActivity extends AppCompatActivity {
CheckBox chk_english, chk_french, chk_hindi;
Button btn_next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
chk_french = (CheckBox) findViewById(R.id.chk_french);
chk_hindi = (CheckBox) findViewById(R.id.chk_hindi);
btn_next = (Button) findViewById(R.id.btn_next);
chk_french.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()) {
chk_hindi.setChecked(false);
}
saveLanguage("fr");
}
});
chk_hindi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()) {
chk_french.setChecked(false);
}
saveLanguage("hi");
}
});
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, NextActivity.class));
}
});
}
public void saveLanguage(String type) {
SharedPreferences.Editor editor = getSharedPreferences("MY_LANGUAGE", MODE_PRIVATE).edit();
editor.putString("myLanguage", type);
editor.apply();
editor.commit();
}}
NextActivity.java
public class NextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next_layout);
}
@Override
protected void onResume() {
super.onResume();
loadLanguage();
}
public void loadLanguage() {
SharedPreferences prefs = getSharedPreferences("MY_LANGUAGE", MODE_PRIVATE);
String myLang = prefs.getString("myLanguage", "");
if (myLang.equals("fr")) {
setLanguage("fr");
} else if (myLang.equals("hi")) {
setLanguage("hi");
} else if (myLang.equals("en")) {
setLanguage("en");
}
}
void setLanguage(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.next_layout);
}}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="test.org.myapp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
<CheckBox
android:id="@+id/chk_french"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="French"
android:paddingLeft="10dp"
android:layout_marginBottom="30dp"/>
<CheckBox
android:id="@+id/chk_hindi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hindi"
android:paddingLeft="10dp"
android:layout_marginBottom="30dp"
/>
<Button
android:id="@+id/btn_next"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Next"
android:background="@color/colorPrimary"
android:textColor="@android:color/white"
/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
next_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="test.org.myapp.NextActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textStyle="bold"
android:textSize="20dp"/>
</LinearLayout>
答案 0 :(得分:1)
首先检查您是否通过输入日志来获取sharedprefrence值,如果是,则将您的代码更改为我的以下代码。
Log.e("LanguageType", prefs.getString("myLanguage", ""));
将您的代码更改为我的以下代码,确保它适合您
public void loadLanguage() {
SharedPreferences prefs = getSharedPreferences("MY_LANGUAGE", MODE_PRIVATE);
// String myLang = prefs.getString("myLanguage", "");
if (prefs.getString("myLanguage", "").equals("fr")) {
setLanguage("fr");
} else if (prefs.getString("myLanguage", "").equals("hi")) {
setLanguage("hi");
} else if (prefs.getString("myLanguage", "").equals("en")) {
setLanguage("en");
}
}