我正在尝试将微调器更改为:按钮 > 对话框 > 微调器。
完美运行,但我看不到下面的消息(发生错误并关闭应用程序):
“您已选择英语 en-us”、“您已选择葡萄牙语 pt-br”
前:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if (pos == 0) {
Toast.makeText(parent.getContext(),
"You have selected English en-us", Toast.LENGTH_SHORT)
.show();
setLocale("en");
} else if (pos == 1) {
Toast.makeText(parent.getContext(),
"You have selected Português pt-br", Toast.LENGTH_SHORT)
.show();
setLocale("br");
}
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}
public void setLocale(String lang)
{
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
我的新代码:
Button bt_lang = (Button) findViewById(R.bt.language);
bt_lang.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Select Language");
builder.setAdapter(adapter, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
AdapterView<?> parent = null;
switch(which)
{
case 0:
setLocale("en");
Toast.makeText(parent.getContext(),
"You have selected English en-us", Toast.LENGTH_SHORT)
.show();
case 1:
setLocale("br");
Toast.makeText(parent.getContext(),
"Você selecionou Português pt-br", Toast.LENGTH_SHORT)
.show();
break;
}
dialog.dismiss();
}
});
builder.create();
builder.show();
}
});
}
public void setLocale(String lang)
{
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}