Android开发中怎么在App中实现一个内语言切换功能

发布时间:2020-12-04 16:15:45 作者:Leah
来源:亿速云 阅读:324

这期内容当中小编将会给大家带来有关Android开发中怎么在App中实现一个内语言切换功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

代码实现:

布局文件(Data-Binding模式),很简单就是两行文字

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
 <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
tools:context="com.tnnowu.android.switchlanguage.MainActivity">
 <TextView
 android:id="@+id/titleTextView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:text="@string/title"
 android:textSize="30sp"
 android:textStyle="bold" />
 <TextView
 android:id="@+id/descTextView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/titleTextView"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="10dp"
 android:text="@string/desc"
 android:textSize="20sp" />
 </RelativeLayout>
</layout>

从实例中我们可以看到右上角是有Menu

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 tools:context=".MainActivity">

 <item
 android:id="@+id/language_english"
 android:orderInCategory="100"
 android:title="@string/menu_english" />
 <item
 android:id="@+id/language_simplified_chinese"
 android:orderInCategory="100"
 android:title="@string/menu_simplified_chinese" />
 <item
 android:id="@+id/language_turkish"
 android:orderInCategory="100"
 android:title="@string/menu_turkish" />
 <item
 android:id="@+id/language_japanese"
 android:orderInCategory="100"
 android:title="@string/menu_japanese" />
</menu>

(既然是多语言,所以就要有N个strings)

Android开发中怎么在App中实现一个内语言切换功能

本案例我创建了4种语言。

好的,Menu的布局写完了,接下来就是实现Menu功能,记住实现Menu就两套代码,一个 onCreateOptionsMenu , 另一个是 onOptionsItemSelected 。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(R.menu.menu, menu);
 return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
 int id = item.getItemId();
 if (id == R.id.language_english) {
 updateViews("en");
 } else if (id == R.id.language_simplified_chinese) {
 updateViews("zh");
 } else if (id == R.id.language_turkish) {
 updateViews("tr");
 } else if (id == R.id.language_japanese) {
 updateViews("ja");
 }
 return super.onOptionsItemSelected(item);
}

在这里,可以看到,我们自定义一个 updateViews() 方法,用来实现切换预言时界面的改变

private void updateViews(String languageCode) {
 Context context = LocaleHelper.setLocale(this, languageCode);
 Resources resources = context.getResources();
 mBinding.titleTextView.setText(resources.getString(R.string.title));
 mBinding.descTextView.setText(resources.getString(R.string.desc));
 setTitle(resources.getString(R.string.toolbar_title));
}

公布一个 语言判断的类 LocaleHelper

public class LocaleHelper {
 private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
 public static Context onAttach(Context context) {
 String lang = getPersistedData(context, Locale.getDefault().getLanguage());
 return setLocale(context, lang);
 }
 public static Context onAttach(Context context, String defaultLanguage) {
 String lang = getPersistedData(context, defaultLanguage);
 return setLocale(context, lang);
 }
 public static String getLanguage(Context context) {
 return getPersistedData(context, Locale.getDefault().getLanguage());
 }
 public static Context setLocale(Context context, String language) {
 persist(context, language);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  return updateResources(context, language);
 }
 return updateResourcesLegacy(context, language);
 }
 private static String getPersistedData(Context context, String defaultLanguage) {
 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
 return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
 }
 private static void persist(Context context, String language) {
 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
 SharedPreferences.Editor editor = preferences.edit();
 editor.putString(SELECTED_LANGUAGE, language);
 editor.apply();
 }
 @TargetApi(Build.VERSION_CODES.N)
 private static Context updateResources(Context context, String language) {
 Locale locale = new Locale(language);
 Locale.setDefault(locale);
 Configuration configuration = context.getResources().getConfiguration();
 configuration.setLocale(locale);
 return context.createConfigurationContext(configuration);
 }
 @SuppressWarnings("deprecation")
 private static Context updateResourcesLegacy(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 context;
 }
}

最后还要做的操作就是,自定义一个Application类,用来设定App的默认语言(当然了,要将这个Application应用到Manifest中)

public class BaseApplication extends Application {
 @Override
 protected void attachBaseContext(Context base) {
 super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
 }
}

上述就是小编为大家分享的Android开发中怎么在App中实现一个内语言切换功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 怎么在iOS中实现app间跳转功能
  2. iOS中在APP内加入AppStore评分功能的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

app android pp

上一篇:按钮点击事件与长按事件在Android项目中的区别是什么

下一篇:如何在Android应用中调用系统自带的分享功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》