本篇內(nèi)容主要講解“android使用kotlin怎么實現(xiàn)點擊更換全局語言”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“android使用kotlin怎么實現(xiàn)點擊更換全局語言”吧!
注:在這里我要說一下,我知道kotlin不太普及,如果有的同學需要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); }}
第四步.然后在主頁面進行跳轉(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>
到此,相信大家對“android使用kotlin怎么實現(xiàn)點擊更換全局語言”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!
新聞名稱:android使用kotlin怎么實現(xiàn)點擊更換全局語言-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://aaarwkj.com/article14/dsjjde.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站建設、網(wǎng)頁設計公司、關(guān)鍵詞優(yōu)化、App設計、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)