博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 多语言(在APP里面内切换语言)
阅读量:6341 次
发布时间:2019-06-22

本文共 5261 字,大约阅读时间需要 17 分钟。

创建SharedPreferences的管理类

public class PreferenceUtil {    private static SharedPreferences mSharedPreferences = null;    private static Editor mEditor = null;        public static void init(Context context){        if (null == mSharedPreferences) {            mSharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context) ;        }    }        public static void removeKey(String key){        mEditor = mSharedPreferences.edit();        mEditor.remove(key);        mEditor.commit();    }        public static void removeAll(){        mEditor = mSharedPreferences.edit();        mEditor.clear();        mEditor.commit();    }    public static void commitString(String key, String value){        mEditor = mSharedPreferences.edit();        mEditor.putString(key, value);        mEditor.commit();    }        public static String getString(String key, String faillValue){        return mSharedPreferences.getString(key, faillValue);    }        public static void commitInt(String key, int value){        mEditor = mSharedPreferences.edit();        mEditor.putInt(key, value);        mEditor.commit();    }        public static int getInt(String key, int failValue){        return mSharedPreferences.getInt(key, failValue);    }        public static void commitLong(String key, long value){        mEditor = mSharedPreferences.edit();        mEditor.putLong(key, value);        mEditor.commit();    }        public static long getLong(String key, long failValue) {        return mSharedPreferences.getLong(key, failValue);    }        public static void commitBoolean(String key, boolean value){        mEditor = mSharedPreferences.edit();        mEditor.putBoolean(key, value);        mEditor.commit();    }        public static Boolean getBoolean(String key, boolean failValue){        return mSharedPreferences.getBoolean(key, failValue);    }}

创建BaseActivity类

public class BaseActivity extends Activity {        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //初始化PreferenceUtil        PreferenceUtil.init(this);        //根据上次的语言设置,重新设置语言        switchLanguage(PreferenceUtil.getString("language", "zh"));    }            protected void switchLanguage(String language) {        //设置应用语言类型        Resources resources = getResources();        Configuration config = resources.getConfiguration();        DisplayMetrics dm = resources.getDisplayMetrics();       if (language.equals("en")) {            config.locale = Locale.ENGLISH;        } else {             config.locale = Locale.SIMPLIFIED_CHINESE;        }        resources.updateConfiguration(config, dm);                //保存设置语言的类型        PreferenceUtil.commitString("language", language);    }}

MainActivity

public class MainActivity extends BaseActivity implements OnClickListener{    private Dialog mDialog;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        DisplayMetrics dm=new DisplayMetrics();        getWindowManager().getDefaultDisplay().getMetrics(dm);        String str="mobile分辨率为:"+dm.widthPixels+"X"+dm.heightPixels;        Log.i("L",str);        TextView textView = (TextView) findViewById(R.id.text);        Button button = (Button) findViewById(R.id.btn);        Button button2 = (Button) findViewById(R.id.btn_2);        textView.setText(R.string.hello_world);        button.setText(R.string.switch_language);                button.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                if (mDialog == null) {                    LayoutInflater inflater = getLayoutInflater();                    View layout = inflater.inflate(R.layout.dialog_select_lanuage,null);                    TextView english = (TextView) layout.findViewById(R.id.select_english);                    TextView chinese = (TextView) layout.findViewById(R.id.select_chinese);                    mDialog = new Dialog(MainActivity.this, R.style.Custom_Dialog_Theme);                    mDialog.setCanceledOnTouchOutside(false);                    english.setOnClickListener(MainActivity.this);                    chinese.setOnClickListener(MainActivity.this);                    mDialog.setContentView(layout);                }                mDialog.show();            }        });                button2.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                Intent it = new Intent(MainActivity.this, SecondActivity.class);                startActivity(it);            }        });    }    @Override    public void onClick(View v) {        mDialog.dismiss();            switch (v.getId()) {            case R.id.select_english:                switchLanguage("en");                break;            case R.id.select_chinese:                switchLanguage("zh");                break;                default:                break;        }        //更新语言后,destroy当前页面,重新绘制        finish();        Intent it = new Intent(MainActivity.this, MainActivity.class);        startActivity(it);    }}

 

 

  

 

转载于:https://www.cnblogs.com/stuart/p/4522066.html

你可能感兴趣的文章
python 字符编码
查看>>
269D Maximum Waterfall
查看>>
C++11 多线程
查看>>
sed-加速你在Linux的文件编辑
查看>>
HttpServer发送数据到kafka
查看>>
phpcms站---去除域名绑定目录中的HTML
查看>>
2017-5-3 打印控件、MDI 窗体容器
查看>>
20155303 2016-2017-2 《Java程序设计》第九周学习总结
查看>>
一次很失败的抄底
查看>>
数据结构C++(10)二叉树——链表实现(linkBinaryTree)
查看>>
利用Condition实现多线程交替执行
查看>>
里氏替换原则(设计模式原则2)
查看>>
lamp一键安装
查看>>
解决“iOS 7 app自动更新,无法在app中向用户展示更新内容”问题
查看>>
OpenCV——Haar-like特征
查看>>
HttpWebResponse发送post请求并接收
查看>>
python 相对路径和绝对路径的区别
查看>>
Day36 python基础--并发编程基础5
查看>>
《Python从小白到大牛》第6章 数据类型
查看>>
三层架构的是与非
查看>>