当前位置: 移动技术网 > IT编程>移动开发>Android > android 使用kotlin 实现点击更换全局语言(中日英切换)

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

2020年03月09日  | 移动技术网IT编程  | 我要评论

低俗喜剧qvod,雪纳瑞,cisco售后

> 因为我的工作要用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 实现点击更换全局语言,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网