【移動應用開發(fā)技術】android中如何使用kotlin實現點擊更換全局語言_第1頁
【移動應用開發(fā)技術】android中如何使用kotlin實現點擊更換全局語言_第2頁
【移動應用開發(fā)技術】android中如何使用kotlin實現點擊更換全局語言_第3頁
【移動應用開發(fā)技術】android中如何使用kotlin實現點擊更換全局語言_第4頁
【移動應用開發(fā)技術】android中如何使用kotlin實現點擊更換全局語言_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

【移動應用開發(fā)技術】android中如何使用kotlin實現點擊更換全局語言

這篇文章給大家分享的是有關android中如何使用kotlin實現點擊更換全局語言的內容。在下覺得挺實用的,因此分享給大家做個參考,一起跟隨在下過來看看吧。注:在這里我要說一下,我知道kotlin不太普及,如果有的同學需要java版的,可以在通讀一遍代碼,了解了之后把kotlin轉化為java,因為kotlin與java是互通的,代碼的一些關鍵點,java語言該怎么寫還怎么寫,如果有不明白的可以留言第一步:簡單寫一下選擇語言的布局就好,會用到點擊事件,因為我要用到三種語言,可以Button控件,TextView控件,都可以第二步:可以看下面截圖1.右鍵res2.new–>androidresourcefile3.輸入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="/apk/res/android"

xmlns:app="/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是一種基于Linux內核的自由及開

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論