android 多语言(在APP里面内切换语言)

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

}

技术分享

 

 

  

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。