android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言

這篇文章給大家分享的是有關(guān)android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供新平網(wǎng)站建設(shè)、新平做網(wǎng)站、新平網(wǎng)站設(shè)計(jì)、新平網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、新平企業(yè)網(wǎng)站模板建站服務(wù),10多年新平做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

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

第一步:簡(jiǎn)單寫一下選擇語(yǔ)言的布局就好,會(huì)用到點(diǎn)擊事件,因?yàn)槲乙玫饺N語(yǔ)言,可以Button控件,TextView控件,都可以

第二步:可以看下面截圖

1.右鍵res


android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言

2.new–>android resource file


android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言

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


android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言

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


android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言

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

第四步.然后在主頁(yè)面進(jìn)行跳轉(zhuǎn)和調(diào)用,LanguageActivity就是需要改變控件語(yǔ)言的界面,下面會(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)


android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言

運(yùn)行截圖:


android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言
android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

感謝各位的閱讀!關(guān)于“android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

文章標(biāo)題:android中如何使用kotlin實(shí)現(xiàn)點(diǎn)擊更換全局語(yǔ)言
分享地址:http://bm7419.com/article42/jciphc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站制作定制網(wǎng)站、外貿(mào)建站域名注冊(cè)、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作