现在的位置: 首页 > 编程语言 > 正文

android使用kotlin实现点击更换全局语言(中日英切换)

2020年02月13日 编程语言 ⁄ 共 3117字 ⁄ 字号 评论关闭

> 因为我的工作要用kotlin所以今天在这里给大家总结一下关于全局语言切换的kotlin语言实现实现,很简单,希望在这里可以帮助到有需要的同学,下面简单说一下实现步骤,会把运行截图放在最后<

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

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

第二步:可以看下面截图

1.右键res

2.new–>android resource file

3.输入filename,在下满local选择需要的语言

4.最后像这样,然后在里面输入所需要控件的语言,在xml空间中运用到,比如 android:text=“@strings/定义的名字”,注意这4个string里面所有控件的数量与名字都要相同

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

public class CommonUtil { public static void configLanguage(Context mContext, String language) { 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 if(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(config, null); }}

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

override fun onClick(v: View) { when(v.id){ R.id.tvChinese->{ CommonUtil.configLanguage(this,"CHINESE") startActivity<LanguageActivity>() } R.id.tvEnglish->{ CommonUtil.configLanguage(this,"ENGLISH") startActivity<LanguageActivity>() } R.id.tvJan->{ CommonUtil.configLanguage(this,"JAPANESE") startActivity<LanguageActivity>() } } }

第五步:activity_language代码

<?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" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/text1" android:padding="10dp" android:textSize="15sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/text2" android:padding="10dp" android:textSize="15sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/text3" android:padding="10dp" android:textSize="15sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/text4" android:padding="10dp" android:textSize="15sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/text5" android:padding="10dp" android:textSize="15sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/text6" android:padding="10dp" android:textSize="15sp" /></LinearLayout>

下面可以看一下整个的目录结构

运行截图:

#####代码地址

总结

以上所述是小编给大家介绍的android 使用kotlin 实现点击更换全局语言,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

本文标题: android 使用kotlin 实现点击更换全局语言(中日英切换)

以上就上有关android使用kotlin实现点击更换全局语言(中日英切换)的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.