在android中使用SharedReference无法实现多语言支持
我正在尝试在我的应用程序中实现多语言支持 我的应用程序包含两个活动,即main activity.class和NextActivity.class 当用户从MainActivity.class选择任何语言选项时,我将在在android中使用SharedReference无法实现多语言支持,android,sharedpreferences,multilingual,Android,Sharedpreferences,Multilingual,我正在尝试在我的应用程序中实现多语言支持 我的应用程序包含两个活动,即main activity.class和NextActivity.class 当用户从MainActivity.class选择任何语言选项时,我将在SharedReference中保存所选语言,并在下一页获取SharedReference语言以更改语言类型。但无法更改语言类型 谁能帮我解决这个问题?谢谢你宝贵的时间 strings.xml(值fr) nextractivity.java public class MainAct
SharedReference
中保存所选语言,并在下一页获取SharedReference
语言以更改语言类型。但无法更改语言类型
谁能帮我解决这个问题?谢谢你宝贵的时间
strings.xml(值fr)
nextractivity.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();
}}
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);
}}
活动\u 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>
<?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>
next_layout.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>
<?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>
首先检查您是否通过输入日志来获取SharedPreference值,如果是,则将代码更改为我下面的代码
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");
}
}
将此日志放在SharedReferences对象创建日志.e(“LanguageType”,prefs.getString(“myLanguage”),之后;然后检查你得到了什么?