Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)切換內(nèi)置語(yǔ)言功能

這篇文章給大家介紹Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)切換內(nèi)置語(yǔ)言功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

在芒康等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需設(shè)計(jì)網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),成都全網(wǎng)營(yíng)銷推廣,成都外貿(mào)網(wǎng)站建設(shè),芒康網(wǎng)站建設(shè)費(fèi)用合理。

一、需求

有時(shí)候應(yīng)用需要在內(nèi)部切換語(yǔ)言但又不影響系統(tǒng)的語(yǔ)言,比如是應(yīng)用現(xiàn)在是中文的,系統(tǒng)語(yǔ)言也是中文的,我把應(yīng)用的切換成英文顯示后系統(tǒng)語(yǔ)言還是中文的,系統(tǒng)語(yǔ)言切換后也不會(huì)被改變,還有就是有些機(jī)的系統(tǒng)是被改造精簡(jiǎn)過的,比如有些國(guó)產(chǎn)機(jī)的系統(tǒng)的語(yǔ)言就被精簡(jiǎn)剩中文和英文。支付寶、微信、Top Story都有在應(yīng)用內(nèi)部設(shè)置語(yǔ)言切換這樣的功能。

Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)切換內(nèi)置語(yǔ)言功能

三、實(shí)現(xiàn)

(一)添加多種語(yǔ)言的資源文件夾及文件

我這默認(rèn)是英語(yǔ)再添加了個(gè)俄文(Google翻譯的)和中文。

Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)切換內(nèi)置語(yǔ)言功能

values/strings.xml

<resources> 
  <string name="app_name">SwitchLanguage</string> 
  <string name="helloworld">Hello World!</string> 
  <string name="language">Eng</string> 
  <string name="english">English</string> 
  <string name="chinese">中文</string> 
  <string name="russian">русский</string> 
  <string name="secondact">Second Activity</string> 
</resources> 

values-ru/strings.xml

<resources> 
  <string name="app_name">Переключение язык</string> 
  <string name="helloworld">привет мир!</string> 
  <string name="language">русский</string> 
  <string name="secondact">второй активность</string> 
</resources> 

values-zh/strings.xml

<resources> 
  <string name="app_name">切換語(yǔ)言</string> 
  <string name="helloworld">你好 世界!</string> 
  <string name="language">中文</string> 
  <string name="secondact">第二屏</string> 
</resources> 

(二)布局文件
activity_main.xml

默認(rèn)標(biāo)題欄的文字是切換語(yǔ)言后是不會(huì)被改變的,使用Toobar替換掉就可以了。

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
<LinearLayout 
  android:id="@+id/activity_main" 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" 
  tools:context="com.ce.switchlanguage.MainActivity" 
  xmlns:app="http://schemas.android.com/apk/res-auto"> 
  <android.support.design.widget.AppBarLayout 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:theme="@style/AppTheme.AppBarOverlay"> 
 
    <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="&#63;attr/actionBarSize" 
      android:background="&#63;attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" 
      app:title="@string/app_name"/> 
 
  </android.support.design.widget.AppBarLayout> 
  <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/helloworld"/> 
  <Button 
    android:id="@+id/change" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="@string/language"/> 
</LinearLayout> 

styles.xml

設(shè)置語(yǔ)言后需要重啟下activity,啟動(dòng)會(huì)有個(gè)效果,使用windowDisablePreview屏蔽掉它。

<resources> 
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
    <item name="android:windowDisablePreview">true</item> 
  </style> 
  <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/> 
  <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark"/> 
</resources> 

main_menu.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
   xmlns:app="http://schemas.android.com/apk/res-auto"> 
  <item android:id="@+id/chinese" android:title="@string/chinese" app:showAsAction="never" /> 
  <item android:id="@+id/english" android:title="@string/english" app:showAsAction="never" /> 
  <item android:id="@+id/russian" android:title="@string/russian" app:showAsAction="never" /> 
</menu> 

(三)LocaleUtils

package com.ce.switchlanguage; 
 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.content.res.Configuration; 
import android.os.Build; 
import android.util.DisplayMetrics; 
 
import com.google.gson.Gson; 
 
import java.util.Locale; 
 
public class LocaleUtils { 
  /** 
   * 中文 
   */ 
  public static final Locale LOCALE_CHINESE = Locale.CHINESE; 
  /** 
   * 英文 
   */ 
  public static final Locale LOCALE_ENGLISH = Locale.ENGLISH; 
  /** 
   * 俄文 
   */ 
  public static final Locale LOCALE_RUSSIAN = new Locale("ru"); 
  /** 
   * 保存SharedPreferences的文件名 
   */ 
  private static final String LOCALE_FILE = "LOCALE_FILE"; 
  /** 
   * 保存Locale的key 
   */ 
  private static final String LOCALE_KEY = "LOCALE_KEY"; 
 
  /** 
   * 獲取用戶設(shè)置的Locale 
   * @param pContext Context 
   * @return Locale 
   */ 
  public static Locale getUserLocale(Context pContext) { 
    SharedPreferences _SpLocale = pContext.getSharedPreferences(LOCALE_FILE, Context.MODE_PRIVATE); 
    String _LocaleJson = _SpLocale.getString(LOCALE_KEY, ""); 
    return jsonToLocale(_LocaleJson); 
  } 
  /** 
   * 獲取當(dāng)前的Locale 
   * @param pContext Context 
   * @return Locale 
   */ 
  public static Locale getCurrentLocale(Context pContext) { 
    Locale _Locale; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多語(yǔ)言設(shè)置獲取頂部的語(yǔ)言 
      _Locale = pContext.getResources().getConfiguration().getLocales().get(0); 
    } else { 
      _Locale = pContext.getResources().getConfiguration().locale; 
    } 
    return _Locale; 
  } 
  /** 
   * 保存用戶設(shè)置的Locale 
   * @param pContext Context 
   * @param pUserLocale Locale 
   */ 
  public static void saveUserLocale(Context pContext, Locale pUserLocale) { 
    SharedPreferences _SpLocal=pContext.getSharedPreferences(LOCALE_FILE, Context.MODE_PRIVATE); 
    SharedPreferences.Editor _Edit=_SpLocal.edit(); 
    String _LocaleJson = localeToJson(pUserLocale); 
    _Edit.putString(LOCALE_KEY, _LocaleJson); 
    _Edit.apply(); 
  } 
  /** 
   * Locale轉(zhuǎn)成json 
   * @param pUserLocale UserLocale 
   * @return json String 
   */ 
  private static String localeToJson(Locale pUserLocale) { 
    Gson _Gson = new Gson(); 
    return _Gson.toJson(pUserLocale); 
  } 
  /** 
   * json轉(zhuǎn)成Locale 
   * @param pLocaleJson LocaleJson 
   * @return Locale 
   */ 
  private static Locale jsonToLocale(String pLocaleJson) { 
    Gson _Gson = new Gson(); 
    return _Gson.fromJson(pLocaleJson, Locale.class); 
  } 
  /** 
   * 更新Locale 
   * @param pContext Context 
   * @param pNewUserLocale New User Locale 
   */ 
  public static void updateLocale(Context pContext, Locale pNewUserLocale) { 
    if (needUpdateLocale(pContext, pNewUserLocale)) { 
      Configuration _Configuration = pContext.getResources().getConfiguration(); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
        _Configuration.setLocale(pNewUserLocale); 
      } else { 
        _Configuration.locale =pNewUserLocale; 
      } 
      DisplayMetrics _DisplayMetrics = pContext.getResources().getDisplayMetrics(); 
      pContext.getResources().updateConfiguration(_Configuration, _DisplayMetrics); 
      saveUserLocale(pContext, pNewUserLocale); 
    } 
  } 
  /** 
   * 判斷需不需要更新 
   * @param pContext Context 
   * @param pNewUserLocale New User Locale 
   * @return true / false 
   */ 
  public static boolean needUpdateLocale(Context pContext, Locale pNewUserLocale) { 
    return pNewUserLocale != null && !getCurrentLocale(pContext).equals(pNewUserLocale); 
  } 
} 

Locale工具類,這里我用SharedPreferences來保存所設(shè)置的Locale,Locale是實(shí)現(xiàn)了Serializable的。

(四)Activity

package com.ce.switchlanguage; 
 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
 
public class MainActivity extends AppCompatActivity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar _Toolbar =(Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(_Toolbar); 
  } 
 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main_menu,menu); 
    return true; 
  } 
 
  @Override 
  public boolean onOptionsItemSelected(MenuItem item) { 
    int _ItemId=item.getItemId(); 
    switch (_ItemId) { 
      case R.id.chinese: 
        if (LocaleUtils.needUpdateLocale(this, LocaleUtils.LOCALE_CHINESE)) { 
          LocaleUtils.updateLocale(this, LocaleUtils.LOCALE_CHINESE); 
          restartAct(); 
        } 
        break; 
      case R.id.english: 
        if (LocaleUtils.needUpdateLocale(this, LocaleUtils.LOCALE_ENGLISH)) { 
          LocaleUtils.updateLocale(this, LocaleUtils.LOCALE_ENGLISH); 
          restartAct(); 
        } 
        break; 
      case R.id.russian: 
        if (LocaleUtils.needUpdateLocale(this, LocaleUtils.LOCALE_RUSSIAN)) { 
          LocaleUtils.updateLocale(this, LocaleUtils.LOCALE_RUSSIAN); 
          restartAct(); 
        } 
    } 
    return true; 
  } 
 
  /** 
   * 重啟當(dāng)前Activity 
   */ 
  private void restartAct() { 
    finish(); 
    Intent _Intent = new Intent(this, MainActivity.class); 
    startActivity(_Intent); 
    //清除Activity退出和進(jìn)入的動(dòng)畫 
    overridePendingTransition(0, 0); 
  } 
} 

這里只有一個(gè)Activity所以切換的時(shí)候重啟下當(dāng)前Activity就好了,棧里還有其他Activity的自己再處理吧。

(五)Application

package com.ce.switchlanguage; 
 
import android.app.Application; 
import android.content.res.Configuration; 
import android.os.Build; 
 
import java.util.Locale; 
 
public class MyApplication extends Application { 
  @Override 
  public void onCreate() { 
    super.onCreate(); 
    Locale _UserLocale=LocaleUtils.getUserLocale(this); 
    LocaleUtils.updateLocale(this, _UserLocale); 
  } 
 
  @Override 
  public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    Locale _UserLocale=LocaleUtils.getUserLocale(this); 
    //系統(tǒng)語(yǔ)言改變了應(yīng)用保持之前設(shè)置的語(yǔ)言 
    if (_UserLocale != null) { 
      Locale.setDefault(_UserLocale); 
      Configuration _Configuration = new Configuration(newConfig); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
        _Configuration.setLocale(_UserLocale); 
      } else { 
        _Configuration.locale =_UserLocale; 
      } 
      getResources().updateConfiguration(_Configuration, getResources().getDisplayMetrics()); 
    } 
  } 
} 

在Application onCreate的時(shí)候更新下,在系統(tǒng)語(yǔ)言改變的時(shí)候也要保持之前設(shè)置的語(yǔ)言不變。

關(guān)于Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)切換內(nèi)置語(yǔ)言功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

標(biāo)題名稱:Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)切換內(nèi)置語(yǔ)言功能
轉(zhuǎn)載注明:http://muchs.cn/article28/ijoocp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈網(wǎng)站改版、品牌網(wǎng)站制作、域名注冊(cè)網(wǎng)站維護(hù)、定制開發(fā)

廣告

聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)