我自己认为这个是超简单的,不信的话你们可以试试,也特别容易理解,需要注意的是,我是看的别人的,转载的,可是我找不到原来的博客了,真不好意思,希望原博主看见了不要 说我抄袭啊,我只是想传播信息罢了!
首先看看效果图
大概就是这样子,然后我们看看代码!
首先就是你的valuse里面的代码,这个就是用来切换的
不会创建的,可以百度,这里就不写了,
然后写个baseactivity,因为要实现进来就获取之前保存的状态,所以就把切换写在base里面,这里我还用到了eventbus来保存的,
compile 'org.greenrobot:eventbus:3.0.0'这个就是eventbus的依赖,
/**
* Created by dell on 2018/5/21.
* Date:2018/5/21
* Time:15:10
* author:NYB
*/
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.Locale;
public class BaseAc extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
changeAppLanguage();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(String str) {
switch (str) {
case "EVENT_REFRESH_LANGUAGE":
changeAppLanguage();
recreate();//刷新界面
break;
}
}
public void changeAppLanguage() {
String sta = Store.getLanguageLocal(this);
if(sta != null && !"".equals(sta)){
// 本地语言设置
Locale myLocale = new Locale(sta);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
接下来就是相对应的工具类Store
package com.example.dell.duoyuyan;
/**
* Created by dell on 2018/5/21.
* Date:2018/5/21
* Time:15:11
* author:NYB
*/
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class Store {
public static void setLanguageLocal(Context context, String language){
SharedPreferences preferences;
SharedPreferences.Editor editor;
preferences = PreferenceManager.getDefaultSharedPreferences(context);
editor = preferences.edit();
editor.putString("language", language);
editor.commit();
}
public static String getLanguageLocal(Context context){
SharedPreferences preferences;
preferences = PreferenceManager.getDefaultSharedPreferences(context);
String language = preferences.getString("language", "");
return language;
}
}
然后就是main
package com.example.dell.duoyuyan;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends BaseAc {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("第一个Activity");
findViewById(R.id.btn_setting).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SettingAc.class));
}
});
}
}
最后就是main跳转的第二个页面,这里我用的是一个布局,
package com.example.dell.duoyuyan;
/**
* Created by dell on 2018/5/21.
* Date:2018/5/21
* Time:15:12
* author:NYB
*/
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import org.greenrobot.eventbus.EventBus;
public class SettingAc extends BaseAc {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("设置Activity");
final String[] cities = {getString(R.string.lan_chinese), getString(R.string.lan_en), getString(R.string.lan_ja), getString(R.string.lan_de)};
final String[] locals = {"zh_CN", "en", "ja", "de"};
Button button = (Button)findViewById(R.id.btn_setting);
button.setText("Language");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(SettingAc.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(R.string.select_language);
builder.setItems(cities, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Store.setLanguageLocal(SettingAc.this, locals[which]);
EventBus.getDefault().post("EVENT_REFRESH_LANGUAGE");
}
});
builder.show();
}
});
}
}
这里要注意的就是上面的数组,这个数组是相对应的,如果你想添加新的语言,或者就是不要那么多语言,你就要对应的删除就好了,locals 这个数组里面的数据,都是自己随便写的就行,
好了,这就是一个多语言切换的demo!
小白记录,希望对大家有用!