Abh*_*waj 4 android android-spinner android-activity android-studio
我Spinner在登录页面上有三种语言选择。我希望当用户选择一种语言时假设“波斯语”整个应用程序的语言应该改变。我只能更改当前的语言Activity。如何更改整个应用程序的语言。
在 Android 中以编程方式更改语言
以下是更改应用程序区域设置的适当方法:
您必须克服一些困难才能以编程方式更改语言。
1.)在配置更改期间关闭或重新创建后,您的应用程序将不会记住您的语言更改。
2.) 您应该根据所选语言正确更新当前可见的 UI。
解决方案
“ LocaleHelper”是您需要的所有解决方案。您只需要在应用程序的主类上初始化语言环境。之后,您的所有语言更改都将保留。
在 Android API 版本 24(Nougat)最近发生变化之后,我们需要覆盖 attachBaseContext 以反映变化。
以下用于更改应用程序语言的方法:
private static boolean updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return true;
}
Run Code Online (Sandbox Code Playgroud)
在以下链接中找到更多详细信息:
小智 3
这是我的观点。它非常适合我。
请按照以下步骤更改语言。
1.将字符串保存在 value-ur(URDU) 等文件夹中
比将所有字符串转换为您必须转换的语言
2.创建一个 BaseActivity 类并将所有其余的 Activity 扩展到该 Base 类
public class BaseActivity extends AppCompatActivity
{
public BaseActivity context;
public MySharePreference mySharePreference;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
context = BaseActivity.this;
mySharePreference = MySharePreference.getInstance(context);
LocaleHelper.setLocale(context, mySharePreference.getLanguage() );
}
protected void attachBaseContext(Context base)
{
super.attachBaseContext(LocaleHelper.onAttach(base));
}
}
Run Code Online (Sandbox Code Playgroud)
3.我有一个ExampleActivity,我想翻译它的语言。
public class ExampleActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
}
}
Run Code Online (Sandbox Code Playgroud)
只需将您的应用程序扩展到 BaseActivity 类即可。
3.创建一个 LocalHelper 类,在其中更改方向和字符串。
public class LocaleHelper
{
public static Context setLocale(Context context, String language)
{
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = new Configuration(resources.getConfiguration());
configuration.setLayoutDirection(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
configuration.setLocale(locale);
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
}
else
{
configuration.locale = locale;
configuration.setLocale(locale);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1)
{
return context.createConfigurationContext(configuration);
}
else
{
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}
public static Context onAttach(Context context)
{
return setLocale(context, MySharePreference.getInstance(context).getLanguage());
}
}
Run Code Online (Sandbox Code Playgroud)
4.在你的App类中附加基础上下文
public class AppClass extends MultiDexApplication
{
private static AppClass appClass;
public static AppClass getintance()
{
return appClass;
}
@Override
public void onCreate()
{
super.onCreate();
appClass = this;
MySharePreference.getInstance(this);
}
@Override
protected void attachBaseContext(Context base)
{
super.attachBaseContext(LocaleHelper.onAttach(base));
}
}
Run Code Online (Sandbox Code Playgroud)
5.在您的 build.gradle 文件中添加最新的 androidx SharedPreferences 依赖项。
implementation 'androidx.preference:preference:1.1.1'
Run Code Online (Sandbox Code Playgroud)
6.就我而言,我创建了自己的 SharedPreferences 类。
public class MySharePreference
{
private static MySharePreference instance;
private static SharedPreferences pref;
private MySharePreference(Context context)
{
if (context != null)
{
pref = PreferenceManager.getDefaultSharedPreferences(context);
}
else
{
pref = PreferenceManager.getDefaultSharedPreferences(App.getintance());
}
}
public static MySharePreference getInstance(Context context)
{
if (instance == null || pref == null)
{
instance = new MySharePreference(context);
}
return instance;
}
public String getLanguage()
{
return pref.getString("appLanguage", "en");
}
public void setLanguage(String b)
{
pref.edit().putString("appLanguage", b).apply();
}
}
Run Code Online (Sandbox Code Playgroud)
7.最后将该语言的缩写保存到 SharedPreferences 中,在我的例子中,我有微调器。
spinner_lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (mySharePreference.getLanguagePosition() != position)
{
mySharePreference.setLanguage(Constants.COUNTRY_LIST.get(position).countryAbbr);
mySharePreference.setLanguagePosition(position);
startActivity(new Intent(mActivity, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
Run Code Online (Sandbox Code Playgroud)
现在一切都完成了……
| 归档时间: |
|
| 查看次数: |
11233 次 |
| 最近记录: |