android 实现应用内语言切换

来源:互联网 发布:网络播放器安装电视猫 编辑:程序博客网 时间:2024/06/05 20:37

android开发中经常需要实现各种语言的切换功能,网上也有一些解决方案,经本人测试都不算好用, 以下是我研究的解决方案,绝对对你有帮助.


Demo下载地址:http://download.csdn.net/detail/qq_26420489/9392952


方法/步骤

  1. 1,strings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">MoreLanguageSwitch</string>
        <string name="action_settings">Settings</string>
        <string name="hello_world">Hello world!</string>
        <string name="app_name_en">my app</string>
        <string name="app_name_zh">去旅游网</string>
    </resources>

  2. 2. MainActivity

    package com.example.morelanguageswitch;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class MainActivity extends Activity implements OnClickListener {
    private Button cnBtn;
    private Button enBtn;
    private TextView appNameText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cnBtn = (Button) findViewById(R.id.cnBtn);
    enBtn = (Button) findViewById(R.id.enbtn);
    appNameText = (TextView) findViewById(R.id.appNameText);
    appNameText.setText(Tools.getStringResId(this, "app_name",
    Tools.getLanguage(this)));
    cnBtn.setOnClickListener(this);
    enBtn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    @Override
    public void onClick(View v) {
    SharedPreferences sp = getSharedPreferences("language",
    Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    switch (v.getId()) {
    case R.id.cnBtn:
    editor.putString("language", "zh");
    editor.commit();
    refresh(this);
    break;
    case R.id.enbtn:
    editor.putString("language", "en");
    editor.commit();
    refresh(this);
    break;
    default:
    break;
    }
    }

    private void refresh(Context context) {
    finish();
    Intent intent = new Intent(context, context.getClass());
    startActivity(intent); // overridePendingTransition(R.anim.new_dync_in_from_right,
    // R.anim.new_dync_out_to_left); } }
    }
    }

  3. 3.Tools

    package com.example.morelanguageswitch;

    import java.util.Locale;
    import android.app.Activity;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.util.Log;

    public class Tools {
    public static int getStringResId(Context context, String name,
    String language) {
    return context.getResources().getIdentifier(name + "_" + language,
    "string", context.getPackageName());
    }

    public static String getLanguage(Context context) {
    SharedPreferences sp = context.getSharedPreferences("language",
    Activity.MODE_PRIVATE);
    String language = sp.getString("language", "");
    if (language == null || language.equals("")) {
    language = Locale.getDefault().getLanguage();
    }
    Log.v("message", "language new :" + language);
    return language;
    }
    }

  4. 4.activity_main.xml

  5. <LinearLayout 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:orientation="vertical" >

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

        <TextView
            android:id="@+id/appNameText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/cnBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CN" />

        <Button
            android:id="@+id/enbtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="EN" />

    </LinearLayout>
  6. 注:Demo下载地址:http://download.csdn.net/detail/qq_26420489/9392952点击打开链接

  1. 4
1 0
原创粉丝点击