英文:
multi language change in android studio app
问题
我正在处理一个项目,需要设置用户可以选择的语言。
当我使用旧版本的Material Design库时,它工作正常,但如果我想将依赖项更改为AndroidX(Android V30),它就无法正常工作。
这意味着如果我使用以下代码:
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
它可以正常工作,但如果我使用以下代码:
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
它就无法正常工作。无法正常工作意味着应用程序会启动,但无法正常运行。
我的主要Activity代码如下:
package com.faranesh.saman.multilanguage2020;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LoadLocale();
setContentView(R.layout.activity_main);
ActionBar actionBar=getSupportActionBar();
actionBar.setTitle(getResources().getString(R.string.app_name));
btn=(Button)findViewById(R.id.btn_chang_language);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ChangeLanguageState();
}
});
}
public void ChangeLanguageState(){
final String[] listItem=new String[]{"English","فارسی"};
AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle("Choose one Language....");
dialog.setSingleChoiceItems(listItem, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which==0){
setLocale("en");
recreate();
}else if (which==1){
setLocale("fa");
recreate();
}
dialog.dismiss();
}
});
AlertDialog builder=dialog.create();
builder.show();
}
public void setLocale(String lang){
Locale locale=new Locale(lang);
Locale.setDefault(locale);
Configuration configuration=new Configuration();
configuration.locale=locale;
getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor=getSharedPreferences("settings",MODE_PRIVATE).edit();
editor.putString("MY_LANG",lang);
editor.apply();
}
public void LoadLocale(){
SharedPreferences sharepref=getSharedPreferences("settings",Activity.MODE_PRIVATE);
String language=sharepref.getString("MY_LANG","");
setLocale(language);
}
}
英文:
i am working on a project that i need to have some settings that users can choose there language
when I use the old version of material design librarys , it work well
but if i want to change the dependencys to androidX (android V30)
it not work
it's mean that if i use
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
it work but if i use
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
it not work
not work means (the app will be launch but not work well)
my main Activity is
package com.faranesh.saman.multilanguage2020;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LoadLocale();
setContentView(R.layout.activity_main);
ActionBar actionBar=getSupportActionBar();
actionBar.setTitle(getResources().getString(R.string.app_name));
btn=(Button)findViewById(R.id.btn_chang_language);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ChangeLanguageState();
}
});
}
public void ChangeLanguageState(){
final String[] listItem=new String[]{"English","فارسی"};
AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle("Chosse one Language....");
dialog.setSingleChoiceItems(listItem, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which==0){
setLocale("en");
recreate();
}else if (which==1){
setLocale("fa");
recreate();
}
dialog.dismiss();
}
});
AlertDialog builder=dialog.create();
builder.show();
}
public void setLocale(String lang){
Locale locale=new Locale(lang);
Locale.setDefault(locale);
Configuration configuration=new Configuration();
configuration.locale=locale;
getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor=getSharedPreferences("settings",MODE_PRIVATE).edit();
editor.putString("MY_LANG",lang);
editor.apply();
}
public void LoadLocale(){
SharedPreferences sharepref=getSharedPreferences("settings",Activity.MODE_PRIVATE);
String language=sharepref.getString("MY_LANG","");
setLocale(language);
}
}
答案1
得分: 1
你应该在调用setContentView之前一起调用setLocal和LoadLocal函数,就像下面的代码一样:
private void changeLanguage(String language) {
sharedPreferences.edit().putString(Constant.LANGUAGE_KEY, language).apply();
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = new Configuration(getResources().getConfiguration());
configuration.setLayoutDirection(locale);
configuration.locale = locale;
configuration.setLocale(locale);
getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
getResources().getConfiguration().setLocale(locale);
}
然后将getResources().getConfiguration()
传递给configuration
。
希望这对你有帮助
英文:
You should call setLocal and LoadLocal function together before setContentView. like below code:
private void changeLanguage(String language) {
sharedPreferences.edit().putString(Constant.LANGUAGE_KEY, language).apply();
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = new
Configuration(getResources().getConfiguration());
configuration.setLayoutDirection(locale);
configuration.locale = locale;
configuration.setLocale(locale);
getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
getResources().getConfiguration().setLocale(locale);
}
and pass getResources().getConfiguration() to configuration
i hope work for you:)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论