溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

android使用kotlin怎么實(shí)現(xiàn)點(diǎn)擊更換全局語言

發(fā)布時(shí)間:2021-08-06 10:59:42 來源:億速云 閱讀:127 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“android使用kotlin怎么實(shí)現(xiàn)點(diǎn)擊更換全局語言”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“android使用kotlin怎么實(shí)現(xiàn)點(diǎ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>

到此,相信大家對(duì)“android使用kotlin怎么實(shí)現(xiàn)點(diǎn)擊更換全局語言”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI