在我的应用程序中,我有两个活动,一个可以旋转到两侧,另一个被锁定在横向模式.
在我的登录活动中使用微调器我正在更改区域设置并更新整个编辑文本和按钮文本.
在按钮单击操作中,我正在更新UI视图,当我旋转设备时,英语区域设置在更新的视图上设置
这是我的整个代码
public class LoginActivity extends Activity
{
Locale locale = null;
Spinner langSpinner;
private SharedPreferences langPrefs;
String langSelected = "";
int langPosition = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.loginscreen);
langPrefs = PreferenceManager.getDefaultSharedPreferences(this);
langSelected = langPrefs.getString(langPrefKey,"");
langPosition = langPrefs.getInt(langPosKey,0);
langSpinner = (Spinner)this.findViewById(R.id.lanuage_spinner1);
langSpinner.setSelection(langPosition);
langSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView> arg0,View arg1,int pos,long arg3)
{
if (pos == 0)
{
langSelected ="en";
locale = Locale.ENGLISH;
}
else if (pos == 1)
{
langSelected ="it";
locale = Locale.ITALIAN;
}
else if (pos == 2)
{
langSelected ="zh";
locale = Locale.SIMPLIFIED_CHINESE;
}
else if (pos == 3)
{
langSelected ="zh-rTW";
locale = Locale.TRADITIONAL_CHINESE;
}
changeLang(langSelected,pos);
}
@Override
public void onNothingSelected(AdapterView> arg0) {
// TODO Auto-generated method stub
}
});
btnLogin = (Button) this.findViewById(R.id.LoginButton);
btnLogin.setOnClickListener(new ButtonClickListener());
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
this.finish();
}
@Override
protected void onDestroy()
{
super.onDestroy();
this.finish();
}
public class ButtonClickListener implements OnClickListener {
public void onClick(View v) {
final LoginTask validateTask = new LoginTask(context,usernameField.getText().toString(),passwordField.getText().toString());
validateTask.execute();
}
// Hide the keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(passwordField.getWindowToken(),0);
}
public class LoginTask extends AsyncTask
最佳答案
如果我理解正确,问题是当你切换屏幕方向时手机的语言环境会恢复,尽管你的AndroidManifest.xml中有android:configChanges =“orientation | screenSize | keyboardHidden”.如果我在你身边,我会从不同的角度来处理这个问题.我会摆脱android:configChanges,当方向改变时,让android重新启动Activity.然后我将使用onSaveInstanceState对来保存用户在Spinner中选择的Locale.再次调用onCreate时,由于方向更改而因为包不为空而调用,因此请读取存储的Locale并重新启动,然后再次更新配置.例如.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerializable("LOCALE",locale);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginscreen);
if (savedInstanceState != null) {
locale = (Locale) savedInstanceState.getSerializable("LOCALE");
if (locale != null) {
// update the configuration
}
}