温馨提示×

温馨提示×

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

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

android使用kotlin怎么实现点击更换全局语言

发布时间:2021-08-06 10:59:42 来源:亿速云 阅读:114 作者:chen 栏目:编程语言

本篇内容主要讲解“android使用kotlin怎么实现点击更换全局语言”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“android使用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怎么实现点击更换全局语言”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

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

AI