詳解Android更改APP語言模式的實現過程_Android

來源:互聯網
上載者:User

一、效果圖

二、描述

更改Android項目中的語言,這個作用於只用於此APP,不會作用於整個系統

三、解決方案

(一)布局檔案

<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);  }  /**   * 點擊按鈕,更換語言   *    * @param view   */  public void changeLanguage(View view) {    Resources resources = getResources();    Configuration configuration = resources.getConfiguration(); // 擷取資源配置    if (configuration.locale.equals(Locale.CHINA)) { // 判斷當前語言是否是中文      configuration.locale = Locale.ENGLISH; // 設定當前語言配置為英文    } else {      configuration.locale = Locale.CHINA; // 設定當前語言配置為中文    }    DisplayMetrics metrics = new DisplayMetrics();    resources.updateConfiguration(configuration, metrics); // 更新設定檔    sendBroadcast(new Intent("language")); // 發送廣播,廣播接受後重新開啟此Activtiy以重新初始化介面語言.//    Intent intent = new Intent(MainActivity.this, MainActivity.class); //或者可以直接跳轉MainActivity//    intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); //去除掉跳轉的動畫,讓使用者看起來好像沒有跳轉的感覺//    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下建立Values-en檔案夾,複製String.xml,並且把裡面的中文改成英文,實現國際化.

values/strings.xml

<resources>  <string name="app_name">語言切換</string>  <string name="hello_world">你好,World!</string>  <string name="action_settings">設定</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>

總結

以上就是詳解Android更改APP語言模式的實現過程的全部內容,希望對大家開發Android有所協助,如果有疑問歡迎留言討論。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.