您的位置:首页 > 移动开发 > Android开发

android开发中英文切换

2016-02-14 15:51 411 查看
今天介绍下安卓开发下面,如何实现中英文相互切换,

一,需要在res下新建文件夹,大陆中文为values-zh-rCN,英文values-en-rUS,更多可以参考http://my.oschina.net/quttap/blog/204499,直接用values-zh,values-en也可以,可以自己查询,然后其下新建strings.xml,(注:android默认访问values下strings.xml,当设置其它语言找不到对应文件夹时,将转而访问values下的strings.xml)

二,通过getResources().getConfiguration().locale.getLanguage()来获取当前应用语言,中文为zh,英文为en,更多可以参考http://blog.sina.com.cn/s/blog_7981f91f01012wm7.html

三,在androidmianfest.xml中配置下属性android:configChanges="locale",就是利用系统类设置语言,附主要代码,

@Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                String sta=getResources().getConfiguration().locale.getLanguage();

                shiftLanguage(sta);

            }

//shift language

    public void shiftLanguage(String sta){

        if(sta.equals("zh")){

        Locale.setDefault(Locale.ENGLISH);

        Configuration config = getBaseContext().getResources().getConfiguration();

                config.locale = Locale.ENGLISH;

                getBaseContext().getResources().updateConfiguration(config

                    , getBaseContext().getResources().getDisplayMetrics());

                refreshSelf();

        }else{

            Locale.setDefault(Locale.CHINESE);

            Configuration config = getBaseContext().get
4000
Resources().getConfiguration();

            config.locale = Locale.CHINESE;

            getBaseContext().getResources().updateConfiguration(config

                , getBaseContext().getResources().getDisplayMetrics());

            refreshSelf();

        }

    }

    //refresh self

    public void refreshSelf(){

        Intent intent=new Intent(this,MainActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        startActivity(intent);

    }

效果图:







更多语言切换大家可举一返三
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息