安卓系统中如何使用芬兰湾的科特林实现点击更换全局语言

  介绍

这篇文章给大家分享的是有关android中如何使用芬兰湾的科特林实现点击更换全局语言的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

<强>注:在这里我要说一下,我知道芬兰湾的科特林不太普及,如果有的同学需要java版的,可以在通读一遍代码,了解了之后把芬兰湾的科特林转化为java,因为芬兰湾的科特林与java是互通的,代码的一些关键点,爪哇语言该怎么写还怎么写,如果有不明白的可以留言

第一步:简单写一下选择语言的布局就好,会用到点击事件,因为我要用到三种语言,可以按钮控件,TextView控件,都可以

第二步:可以看下面截图

1。右键res


 android中如何使用芬兰湾的科特林实现点击更换全局语言“> <br/> </p> <p> 2.新→android资源文件</p> <p> <br/> <img src=

第二步:这里要用到CommonUtil工具类,因为芬兰湾的科特林与java是互通的,我把代码写在下面可以直接用

public  class  CommonUtil  {   ,public  static  void  configLanguage (Context  mContext, String 语言),{   ,Configuration  config =, mContext.getResources () .getConfiguration ();   ,if  (Build.VERSION.SDK_INT 祝辞=,Build.VERSION_CODES.HONEYCOMB), {   ,if  (language.equals (“CHINESE")), {=,,config.locale  Locale.SIMPLIFIED_CHINESE;   ,}else  if  (language.equals (“ENGLISH")), {=,,config.locale  Locale.US;   ,}else 如果(language.equals (“JAPANESE")) {=,,config.locale  Locale.JAPAN;   ,}else  {=,,config.locale  Locale.SIMPLIFIED_CHINESE;   ,}   ,}else  {   ,if  (language.equals (“CHINESE")), {=,,config.locale  Locale.CHINESE;   ,}else  if  (language.equals (“ENGLISH")), {=,,config.locale  Locale.ENGLISH;   ,}else  if  (language.equals (“JAPANESE")) {=,,config.locale  Locale.JAPAN;   ,}else  {=,,config.locale  Locale.CHINESE;   ,}   ,}   ,mContext.getResources () .updateConfiguration(配置,,空);   ,}   }

第四步。然后在主页面进行跳转和调用,LanguageActivity就是需要改变控件语言的界面,下面会有activity_language界面代码

override  fun  onClick (v:视图),{   ,当(v.id) {   ,R.id.tvChinese→{   ,CommonUtil.configLanguage(这一点,“CHINESE")   ,startActivity ()   ,}   ,R.id.tvEnglish→{   ,CommonUtil.configLanguage(这一点,“ENGLISH")   ,startActivity ()   ,}   ,R.id.tvJan→{   ,CommonUtil.configLanguage(这一点,“JAPANESE")   ,startActivity ()   ,}   ,}   以前,}

第五步:activity_language代码

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   LinearLayout & lt;   ,xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,xmlns:应用=癶ttp://schemas.android.com/apk/res-auto"   ,android:取向=皏ertical"   ,android: layout_width=癿atch_parent"   ,android: layout_height=癿atch_parent"祝辞   & lt; TextView   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=癅string/text1"   ,android:填充=?0 dp"   ,android: textSize=?5 sp"   ,/比;   & lt; TextView   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=癅string/text2"   ,android:填充=?0 dp"   ,android: textSize=?5 sp"   ,/比;   & lt; TextView   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=癅string/text3"   ,android:填充=?0 dp"   ,android: textSize=?5 sp"   ,/比;   & lt; TextView   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=癅string/text4"   ,android:填充=?0 dp"   ,android: textSize=?5 sp"   ,/比;   & lt; TextView   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=癅string/text5"   ,android:填充=?0 dp"   ,android: textSize=?5 sp"   ,/比;   & lt; TextView   ,android: layout_width=癿atch_parent"   ,android: layout_height=皐rap_content"   ,android:文本=癅string/text6"   ,android:填充=?0 dp"   ,android: textSize=?5 sp"   ,/比;   null

安卓系统中如何使用芬兰湾的科特林实现点击更换全局语言