欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Android更改APP語言模式的實(shí)現(xiàn)過程

 更新時間:2016年08月11日 17:45:11   投稿:daisy  
本文詳細(xì)介紹如何更改Android中APP的語言模式,這個功能對于大家開發(fā)Android APP很有幫助,本文運(yùn)用文字介紹和代碼示例把過程寫的很詳細(xì),有需要的可以參考借鑒。

一、效果圖

二、描述

更改Android項(xiàng)目中的語言,這個作用于只用于此APP,不會作用于整個系統(tǒng)

三、解決方案

(一)布局文件

<LinearLayout 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"
  android:padding="20dp" >

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hellow" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="changeLanguage" 
    android:text="語言切換" />

</LinearLayout>

(二)MainActivity主頁面

package com.example.chinesepage;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  /**
   * 點(diǎn)擊按鈕,更換語言
   * 
   * @param view
   */
  public void changeLanguage(View view) {
    Resources resources = getResources();
    Configuration configuration = resources.getConfiguration(); // 獲取資源配置
    if (configuration.locale.equals(Locale.CHINA)) { // 判斷當(dāng)前語言是否是中文
      configuration.locale = Locale.ENGLISH; // 設(shè)置當(dāng)前語言配置為英文
    } else {
      configuration.locale = Locale.CHINA; // 設(shè)置當(dāng)前語言配置為中文
    }
    DisplayMetrics metrics = new DisplayMetrics();
    resources.updateConfiguration(configuration, metrics); // 更新配置文件
    sendBroadcast(new Intent("language")); // 發(fā)送廣播,廣播接受后重新開啟此Activtiy以重新初始化界面語言.
//    Intent intent = new Intent(MainActivity.this, MainActivity.class); //或者可以直接跳轉(zhuǎn)MainActivity
//    intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); //去除掉跳轉(zhuǎn)的動畫,讓用戶看起來好像沒有跳轉(zhuǎn)的感覺
//    startActivity(intent);
    finish();
  }
}

(三)ChangeReceiver廣播類

package com.example.chinesepage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * 自定義廣播類 語言改變后重啟Activity
 * 
 * @author asus
 * 
 */
public class ChangeReceiver extends BroadcastReceiver {
  private Intent mIntent;

  @Override
  public void onReceive(Context context, Intent intent) {
    mIntent = new Intent(context, MainActivity.class);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(mIntent);
  }
}

(四)在Res下創(chuàng)建Values-en文件夾,復(fù)制String.xml,并且把里面的中文改成英文,實(shí)現(xiàn)國際化.

values/strings.xml

<resources>

  <string name="app_name">語言切換</string>
  <string name="hello_world">你好,World!</string>
  <string name="action_settings">設(shè)置</string>
  <string name="hellow">你好</string>

</resources>

values-en/strings.xml

<resources>

  <string name="app_name">ChinesePage</string>
  <string name="hello_world">Hello world!</string>
  <string name="action_settings">Settings</string>
  <string name="hellow">Hellow</string>

</resources>

(五)注冊廣播(這個別忘了~)

 <receiver android:name="com.example.chinesepage.ChangeReceiver" >
      <intent-filter>
        <action android:name="language" />
      </intent-filter>
    </receiver>

總結(jié)

以上就是詳解Android更改APP語言模式的實(shí)現(xiàn)過程的全部內(nèi)容,希望對大家開發(fā)Android有所幫助,如果有疑問歡迎留言討論。

相關(guān)文章

  • Android Activity Results API代替onActivityResult處理頁面數(shù)據(jù)

    Android Activity Results API代替onActivityResul

    說到onActivityResult,我們已經(jīng)非常熟悉來,通過在A activity啟動B activity并且傳入數(shù)據(jù)到B中,然后在A中通過onActivityResult來接收B中返回的數(shù)據(jù)。在最新的activity-ktx的beta版本中,谷歌已經(jīng)廢棄了onActivityResult
    2022-09-09
  • Android 圖片保存到相冊不顯示的解決方案(兼容Android 10及更高版本)

    Android 圖片保存到相冊不顯示的解決方案(兼容Android 10及更高版本)

    這篇文章主要介紹了Android 圖片保存到系統(tǒng)相冊不顯示的解決方案,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下
    2021-04-04
  • Android中HttpURLConnection類使用介紹

    Android中HttpURLConnection類使用介紹

    早些時候其實(shí)我們都習(xí)慣性使用HttpClient,但是后來Android6.0之后不再支持HttpClient,需要添加Apache的jar才行,所以,就有很多開發(fā)者放棄使用HttpClient了,HttpURLConnection畢竟是標(biāo)準(zhǔn)Java接口(java.net) ,適配性還是很強(qiáng)的
    2022-12-12
  • Android?WebView開發(fā)之自定義WebView工具框

    Android?WebView開發(fā)之自定義WebView工具框

    在WebView頁面長按時會彈出一個復(fù)制框,有的時候里面的item不是我們想要,這個時候我們就可以自定義一個工具框。本文就將介紹如何通過WebView自定義工具框,需要的朋友可以參考一下
    2021-12-12
  • Android 滑動小圓點(diǎn)ViewPager的兩種設(shè)置方法詳解流程

    Android 滑動小圓點(diǎn)ViewPager的兩種設(shè)置方法詳解流程

    Viewpager,視圖翻頁工具,提供了多頁面切換的效果。Android 3.0后引入的一個UI控件,位于v4包中。低版本使用需要導(dǎo)入v4包,現(xiàn)在我們一般不再兼容3.0及以下版本,另外使用Android studio開發(fā),默認(rèn)導(dǎo)入v7包,v7包含了v4,所以不用導(dǎo)包,越來越方便了
    2021-11-11
  • Android雙擊返回鍵退出程序的實(shí)現(xiàn)方法

    Android雙擊返回鍵退出程序的實(shí)現(xiàn)方法

    這篇文章主要介紹了Android雙擊返回鍵退出程序的實(shí)現(xiàn)方法,是Android程序開發(fā)中非常具有實(shí)用價值的重要技巧,需要的朋友可以參考下
    2014-09-09
  • Android圖片壓縮上傳之基礎(chǔ)篇

    Android圖片壓縮上傳之基礎(chǔ)篇

    這篇文章主要介紹了Android圖片壓縮上傳之基礎(chǔ)篇的相關(guān)內(nèi)容,本文介紹的非常詳解,具有參考借鑒價值,感興趣的朋友一起看下吧
    2016-05-05
  • Kotlin中實(shí)體類的創(chuàng)建方式

    Kotlin中實(shí)體類的創(chuàng)建方式

    這篇文章主要介紹了Kotlin中實(shí)體類的創(chuàng)建方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android仿微信拍攝短視頻

    Android仿微信拍攝短視頻

    本文主要對Android仿微信拍攝短視頻的實(shí)現(xiàn)方法進(jìn)行介紹,其功能設(shè)置為類似于微信,點(diǎn)擊開始拍攝,設(shè)置最長拍攝時間。具有很好的參考價值,需要的朋友一起來看下吧
    2016-12-12
  • Android中關(guān)于JSON相關(guān)應(yīng)用分析

    Android中關(guān)于JSON相關(guān)應(yīng)用分析

    這篇文章主要介紹了Android中關(guān)于JSON相關(guān)應(yīng)用,較為詳細(xì)的分析了Android中關(guān)于json相關(guān)類與使用方法,需要的朋友可以參考下
    2016-06-06

最新評論