亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android 使用kotlin 實現(xiàn)點擊更換全局語言(中日英切換)

 更新時間:2019年11月13日 14:14:41   作者:吃餃子蘸醋嗎  
這篇文章主要介紹了android kotlin 點擊更換全局語言的實現(xiàn)方法,這里主要介紹中日英切換,需要的朋友可以參考下

> 因為我的工作要用kotlin所以今天在這里給大家總結(jié)一下關(guān)于全局語言切換的kotlin語言實現(xiàn)實現(xiàn),很簡單,希望在這里可以幫助到有需要的同學(xué),下面簡單說一下實現(xiàn)步驟,會把運行截圖放在最后<

注:在這里我要說一下,我知道kotlin不太普及,如果有的同學(xué)需要java版的,可以在通讀一遍代碼,了解了之后把kotlin轉(zhuǎn)化為java,因為kotlin與java是互通的,代碼的一些關(guān)鍵點,java語言該怎么寫還怎么寫,如果有不明白的可以留言

第一步:簡單寫一下選擇語言的布局就好,會用到點擊事件,因為我要用到三種語言,可以Button控件,TextView控件,都可以

第二步:可以看下面截圖

1.右鍵res



2.new–>android resource file


在這里插入圖片描述

3.輸入filename,在下滿local選擇需要的語言


在這里插入圖片描述

4.最后像這樣,然后在里面輸入所需要控件的語言,在xml空間中運用到,比如 android:text=“@strings/定義的名字”,注意這4個string里面所有控件的數(shù)量與名字都要相同


在這里插入圖片描述

第二步:這里要用到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);
 }
}

第四步.然后在主頁面進(jìn)行跳轉(zhuǎn)和調(diào)用,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>

下面可以看一下整個的目錄結(jié)構(gòu)


在這里插入圖片描述

運行截圖:


在這里插入圖片描述在這里插入圖片描述在這里插入圖片描述
在這里插入圖片描述

#####代碼地址

總結(jié)

以上所述是小編給大家介紹的android 使用kotlin 實現(xiàn)點擊更換全局語言,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • Android短信驗證碼自動填寫實現(xiàn)代碼

    Android短信驗證碼自動填寫實現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Android短信驗證碼自動填寫實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 最新評論