![中英文切换主要的地方城市选择](https://img-blog.csdnimg.cn/20190902144406349.jpg)
MainActivity 界面
public class MainActivity extends AppCompatActivity {
private TextView textview;
private Button button;
private Button button1;
private Configuration config;
private Intent intent;
private DisplayMetrics metrics;
private Resources resources;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initSetData();
}
//点击事件
private void initSetData() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取当时所有数据是为什么语言
String sta=getResources().getConfiguration().locale.getLanguage();
//判断
shiftLanguage(sta);
}
});
}
//判断
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().getResources().getConfiguration();
config.locale = Locale.CHINESE;
getBaseContext().getResources().updateConfiguration(config
, getBaseContext().getResources().getDisplayMetrics());
refreshSelf();
}
}
//跳转
public void refreshSelf(){
//跳界面
Intent intent=new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
//初始化
private void initView() {
textview = (TextView) findViewById(R.id.textview);
button = (Button) findViewById(R.id.button);
}
MainActivity 布局
中文strings
数据的卡会尽快
英文strings
Thedatacardwillbeassoonaspossible