android kotlin 點擊更換全局語言(中日英切換)
> 因爲我的工作要用kotlin所以今天在這裏給大家總結一下關於全局語言切換的kotlin語言實現實現,很簡單,希望在這裏可以幫助到有需要的同學,下面簡單說一下實現步驟,會把運行截圖放在最後<
注:在這裏我要說一下,我知道kotlin不太普及,如果有的同學需要java版的,可以在通讀一遍代碼,瞭解了之後把kotlin轉化爲java,因爲kotlin與java是互通的,代碼的一些關鍵點,java語言該怎麼寫還怎麼寫,如果有不明白的可以留言
第一步:簡單寫一下選擇語言的佈局就好,會用到點擊事件,因爲我要用到三種語言,可以Button控件,TextView控件,都可以
第二步:可以看下面截圖
1.右鍵res
2.new–>android resource file
3.輸入filename,在下滿local選擇需要的語言
4.最後像這樣,然後在裏面輸入所需要控件的語言,在xml空間中運用到,比如 android:text=“@strings/定義的名字”,注意這4個string裏面所有控件的數量與名字都要相同
第二步:這裏要用到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);
}
}
第四步.然後在主頁面進行跳轉和調用,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>
下面可以看一下整個的目錄結構
運行截圖:
#####代碼地址