android 使用kotlin 實(shí)現(xiàn)點(diǎn)擊更換全局語言(中日英切換)
> 因?yàn)槲业墓ぷ饕胟otlin所以今天在這里給大家總結(jié)一下關(guān)于全局語言切換的kotlin語言實(shí)現(xiàn)實(shí)現(xiàn),很簡(jiǎn)單,希望在這里可以幫助到有需要的同學(xué),下面簡(jiǎn)單說一下實(shí)現(xiàn)步驟,會(huì)把運(yùn)行截圖放在最后<
注:在這里我要說一下,我知道kotlin不太普及,如果有的同學(xué)需要java版的,可以在通讀一遍代碼,了解了之后把kotlin轉(zhuǎn)化為java,因?yàn)閗otlin與java是互通的,代碼的一些關(guān)鍵點(diǎn),java語言該怎么寫還怎么寫,如果有不明白的可以留言
第一步:簡(jiǎn)單寫一下選擇語言的布局就好,會(huì)用到點(diǎn)擊事件,因?yàn)槲乙玫饺N語言,可以Button控件,TextView控件,都可以
第二步:可以看下面截圖
1.右鍵res
2.new–>android resource file
3.輸入filename,在下滿local選擇需要的語言
4.最后像這樣,然后在里面輸入所需要控件的語言,在xml空間中運(yùn)用到,比如 android:text=“@strings/定義的名字”,注意這4個(gè)string里面所有控件的數(shù)量與名字都要相同
第二步:這里要用到CommonUtil工具類,因?yàn)閗otlin與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就是需要改變控件語言的界面,下面會(huì)有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>
下面可以看一下整個(gè)的目錄結(jié)構(gòu)
運(yùn)行截圖:
總結(jié)
以上所述是小編給大家介紹的android 使用kotlin 實(shí)現(xiàn)點(diǎn)擊更換全局語言,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Android實(shí)現(xiàn)app應(yīng)用多語言切換功能
- Android app應(yīng)用多語言切換功能實(shí)現(xiàn)
- android動(dòng)態(tài)設(shè)置app當(dāng)前運(yùn)行語言的方法
- 詳解Android更改APP語言模式的實(shí)現(xiàn)過程
- Android 7.0以上版本實(shí)現(xiàn)應(yīng)用內(nèi)語言切換的方法
- Android 系統(tǒng)語言切換監(jiān)聽和設(shè)置實(shí)例代碼
- Android實(shí)現(xiàn)應(yīng)用內(nèi)置語言切換功能
- Android實(shí)現(xiàn)系統(tǒng)語言切換功能
- Android App中進(jìn)行語言的切換
相關(guān)文章
Kotlin中常見內(nèi)聯(lián)擴(kuò)展函數(shù)的使用方法教程
在Kotlin中,使用inline修飾符標(biāo)記內(nèi)聯(lián)函數(shù),既會(huì)影響到函數(shù)本身, 也影響到傳遞給它的Lambda表達(dá)式,這兩者都會(huì)被內(nèi)聯(lián)到調(diào)用處。下面這篇文章主要給大家介紹了關(guān)于Kotlin中常見內(nèi)聯(lián)擴(kuò)展函數(shù)的使用方法,需要的朋友可以參考下。2017-12-12Android 中兩個(gè)Activity 之間的傳值問題
這篇文章主要介紹了Android 中兩個(gè)Activity 之間的傳值問題的相關(guān)資料,需要的朋友可以參考下2017-08-08Android實(shí)現(xiàn)動(dòng)態(tài)定值范圍效果的控件
這篇文中給大家分享一個(gè)Android的控件,這個(gè)控件實(shí)現(xiàn)是一個(gè)可以動(dòng)態(tài)選擇定值范圍的效果,實(shí)現(xiàn)后的效果很不錯(cuò),對(duì)大家日常開發(fā)或許有所幫助,感興趣的朋友們可以一起來看看。2016-09-09Android studio有關(guān)側(cè)滑的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android studio有關(guān)側(cè)滑的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Android流式布局實(shí)現(xiàn)歷史搜索記錄功能
最近在開發(fā)項(xiàng)目的時(shí)候,有一個(gè)需求是展示歷史搜索記錄 ,展示的樣式是流式布局(就是根據(jù)內(nèi)容自動(dòng)換行)。接下來通過本文給大家分享android流式布局實(shí)現(xiàn)歷史搜索記錄功能,感興趣的的小伙伴參考下2017-05-05Android自定義視圖實(shí)現(xiàn)手指移動(dòng)軌跡
這篇文章主要為大家詳細(xì)介紹了Android自定義視圖實(shí)現(xiàn)手指移動(dòng)軌跡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06學(xué)習(xí)使用Android Chronometer計(jì)時(shí)器
Chronometer是一個(gè)簡(jiǎn)單的計(jì)時(shí)器,你可以給它一個(gè)開始時(shí)間,并以計(jì)時(shí),或者如果你不給它一個(gè)開始時(shí)間,它將會(huì)使用你的時(shí)間通話開始,這篇文章主要幫助大家學(xué)習(xí)掌握使用Android Chronometer計(jì)時(shí)器,感興趣的小伙伴們可以參考一下2016-04-04

Android短信驗(yàn)證碼自動(dòng)填寫實(shí)現(xiàn)代碼