gpt4 book ai didi

方向变化的android语言环境变化

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:43:47 26 4
gpt4 key购买 nike

在我的应用程序中,我有两个 Activity,一个可以向两侧旋转,另一个被锁定在横向模式。

以下是我的 list 文件详细信息,其中添加了 Activity

<activity
android:name="com.hogaming.android.Activities.LoginActivity"
android:configChanges="orientation|screenSize|keyboardHidden">
</activity>

<activity
android:name="com.android.activities.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden">
</activity>

在我使用微调器的登录 Activity 中,我正在更改语言环境并更新整个编辑文本和按钮文本。在按钮单击操作中,我正在更新 UI View ,当我旋转设备时,英语语言环境设置在更新的 View 上这是我的全部代码

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<Void, Void, String>
{
protected LoginActivity context;
protected Exception exception;
private String username;
private String password;

public LoginTask(LoginActivity context, String uname, String pwd) {
this.context = context;
this.username = uname;
this.password = pwd;
}

@Override
protected String doInBackground(Void... params) {
try
{
return HTTPHelper.LoginTaskData(this.context, username, password);
}
catch (Exception e)
{
exception = e;
Log.e(TAG, "Login Task Error", e);
}
return null;
}

@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
Log.e(TAG, "LoginTask: " + result);
if (result.equals("true"))
{
// moves to next activity
}
else if (result.equals("false"))
{
//showing an alert textview with selected language text
}
}
}

public void changeLang(String lang, int pos)
{
if (lang.length() != 0)
{
saveLocale(lang, pos, locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
updateTexts();
}
}

public void saveLocale(String lang, int pos, Locale locale)
{
SharedPreferences.Editor editor1 = langPrefs.edit();
editor1.remove(langPrefKey);
editor1.remove(langPosKey);
editor1.commit();

SharedPreferences.Editor editor = langPrefs.edit();
editor.putString(langPrefKey, lang);
editor.putInt(langPosKey, pos);
editor.commit();

langSelected = langPrefs.getString(langPrefKey, "");
langPosition = langPrefs.getInt(langPosKey, 0);
}

private void updateTexts()
{
// here i will once again set all textview String values, it changes to the selected language
}

@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (locale != null){
newConfig.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
}

最佳答案

如果我没理解错的话,问题是当你切换屏幕方向时手机的语言环境会恢复,尽管你有 android:configChanges="orientation|screenSize|keyboardHidden",在你的 AndroidManifest.xml。如果我在你身边,我会从不同的角度处理这个问题。我会摆脱 android:configChanges,让 android 在方向改变时重新启动 Activity。然后,我将使用 onSaveInstanceState 对来保存用户在 Spinner 中选择的 Locale。当 onCreate 再次被调用时,你知道由于方向改变而被调用,因为 bundle 不为空,读取存储的 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
}
}

关于方向变化的android语言环境变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30123357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com