我在Eclipse IDE上使用Android应用程序,我的目标是将textviews的文本和按钮的文本从英语转换为阿拉伯语,反之亦然。 我在activity_main.xml上有两个Id阿拉伯语&按钮。英语 我有两个英文值的文件夹/ res /值和/ res / values-ar表示阿拉伯语值 当我按下英语按钮时,文本将转换为英语(应用程序从默认值文件夹/res/values/strings.xml中读取)。
但是当我按下阿拉伯语按钮时,文本保持不变(它们不会按预期返回阿拉伯语)。
这些是我在main_activity.xml文件上的按钮
<Button
android:id="@+id/English"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="بالانجليزي" />
<Button
android:id="@+id/Arabic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="بالعربية" />
这是活动
public class MainActivity extends Activity
{
private Button English,Arabic;
private Locale myLocale;
public void changeLang(String lang)
{
if (lang.equalsIgnoreCase(""))
return;
myLocale = new Locale(lang);
saveLocale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
updateTexts();
}
public void saveLocale(String lang)
{
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(langPref, lang);
editor.commit();
}
public void loadLocale()
{
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
String language = prefs.getString(langPref, "");
changeLang(language);
}
private void updateTexts()
{
English.setText(R.string.English);
Arabic.setText(R.string.Arabic);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.English= (Button)findViewById(R.id.English);
this.Arabic = (Button)findViewById(R.id.Arabic);
Button english = (Button) findViewById(R.id.English);
english.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String lang = "en";
switch (v.getId()) {
case R.id.English:
lang = "en";
break;
case R.id.Arabic:
lang = "ar";
break;
default:
break;
}
changeLang(lang);
}
});
Button Arabic= (Button) findViewById(R.id.Arabic);
english.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String lang = "ar";
switch (v.getId()) {
case R.id.English:
lang = "en";
break;
case R.id.Arabic:
lang = "ar";
break;
default:
break;
}
changeLang(lang);
}
});
}}
这是默认文件值\ strings.xml(用于存储英文值)
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string name="app_name">كومتاس</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="green_color">#33CCFF</string>
<string name="application_title">كومتاس</string>
<string name="Life_Plus_Button">Life Palace</string>
<string name="Arabic">Arabic language</string>
<string name="English">English language</string>
</resources>
此文件用于存储阿拉伯语值res \ values-ar \ strings.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources><string name="app_name">كومتاس</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="green_color">#33CCFF</string>
<string name="English">بالانجليزية</string>
<string name="Arabic">بالعربية</string>
<string name="Life_Plus_Button">لايف بلس</string>
</resources>
答案 0 :(得分:2)
您两次致电英语 button
clicklistener。只需要修复你的代码:
Button Arabic= (Button) findViewById(R.id.Arabic);
English.setOnClickListener(new View.OnClickListener()
TO:
Arabic.setOnClickListener(new View.OnClickListener()
我建议在所有xml文件中把它放在最上面:
<?xml version="1.0" encoding="UTF-8"?>
希望有所帮助