码迷,mamicode.com
首页 > 移动开发 > 详细

【Android 多语言切换简单实例分享】

时间:2015-07-28 00:55:39      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:android   语言   国际化   实例   

一.Android多语言切换

Android应用的开发不可能只针对某一个国家或者区域使用,各国间语言文化各不相同,因此一个优秀的APP必须支持多种语言,为了实现这个特性,Android给出了一个解决方式,在res目录下通过values+语言编码来实现多国语言的支持(中间采用连字符号-连接),例如:values-es代表英文,如果你决定了你的应用要支持多国语言,那么你就必须新建多种values-XXX的多语言支持文件,下面通过一个例子来看看基本的使用方式,先看下效果图:

技术分享

1.工程结构

技术分享

注:主要看下res/values-en/strings.xml代表英文字符串资源文件,res/values-zh/strings.xml代表中文.

  • res/values-en/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SwitchLanguage</string>
    <string name="hello_world">This is an instance of Android</string>
    <string name="switch_language">Switch Language</string>
    <string name="english">English</string>
    <string name="chinese">Chinese</string>
    <string name="jump">Jump to Next Activity</string>
    <string name="change">The language of the page has changed, too.</string>
    <string name="showtext">You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.</string>
</resources>
  • res/values-zh/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">切换语言</string>
    <string name="hello_world">这是一个Android实例</string>
    <string name="switch_language">切换语言</string>
    <string name="english">英语</string>
    <string name="chinese">中文</string>
    <string name="jump">跳转到另一个页面</string>
    <string name="change">这个页面语言也发生了变化</string>
    <string name="showtext">你不能改变你的过去,但你可以让你的未来变得更美好。一旦时间浪费了,生命就浪费了。</string>

</resources>

2.代码实现

  1. BaseActivity.java
public class BaseActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // 初始化PreferenceUtil
        PreferenceUtil.init(this);
        // 根据上次的语言设置,重新设置语言
        switchLanguage(PreferenceUtil.getString("language", "zh"));
    }

    /**
     * 
     * <切换语言>
     * 
     * @param language
     * @see [类、类#方法、类#成员]
     */
    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);
    }
}

2、 MainActivity.java

public class MainActivity extends BaseActivity implements OnClickListener{

    private Dialog mDialog;

    private TextView textShow;

    private Button button;

    private Button button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = (TextView) findViewById(R.id.text);
        button = (Button) findViewById(R.id.btn);
        button2 = (Button) findViewById(R.id.btn_2);
        textShow = (TextView)findViewById(R.id.text_show);
        textView.setText(R.string.hello_world);
        button.setText(R.string.switch_language);
        //监听切换语言按钮,弹出dialog选择语言
        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);
    }

}
  • activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="@string/switch_language" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="@string/jump" />

    <TextView
        android:id="@+id/text_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:textSize="18sp"
        android:text="@string/showtext" />

</RelativeLayout>

3、 工具类PreferenceUtil.java

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);
    }
}

4、 SecondActivity.java是要跳转的Activity,比较简单,只有一个TextView用来显示文本,语言切换,文本语言也随之切换。

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}
  • activity_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/change" />

</RelativeLayout>

例子比较简单,关键代码都有注释,有问题欢迎留言讨论,共同学习。

参考文献:http://my.oschina.net/jack1900/blog/290694

版权声明:本文为博主原创文章,未经博主允许不得转载。

【Android 多语言切换简单实例分享】

标签:android   语言   国际化   实例   

原文地址:http://blog.csdn.net/wangkeke1860/article/details/47093985

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!