├── AndroidManifest.xml
├── ic_launcher-web.png
├── libs
└── android-support-v4.jar
├── proguard-project.txt
├── project.properties
├── res
├── drawable-hdpi
│ └── ic_launcher.png
├── drawable-mdpi
│ └── ic_launcher.png
├── drawable-xhdpi
│ └── ic_launcher.png
├── drawable-xxhdpi
│ └── ic_launcher.png
├── layout
│ ├── activity_main.xml
│ ├── activity_second.xml
│ └── dialog_select_lanuage.xml
├── values-en
│ └── strings.xml
├── values-zh
│ └── strings.xml
└── values
│ ├── dimens.xml
│ └── styles.xml
└── src
└── com
└── jit
└── language
├── BaseActivity.java
├── MainActivity.java
├── PreferenceUtil.java
└── SecondActivity.java
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JackCho/SwitchLanguageDemo/7e6ae9291088310877e5a56b3c75bff55b2913af/ic_launcher-web.png
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JackCho/SwitchLanguageDemo/7e6ae9291088310877e5a56b3c75bff55b2913af/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JackCho/SwitchLanguageDemo/7e6ae9291088310877e5a56b3c75bff55b2913af/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JackCho/SwitchLanguageDemo/7e6ae9291088310877e5a56b3c75bff55b2913af/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JackCho/SwitchLanguageDemo/7e6ae9291088310877e5a56b3c75bff55b2913af/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JackCho/SwitchLanguageDemo/7e6ae9291088310877e5a56b3c75bff55b2913af/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
17 |
18 |
26 |
27 |
35 |
36 |
--------------------------------------------------------------------------------
/res/layout/activity_second.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
16 |
17 |
--------------------------------------------------------------------------------
/res/layout/dialog_select_lanuage.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
15 |
16 |
22 |
23 |
33 |
34 |
40 |
41 |
51 |
52 |
--------------------------------------------------------------------------------
/res/values-en/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | SwitchLanguageDemo
5 | Hello buddy!Welcome to Android\'s skill share
6 | Switch Language
7 | English
8 | Chinese
9 | Jump to Next Activity
10 |
11 |
--------------------------------------------------------------------------------
/res/values-zh/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 切换语言demo
5 | 你好,少年!欢迎收听android干货分享
6 | 切换语言
7 | 英语
8 | 中文
9 | 跳转到另一个页面
10 |
11 |
--------------------------------------------------------------------------------
/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/src/com/jit/language/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package com.jit.language;
2 |
3 | import java.util.Locale;
4 | import android.app.Activity;
5 | import android.content.res.Configuration;
6 | import android.content.res.Resources;
7 | import android.os.Bundle;
8 | import android.util.DisplayMetrics;
9 |
10 | public class BaseActivity extends Activity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | //初始化PreferenceUtil
16 | PreferenceUtil.init(this);
17 | //根据上次的语言设置,重新设置语言
18 | switchLanguage(PreferenceUtil.getString("language", "zh"));
19 | }
20 |
21 |
22 | protected void switchLanguage(String language) {
23 | //设置应用语言类型
24 | Resources resources = getResources();
25 | Configuration config = resources.getConfiguration();
26 | DisplayMetrics dm = resources.getDisplayMetrics();
27 | if (language.equals("en")) {
28 | config.locale = Locale.ENGLISH;
29 | } else {
30 | config.locale = Locale.SIMPLIFIED_CHINESE;
31 | }
32 | resources.updateConfiguration(config, dm);
33 |
34 | //保存设置语言的类型
35 | PreferenceUtil.commitString("language", language);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/com/jit/language/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.jit.language;
2 |
3 | import android.app.Dialog;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.View.OnClickListener;
9 | import android.widget.Button;
10 | import android.widget.TextView;
11 |
12 | public class MainActivity extends BaseActivity implements OnClickListener{
13 |
14 | private Dialog mDialog;
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_main);
20 |
21 | TextView textView = (TextView) findViewById(R.id.text);
22 | Button button = (Button) findViewById(R.id.btn);
23 | Button button2 = (Button) findViewById(R.id.btn_2);
24 | textView.setText(R.string.hello_world);
25 | button.setText(R.string.switch_language);
26 |
27 | button.setOnClickListener(new OnClickListener() {
28 |
29 | @Override
30 | public void onClick(View v) {
31 | if (mDialog == null) {
32 | LayoutInflater inflater = getLayoutInflater();
33 | View layout = inflater.inflate(R.layout.dialog_select_lanuage,null);
34 | TextView english = (TextView) layout.findViewById(R.id.select_english);
35 | TextView chinese = (TextView) layout.findViewById(R.id.select_chinese);
36 | mDialog = new Dialog(MainActivity.this, R.style.Custom_Dialog_Theme);
37 | mDialog.setCanceledOnTouchOutside(false);
38 | english.setOnClickListener(MainActivity.this);
39 | chinese.setOnClickListener(MainActivity.this);
40 | mDialog.setContentView(layout);
41 | }
42 | mDialog.show();
43 | }
44 | });
45 |
46 | button2.setOnClickListener(new OnClickListener() {
47 |
48 | @Override
49 | public void onClick(View v) {
50 | Intent it = new Intent(MainActivity.this, SecondActivity.class);
51 | startActivity(it);
52 | }
53 | });
54 | }
55 |
56 | @Override
57 | public void onClick(View v) {
58 | mDialog.dismiss();
59 | switch (v.getId()) {
60 | case R.id.select_english:
61 | switchLanguage("en");
62 | break;
63 | case R.id.select_chinese:
64 | switchLanguage("zh");
65 | break;
66 |
67 | default:
68 | break;
69 | }
70 | //更新语言后,destroy当前页面,重新绘制
71 | finish();
72 | Intent it = new Intent(MainActivity.this, MainActivity.class);
73 | startActivity(it);
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/src/com/jit/language/PreferenceUtil.java:
--------------------------------------------------------------------------------
1 | package com.jit.language;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 | import android.content.SharedPreferences.Editor;
6 |
7 | /**
8 | * Description:SharedPreferences的管理类
9 | *
10 | */
11 | public class PreferenceUtil {
12 |
13 | private static SharedPreferences mSharedPreferences = null;
14 | private static Editor mEditor = null;
15 |
16 | public static void init(Context context){
17 | if (null == mSharedPreferences) {
18 | mSharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context) ;
19 | }
20 | }
21 |
22 | public static void removeKey(String key){
23 | mEditor = mSharedPreferences.edit();
24 | mEditor.remove(key);
25 | mEditor.commit();
26 | }
27 |
28 | public static void removeAll(){
29 | mEditor = mSharedPreferences.edit();
30 | mEditor.clear();
31 | mEditor.commit();
32 | }
33 |
34 | public static void commitString(String key, String value){
35 | mEditor = mSharedPreferences.edit();
36 | mEditor.putString(key, value);
37 | mEditor.commit();
38 | }
39 |
40 | public static String getString(String key, String faillValue){
41 | return mSharedPreferences.getString(key, faillValue);
42 | }
43 |
44 | public static void commitInt(String key, int value){
45 | mEditor = mSharedPreferences.edit();
46 | mEditor.putInt(key, value);
47 | mEditor.commit();
48 | }
49 |
50 | public static int getInt(String key, int failValue){
51 | return mSharedPreferences.getInt(key, failValue);
52 | }
53 |
54 | public static void commitLong(String key, long value){
55 | mEditor = mSharedPreferences.edit();
56 | mEditor.putLong(key, value);
57 | mEditor.commit();
58 | }
59 |
60 | public static long getLong(String key, long failValue) {
61 | return mSharedPreferences.getLong(key, failValue);
62 | }
63 |
64 | public static void commitBoolean(String key, boolean value){
65 | mEditor = mSharedPreferences.edit();
66 | mEditor.putBoolean(key, value);
67 | mEditor.commit();
68 | }
69 |
70 | public static Boolean getBoolean(String key, boolean failValue){
71 | return mSharedPreferences.getBoolean(key, failValue);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/com/jit/language/SecondActivity.java:
--------------------------------------------------------------------------------
1 | package com.jit.language;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 |
6 | public class SecondActivity extends Activity {
7 |
8 | @Override
9 | protected void onCreate(Bundle savedInstanceState) {
10 | super.onCreate(savedInstanceState);
11 | setContentView(R.layout.activity_second);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------