在我的项目中,我必须实现多语言选项。为此我写了下面的代码。
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);
}
}
但是这只在重启活动后才起作用。我想点击列表视图更新。我知道这是可能的。但我不知道如何实现它。
答案 0 :(得分:1)
有两种方法可以解决这个问题:
方法1:
您可以覆盖列表片段中的onItemClick,并在该方法中,通过代码更新按钮的文本。
更新您的听众:
public interface onLanguageSelectedListener {
void setonLanguage();
void onLanguageItemSelected(String language);
}
在项目点击时调用回调:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mListener = (onLanguageSelectedListener) getActivity();
mListener.onLanguageItemSelected(language_id[position].toString());
}
然后在活动内部实现onLanguageSelected()
并更新按钮文本。
这种方式有其局限性,因为如果您想要更新的视图(按钮除外)更多,那么您必须逐个更新每个视图。
方法2:
此问题的更通用解决方案是在配置更改后重新加载内容视图。这里介绍了这种方法:https://stackoverflow.com/a/11610910/827110
但是,由于您希望每次选择其他项目时都更新文本,因此方法2可能不太合适。
答案 1 :(得分:0)
首先,在AdroidManifest.xml中,声明您将处理所有活动的区域设置更改:
<activity android:name="YourActivity" android:configChanges="locale" />
其次,您需要在所有活动类中处理onConfigurationChanged()回调:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Change text for all views
textView.setText(getString(R.string.text));
...
}