我是java的新手,实际上正在开发一款游戏应用程序,我想添加一项可以改变游戏语言的功能。
我已经制作了2个strings.xml。一个是默认(英语),另一个是翻译版本(fil)
这是我的代码
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class LanguageActivity extends Activity {
private static Button button_fil;
private static Button button_eng;
public void onButtonClickListener() {
button_fil = (Button) findViewById(R.id.btnFilipino);
button_fil.setOnClickListener(
new View.OnClickListener() {@
Override
public void onClick(View v) {
Toast.makeText(LanguageActivity.this, "Filipino Language", Toast.LENGTH_SHORT).show();
}
}
);
button_eng = (Button) findViewById(R.id.btnEnglish);
button_eng.setOnClickListener(
new View.OnClickListener() {@
Override
public void onClick(View v) {
Toast.makeText(LanguageActivity.this, "English Language", Toast.LENGTH_SHORT).show();
}
}
);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.language);
onButtonClickListener();
}
}
非常感谢!
答案 0 :(得分:10)
请试试这个例子。也许它会帮助你。 在这里,我使用微调器来选择语言。
在你的行动中
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class AndroidLocalize extends Activity {
Spinner spinnerctrl;
Button btn;
Locale myLocale;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinnerctrl = (Spinner) findViewById(R.id.spinner1);
spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if (pos == 1) {
Toast.makeText(parent.getContext(),
"You have selected Tamil", Toast.LENGTH_SHORT)
.show();
setLocale("ta");
} else if (pos == 2) {
Toast.makeText(parent.getContext(),
"You have selected Hindi", Toast.LENGTH_SHORT)
.show();
setLocale("hi");
} else if (pos == 3) {
Toast.makeText(parent.getContext(),
"You have selected English", Toast.LENGTH_SHORT)
.show();
setLocale("en");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
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, AndroidLocalize.class);
startActivity(refresh);
}
}
XML中的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/greet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/greet"
android:textSize="25sp" android:gravity="center" android:paddingTop="25sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/langselection"
android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center" android:paddingTop="25sp"/>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/languages"
android:gravity="center" android:paddingTop="25sp" />
</LinearLayout>
然后为您的语言添加strings.xml 喜欢
<resources>
<string name="app_name">Androidlocalization</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_android_localize">AndroidLocalize</string>
<string name="greet">बधाई सचिन !!</string>
<string name="langselection">जिस भाषा में आप सचिन को नमस्कार करना चाहते हैं का चयन करें!!!!</string>
<string name="chooselang">Choose the language</string>
<string-array name="languages">
<item>Select language</item>
<item>தமிழ்</item>
<item>हिंदी</item>
<item>English</item>
</string-array>
</resources>
请更新你的清单,我希望能解决你的问题..
像这样更新。
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".AndroidLocalize"
android:label="@string/title_activity_android_localize"
android:configChanges="locale|orientation|keyboardHidden"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 1 :(得分:4)
这是我写的一个方法,对我来说非常好用来改变应用程序中的语言(而且只是单个应用程序 - 而不是整个设备):
private void setLanguageForApp(String languageToLoad){
Locale locale;
if(languageToLoad.equals("not-set")){ //use any value for default
locale = Locale.getDefault();
}
else {
locale = new Locale(languageToLoad);
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
注意:每次打开应用时,都会在第一个活动的setContentView()
onCreate()
之前调用此方法。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setLanguageForApp("en"); //hard-coded here - get from whereever you stored
setContentView(R.layout.activity_category_list);
...
...
...
将选定的区域设置代码存储在共享首选项中,并检索以作为参数传递。
语言选择对话框的方法:(注意:语言更改后重新加载应用程序以使语言更改生效)
private void showLanguageChangePopup() {
CharSequence languages[] = new CharSequence[] {
"English",
"हिंदी (Hindi)",
"Français (French)",
"Italiano (Italian)",
"Deutsch (German)",
"Español (Spanish)",
"日本語 (Japanese)",
"í•œêµì–´ (Korean)",
"Nederlands (Dutch)",
"Português (Portuguese)",
"руÑÑкий (Russian)",
"ä¸æ–‡ (Chinese)",
"العربية (Arabic)"
};
final String codes[] = new String[] {
"en",
"hi",
"fr",
"it",
"de",
"es",
"ja",
"ko",
"nl",
"pt",
"ru",
"zh",
"ar"
};
int currentLangIndex = Prefs.getUserPreferenceIntValue(Prefs.Key.SELECTED_LANGUAGE_INDEX, getBaseContext());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.text_select_language);
builder.setSingleChoiceItems(languages, currentLangIndex, null);
builder.setNegativeButton(R.string.text_translate_cancel, null);
builder.setPositiveButton(R.string.action_change_language, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
int selectedIndex = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
Prefs.setUserPreferenceStringValue(Prefs.Key.LANGUAGE, codes[selectedIndex], getBaseContext());
Prefs.setUserPreferenceIntValue(Prefs.Key.SELECTED_LANGUAGE_INDEX, selectedIndex, getBaseContext());
Intent i = new Intent(CategoryListActivity.this, CategoryListActivity.class);
startActivity(i);
finish();
}
});
builder.show();
}
答案 2 :(得分:3)
答案 3 :(得分:2)
当您支持多种语言时,您需要创建单独的值文件夹,例如values-fr,并将stings.xml文件放在此文件夹中。应该管用。希望这有帮助!
答案 4 :(得分:2)
<resources>
<string name="app_name">Androidlocalization</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_android_localize">AndroidLocalize</string>
<string name="greet">बधाई सचिन !!</string>
<string name="langselection">जिस भाषा में आप सचिन को नमस्कार करना चाहते हैं का चयन करें!!!!</string>
<string name="chooselang">Choose the language</string>
<string-array name="languages">
<item>Select language</item>
<item>தமிழ்</item>
<item>हिंदी</item>
<item>English</item>
</string-array>
</resources>
每个代码都在同一文件夹中,为不同的语言添加不同的值文件夹
例如,hindi的值文件夹进入value-hi