我想制作多语言这个应用程序,它运行正常,但问题是重新加载页面以更改所选语言,重新加载后它不会在微调器中显示选定的语言,但会更改视图中的语言。问题是我如何在重新加载或更新lang后设置所选语言。另外我想在进一步的活动中选择这个选定的语言来加载所选语言中的内容。 谢谢。抱歉我发布的任何错误都是新的。请忽略动作栏中的微调器。
包com.ssoft.myapp;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Locale;
public class Login extends ActionBarActivity implements ActionBar.OnNavigationListener {
Locale myLocale;
String lan;
String[] Country_list = {"English(US)", "German", "French"};
Integer[] flags = {R.drawable.ic_us, R.drawable.ic_ger, R.drawable.ic_frn};
public TextView frgt;
Button login;
EditText et_UserName = null, et_Password = null;
String String_Email = "", String_Password;
String email_Pattern = "[a-zA-Z0-9._]+@[a-z]+\\.+[a-z]+";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final Spinner mySpinner = (Spinner) findViewById(R.id.spinner);
mySpinner.setAdapter(new MyCustomAdapter(Login.this,
R.layout.language, Country_list, flags));
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
lan = mySpinner.getSelectedItem().toString();
Log.e("Language Selected", lan);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("lan", "lan");
/*if (i==0) {
setLocale("en");
}
else*/
if (i == 1) {
setLocale("de");
} else if (i == 2) {
setLocale("fr");
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
frgt = (TextView) findViewById(R.id.forgot);
et_UserName = (EditText) findViewById(R.id.et_UserName);
et_Password = (EditText) findViewById(R.id.et_Password);
login = (Button) findViewById(R.id.btn_Login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String_Email = et_UserName.getText().toString();
String_Password = et_Password.getText().toString();
if (TextUtils.isEmpty(String_Email) || TextUtils.isEmpty(String_Password)) {
if (TextUtils.isEmpty(String_Email))
et_UserName.setError("Please enter email");
if (TextUtils.isEmpty(String_Password))
et_Password.setError("Please enter password");
} else if (!String_Password.matches("[a-zA-Z0-9.?]*")) {
et_Password.setError("special character not allowed");
} else if (!String_Email.matches(email_Pattern)) {
et_UserName.setError("Please enter a valid email");
} else {
Intent i1 = new Intent(Login.this, MainActivity.class);
startActivity(i1);
}
}
});
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
final String[] dropdownValues = getResources().getStringArray(
R.array.languages);
ArrayAdapter adapter = new ArrayAdapter(
actionBar.getThemedContext(),
android.R.layout.simple_spinner_item, android.R.id.text1,
dropdownValues);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
actionBar.setListNavigationCallbacks(adapter, this);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.newheader));
}
@Override
public boolean onNavigationItemSelected(int i, long l) {
/*if (i==0) {
setLocale("en");
Log.d("Selected item is","English");
}
else*/
if (i == 1) {
setLocale("de");
} else if (i == 2) {
setLocale("fr");
}
return true;
}
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);
Intent refresh = new Intent(this, Login.class);
}
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects, Integer[] image) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
// return super.getView(position, convertView, parent);
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.language, parent, false);
TextView label = (TextView) row.findViewById(R.id.country);
label.setText(Country_list[position]);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
icon.setImageResource(flags[position]);
return row;
}
}
public class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(
parent.getContext(),
"The country is "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
答案 0 :(得分:0)
从我在代码中收集的内容来看,存储和恢复语言存在问题。以下两种方法可以帮助您实现这一目标。加载语言后,您可以初始化微调器(或任何其他视图)。
public void saveLanguage(String language)
{
SharedPreferences pref = getApplicationContext()
.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("lan", language);
editor.commit();
}
public String loadLanguage(String defaultLanguage)
{
SharedPreferences pref = getApplicationContext()
.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
return pref.getString("lan", defaultLanguage);
}
在重新创建活动之前,您必须在loadLanguage
中调用onCreate
,在更改语言时调用saveLanguage
。您应该将defaultLanguage
参数设置为默认加载的任何语言,因此如果尚未保存首选项,loadLanguage
将返回默认值。
基于Change language programmatically in Android设置区域设置应该在您必须使其工作的每个活动中onCreate
完成。另外setLocale
应该只刷新语言环境配置而不是重新启动Activity。只有在用户选择不同语言时才应重新启动活动。
您的setLocale
和restartActivity
方法将是:
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);
}
public void restartActivity() {
if (Build.VERSION.SDK_INT >= 11) {
recreate();
}
else {
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
overridePendingTransition(0, 0);
}
}
为了更轻松地将值加载并保存到微调器,除了Locale_list
Country_list
List<String> Locale_list = new ArrayList<String>(Arrays.asList("en", "de", "fr"));
onCreate
中的代码如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String lan = loadLanguage("en");
setLocale(lan);
setContentView(R.layout.login);
Spinner mySpinner = (Spinner) findViewById(R.id.spinner);
mySpinner.setAdapter(new MyCustomAdapter(this, R.layout.language, Country_list));
mySpinner.setSelection(Locale_list.indexOf(lan));
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
String lan = Locale_list.get(i);
saveLanguage(lan);
restartActivity();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
我可以看到你正在保存语言,但你没有在任何地方加载它。另外,当您致电editor.putString("lan", "lan");
editor.putString("lan", lan);
时,您有明显的错误,因为您的代码不是存储变量lan
的值,而是存储字符串"lan"
。