最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、效果圖

二、描述

更改Android項(xiàng)目中的語(yǔ)言,這個(gè)作用于只用于此APP,不會(huì)作用于整個(gè)系統(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="語(yǔ)言切換" />

</LinearLayout>

(二)MainActivity主頁(yè)面

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

(三)ChangeReceiver廣播類(lèi)

package com.example.chinesepage;

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

/**
 * 自定義廣播類(lèi) 語(yǔ)言改變后重啟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)國(guó)際化.

values/strings.xml

<resources>

  <string name="app_name">語(yǔ)言切換</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>

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

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

總結(jié)

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

相關(guān)文章

最新評(píng)論

清水河县| 佛山市| 彭山县| 辽宁省| 淄博市| 衡东县| 亳州市| 辽中县| 高雄县| 华池县| 乐都县| 南召县| 乌拉特前旗| 通海县| 来宾市| 高淳县| 合阳县| 云浮市| 奈曼旗| 永德县| 蕲春县| 林甸县| 滕州市| 志丹县| 嘉善县| 绿春县| 永和县| 兴文县| 长白| 北流市| 元氏县| 资阳市| 五常市| 永登县| 张家港市| 乌苏市| 东宁县| 将乐县| 墨玉县| 洪洞县| 泉州市|