• android APP国际化一键切换实现


    首先看目录:

    上代码:

    package com.loaderman.language;
    
    import android.content.res.Configuration;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.util.DisplayMetrics;
    
    import org.greenrobot.eventbus.EventBus;
    import org.greenrobot.eventbus.Subscribe;
    import org.greenrobot.eventbus.ThreadMode;
    
    import java.util.Locale;
    
    
    public class BaseActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            EventBus.getDefault().register(this);
            changeAppLanguage();
        }
    
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onEvent(String str) {
            switch (str) {
                case "EVENT_REFRESH_LANGUAGE":
                    changeAppLanguage();
                    recreate();//刷新界面
                    break;
            }
        }
    
    
        public void changeAppLanguage() {
            String sta = Store.getLanguageLocal(this);
            if(sta != null && !"".equals(sta)){
                // 本地语言设置
                Locale myLocale = new Locale(sta);
                Resources res = getResources();
                DisplayMetrics dm = res.getDisplayMetrics();
                Configuration conf = res.getConfiguration();
                conf.locale = myLocale;
                res.updateConfiguration(conf, dm);
            }
    
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            EventBus.getDefault().unregister(this);
        }
    }
    package com.loaderman.language;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;
    
    
    
    public class Store {
    
        public static void setLanguageLocal(Context context, String language){
            SharedPreferences preferences;
            SharedPreferences.Editor editor;
            preferences = PreferenceManager.getDefaultSharedPreferences(context);
            editor = preferences.edit();
            editor.putString("language", language);
            editor.commit();
        }
    
        public static String getLanguageLocal(Context context){
            SharedPreferences preferences;
            preferences = PreferenceManager.getDefaultSharedPreferences(context);
            String language = preferences.getString("language", "");
            return language;
        }
    }
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends BaseActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setTitle("第一个Activity");
            findViewById(R.id.btn_setting).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(MainActivity.this, SettingActivity.class));
                }
            });
        }
    }
    package com.loaderman.language;
    
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AlertDialog;
    import android.view.View;
    import android.widget.Button;
    
    import org.greenrobot.eventbus.EventBus;
    
    
    public class SettingActivity extends BaseActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setTitle("设置Activity");
            final String[] cities = {getString(R.string.lan_chinese), getString(R.string.lan_en), getString(R.string.lan_ja), getString(R.string.lan_de)};
            final String[] locals = {"zh_CN", "en", "ja", "de"};
            Button button = (Button)findViewById(R.id.btn_setting);
            button.setText("Language");
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                    builder.setIcon(R.mipmap.ic_launcher);
                    builder.setTitle(R.string.select_language);
                    builder.setItems(cities, new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            Store.setLanguageLocal(SettingActivity.this, locals[which]);
                            EventBus.getDefault().post("EVENT_REFRESH_LANGUAGE");
                        }
                    });
                    builder.show();
                }
            });
        }
    }

    效果图:

  • 相关阅读:
    一百个人眼中的一百种爱情
    苏迪曼杯羽毛球赛
    站点地图
    魔兽争霸微操教学(精华篇)
    (转)为winform程序注册闪屏(等待窗体)
    (转)Blend操作入门: 别站在门外偷看,快进来吧!
    (转)Asp.Net MVC的路由机制
    (转)VS2010在制作Setup项目的时候没有.NET 2.0的先决条件发行包
    (转)打造自己的LINQ Provider(上):Expression Tree揭秘
    (转)C#中的委托,匿名方法和Lambda表达式
  • 原文地址:https://www.cnblogs.com/loaderman/p/10600129.html
Copyright © 2020-2023  润新知