在我的项目中,我必须实现多语言选项。为此,我编写了以下代码。
Locale locale = new Locale(Language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
图片供参考:
在这里,我想更新所选语言的按钮文本。为此,我尝试以下方式。
在我的活动中。
我的片段代码是:
public class LanguageFragment extends ListFragment {
String[] language_id = new String[] { "", "tn", "ja" };
ListView lv;
SharedPreferences settings;
SharedPreferences.Editor editor;
onLanguageSelectedListener mListener;
int pos;
public interface onLanguageSelectedListener {
void setonLanguage();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
lv = getListView();
System.out.println("getlistview");
String english = getActivity().getString(R.string.english) + "";
String tamil = getActivity().getText(R.string.tamil) + "";
String japanse = getActivity().getText(R.string.japanese) + "";
String[] language_list = new String[] { english, tamil, japanse };
settings = getActivity().getSharedPreferences("LANGUAGE_TYPE", 0);
ListAdapter adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, language_list) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(android.R.layout.simple_list_item_1, null);
}
if (lv.isItemChecked(position)) {
// v.setActivated(true);
editor = settings.edit();
editor.putString("Position", lv.getCheckedItemPosition() + "");
editor.putString("Language",
language_id[lv.getCheckedItemPosition()].toString());
editor.commit();
mListener = (onLanguageSelectedListener) getActivity();
mListener.setonLanguage();
v.setBackgroundResource(R.drawable.blue_top1);
} else {
v.setBackgroundResource(0);
}
return super.getView(position, v, parent);
}
};
String s = settings.getString("Position", 0 + "");
pos = Integer.parseInt(s);
setListAdapter(adapter);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setItemChecked(pos, true);
lv.setSelector(R.drawable.blue_top1);
}
}
我的活动是:
public class SettingsActivity extends Activity implements
OnItemSelectedListener, onLanguageSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences("LANGUAGE_TYPE", 0);
String Language = settings.getString("Language", "");
Locale locale = new Locale(Language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.settings_activity);
}
public void onSettingsItemSelected(String link) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment language;
if ("language".equals(link)) {
language = new LanguageFragment();
transaction.replace(R.id.detailFragment, language);
transaction.commit();
}
}
public void setonLanguage() {
SharedPreferences settings = getSharedPreferences("LANGUAGE_TYPE", 0);
String Language = settings.getString("Language", "");
Locale locale = new Locale(Language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
name = getText(R.string.hi) + " " + name;
// Intent intent = getIntent();
// overridePendingTransition(0, 0);
// intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
// finish();
//
// overridePendingTransition(0, 0);
// startActivity(intent);
System.out.println("inside listener : " + Language);
}
}
但这仅在重新启动活动后才有效。我想在单击列表视图时更新。我知道这是可能的。但我不知道如何实现它。