zoukankan      html  css  js  c++  java
  • 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();
                }
            });
        }
    }

    效果图:

  • 相关阅读:
    javascript 笔记
    i18n,国际化翻译,excel与js互转
    nginx 一个端口布署多个单页应用(history路由模式)。
    html, js,css应用文件路径规则
    vue响应式原理,去掉优化,只看核心
    js 大量数据优化,通用方法
    nginx 常用的location rewrite proxy_pass
    javascript,排列组合
    zk分布式任务管理
    springboot+mybatis+dubbo+aop日志终结篇
  • 原文地址:https://www.cnblogs.com/loaderman/p/10600129.html
Copyright © 2011-2022 走看看