如何从android的设备语言列表中更改我们自己的应用程序语言?
我希望动态更改我的应用程序语言,而不是输入任何硬编码字符串语言,如“us”、“uk”等 我想获取设备语言列表。 重要的是,我想打开活动中的语言列表,如对话框列表未设置。例如,当用户按下文本视图设备时,所有语言列表均已打开,当用户选择任何语言时,该语言将在他/她按下的同一文本视图上设置,同时整个应用程序语言也将更改。默认情况下,选择一种语言,例如英语如何从android的设备语言列表中更改我们自己的应用程序语言?,android,localization,device,Android,Localization,Device,我希望动态更改我的应用程序语言,而不是输入任何硬编码字符串语言,如“us”、“uk”等 我想获取设备语言列表。 重要的是,我想打开活动中的语言列表,如对话框列表未设置。例如,当用户按下文本视图设备时,所有语言列表均已打开,当用户选择任何语言时,该语言将在他/她按下的同一文本视图上设置,同时整个应用程序语言也将更改。默认情况下,选择一种语言,例如英语 请以代码的形式帮助我,并提供一些关于“OnActivityResult”或任何其他流程的信息。我非常喜欢谷歌,我发现每一个地方静态代码意味着硬代码字
请以代码的形式帮助我,并提供一些关于“OnActivityResult”或任何其他流程的信息。我非常喜欢谷歌,我发现每一个地方静态代码意味着硬代码字符串 您可以使用
Locale.getDefault().getDisplayLanguage();
您可以将语言设置为(对于英语)
编辑:
这是来自Android模拟器的CustomLocale应用程序的源代码
package com.android.customlocale;
导入android.app.ActivityManagerNative;
导入android.app.IActivityManager;
导入android.app.ListActivity;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.content.res.Configuration;
导入android.os.Bundle;
导入android.os.RemoteException;
导入android.util.Log;
导入android.view.ContextMenu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ContextMenu.ContextMenuInfo;
导入android.widget.Button;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.widget.AdapterView.AdapterContextMenuInfo;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.HashMap;
导入java.util.Locale;
导入java.util.Map;
/**
*显示系统区域设置的列表,并维护用户区域设置的自定义列表
*地区。用户可以选择并应用区域设置,也可以创建或删除区域设置
*自定义区域设置。
*/
公共类CustomLocaleActivity扩展了ListActivity{
私有静态最终字符串自定义_LOCALES_SEP=“”;
私有静态最终字符串CUSTOM\u LOCALES=“CUSTOM\u LOCALES”;
私有静态最终字符串键\u CUSTOM=“CUSTOM”;
私有静态最终字符串键\u NAME=“NAME”;
私有静态最终字符串键\u CODE=“CODE”;
私有静态最终字符串TAG=“LocaleSetup”;
私有静态最终布尔调试=true;
/**NewLocaleDialog活动完成时返回的请求代码*/
私有静态最终整数更新列表=42;
/**用于应用区域设置的菜单项id*/
专用静态最终整数菜单_APPLY=43;
/**用于删除自定义区域设置的菜单项id*/
专用静态最终整型菜单_REMOVE=44;
/**显示系统和自定义区域设置的列表视图*/
私有列表视图;
/**用于显示当前语言环境的文本视图*/
私有文本视图mCurrentLocaleTextView;
/**此活动的私人共享首选项*/
私人共享参考MPREF;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPrefs=getPreferences(模式\私有);
按钮newLocaleButton=(按钮)findViewById(R.id.new_locale);
newLocaleButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
Intent i=新Intent(CustomLocaleActivity.this,NewLocaleDialog.class);
startActivityForResult(i,更新列表);
}
});
mListView=(ListView)findViewById(android.R.id.list);
mListView.setFocusable(true);
mListView.setFocusableInTouchMode(true);
mListView.requestFocus();
registerForContextMenu(mListView);
setupLocaleList();
mCurrentLocaleTextView=(TextView)findviewbyd(R.id.current\u locale);
displayCurrentLocale();
}
@抑制警告(“未选中”)
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==UPDATE\u LIST&&resultCode==RESULT\u OK&&data!=null){
String locale=data.getExtras().getString(NewLocaleDialog.INTENT\u EXTRA\u locale);
if(locale!=null&&locale.length()>0){
//获取当前自定义区域设置列表
String customLocales=mPrefs.getString(CUSTOM_LOCALES,null);
//更新
if(customLocales==null){
customLocales=locale;
}否则{
customLocales+=CUSTOM_LOCALES_SEP+locale;
}
//保存首选项
如果(调试){
Log.d(标记“添加/自定义语言环境:”+customLocales);
}
mPrefs.edit().putString(自定义语言环境,自定义语言环境).commit();
Toast.makeText(这个,“添加了自定义区域设置:“+locale,Toast.LENGTH_SHORT).show();
//更新列表视图
setupLocaleList();
//找到要在列表视图中选择的项目
ListAdapter a=mListView.getAdapter();
对于(int i=0;iLocale.setDefault("en")
package com.android.customlocale;
import android.app.ActivityManagerNative;
import android.app.IActivityManager;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* Displays the list of system locales as well as maintain a custom list of user
* locales. The user can select a locale and apply it or it can create or remove
* a custom locale.
*/
public class CustomLocaleActivity extends ListActivity {
private static final String CUSTOM_LOCALES_SEP = " ";
private static final String CUSTOM_LOCALES = "custom_locales";
private static final String KEY_CUSTOM = "custom";
private static final String KEY_NAME = "name";
private static final String KEY_CODE = "code";
private static final String TAG = "LocaleSetup";
private static final boolean DEBUG = true;
/** Request code returned when the NewLocaleDialog activity finishes. */
private static final int UPDATE_LIST = 42;
/** Menu item id for applying a locale */
private static final int MENU_APPLY = 43;
/** Menu item id for removing a custom locale */
private static final int MENU_REMOVE = 44;
/** List view displaying system and custom locales. */
private ListView mListView;
/** Textview used to display current locale */
private TextView mCurrentLocaleTextView;
/** Private shared preferences of this activity. */
private SharedPreferences mPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPrefs = getPreferences(MODE_PRIVATE);
Button newLocaleButton = (Button) findViewById(R.id.new_locale);
newLocaleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(CustomLocaleActivity.this, NewLocaleDialog.class);
startActivityForResult(i, UPDATE_LIST);
}
});
mListView = (ListView) findViewById(android.R.id.list);
mListView.setFocusable(true);
mListView.setFocusableInTouchMode(true);
mListView.requestFocus();
registerForContextMenu(mListView);
setupLocaleList();
mCurrentLocaleTextView = (TextView) findViewById(R.id.current_locale);
displayCurrentLocale();
}
@SuppressWarnings("unchecked")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == UPDATE_LIST && resultCode == RESULT_OK && data != null) {
String locale = data.getExtras().getString(NewLocaleDialog.INTENT_EXTRA_LOCALE);
if (locale != null && locale.length() > 0) {
// Get current custom locale list
String customLocales = mPrefs.getString(CUSTOM_LOCALES, null);
// Update
if (customLocales == null) {
customLocales = locale;
} else {
customLocales += CUSTOM_LOCALES_SEP + locale;
}
// Save prefs
if (DEBUG) {
Log.d(TAG, "add/customLocales: " + customLocales);
}
mPrefs.edit().putString(CUSTOM_LOCALES, customLocales).commit();
Toast.makeText(this, "Added custom locale: " + locale, Toast.LENGTH_SHORT).show();
// Update list view
setupLocaleList();
// Find the item to select it in the list view
ListAdapter a = mListView.getAdapter();
for (int i = 0; i < a.getCount(); i++) {
Object o = a.getItem(i);
if (o instanceof Map<?, ?>) {
String code = ((Map<String, String>) o).get(KEY_CODE);
if (code != null && code.equals(locale)) {
mListView.setSelection(i);
break;
}
}
}
if (data.getExtras().getBoolean(NewLocaleDialog.INTENT_EXTRA_SELECT)) {
selectLocale(locale);
}
}
}
}
private void setupLocaleList() {
if (DEBUG) {
Log.d(TAG, "Update locate list");
}
ArrayList<Map<String, String>> data = new ArrayList<Map<String, String>>();
// Insert all system locales
String[] locales = getAssets().getLocales();
for (String locale : locales) {
Locale loc = new Locale(locale);
Map<String, String> map = new HashMap<String, String>(1);
map.put(KEY_CODE, locale);
map.put(KEY_NAME, loc.getDisplayName());
data.add(map);
}
locales = null;
// Insert all custom locales
String customLocales = mPrefs.getString(CUSTOM_LOCALES, "");
if (DEBUG) {
Log.d(TAG, "customLocales: " + customLocales);
}
for (String locale : customLocales.split(CUSTOM_LOCALES_SEP)) {
if (locale != null && locale.length() > 0) {
Locale loc = new Locale(locale);
Map<String, String> map = new HashMap<String, String>(1);
map.put(KEY_CODE, locale);
map.put(KEY_NAME, loc.getDisplayName() + " [Custom]");
// the presence of the "custom" key marks it as custom.
map.put(KEY_CUSTOM, "");
data.add(map);
}
}
// Sort all locales by code
Collections.sort(data, new Comparator<Map<String, String>>() {
public int compare(Map<String, String> lhs, Map<String, String> rhs) {
return lhs.get(KEY_CODE).compareTo(rhs.get(KEY_CODE));
}
});
// Update the list view adapter
mListView.setAdapter(new SimpleAdapter(this, data, R.layout.list_item, new String[] {
KEY_CODE, KEY_NAME}, new int[] {R.id.locale_code, R.id.locale_name}));
}
@SuppressWarnings("unchecked")
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (menuInfo instanceof AdapterContextMenuInfo) {
int position = ((AdapterContextMenuInfo) menuInfo).position;
Object o = mListView.getItemAtPosition(position);
if (o instanceof Map<?, ?>) {
String locale = ((Map<String, String>) o).get(KEY_CODE);
String custom = ((Map<String, String>) o).get(KEY_CUSTOM);
if (custom == null) {
menu.setHeaderTitle("System Locale");
menu.add(0, MENU_APPLY, 0, "Apply");
} else {
menu.setHeaderTitle("Custom Locale");
menu.add(0, MENU_APPLY, 0, "Apply");
menu.add(0, MENU_REMOVE, 0, "Remove");
}
}
}
}
@SuppressWarnings("unchecked")
@Override
public boolean onContextItemSelected(MenuItem item) {
String pendingLocale = null;
boolean is_custom = false;
ContextMenuInfo menuInfo = item.getMenuInfo();
if (menuInfo instanceof AdapterContextMenuInfo) {
int position = ((AdapterContextMenuInfo) menuInfo).position;
Object o = mListView.getItemAtPosition(position);
if (o instanceof Map<?, ?>) {
pendingLocale = ((Map<String, String>) o).get(KEY_CODE);
is_custom = ((Map<String, String>) o).get(KEY_CUSTOM) != null;
}
}
if (pendingLocale == null) {
// should never happen
return super.onContextItemSelected(item);
}
if (item.getItemId() == MENU_REMOVE) {
// Get current custom locale list
String customLocales = mPrefs.getString(CUSTOM_LOCALES, "");
if (DEBUG) {
Log.d(TAG, "Remove " + pendingLocale + " from custom locales: " + customLocales);
}
// Update
StringBuilder sb = new StringBuilder();
for (String locale : customLocales.split(CUSTOM_LOCALES_SEP)) {
if (locale != null && locale.length() > 0 && !locale.equals(pendingLocale)) {
if (sb.length() > 0) {
sb.append(CUSTOM_LOCALES_SEP);
}
sb.append(locale);
}
}
String newLocales = sb.toString();
if (!newLocales.equals(customLocales)) {
// Save prefs
mPrefs.edit().putString(CUSTOM_LOCALES, customLocales).commit();
Toast.makeText(this, "Removed custom locale: " + pendingLocale, Toast.LENGTH_SHORT)
.show();
}
} else if (item.getItemId() == MENU_APPLY) {
selectLocale(pendingLocale);
}
return super.onContextItemSelected(item);
}
private void selectLocale(String locale) {
if (DEBUG) {
Log.d(TAG, "Select locale " + locale);
}
try {
IActivityManager am = ActivityManagerNative.getDefault();
Configuration config = am.getConfiguration();
Locale loc = null;
String[] langCountry = locale.split("_");
if (langCountry.length == 2) {
loc = new Locale(langCountry[0], langCountry[1]);
} else {
loc = new Locale(locale);
}
config.locale = loc;
// indicate this isn't some passing default - the user wants this
// remembered
config.userSetLocale = true;
am.updateConfiguration(config);
Toast.makeText(this, "Select locale: " + locale, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
if (DEBUG) {
Log.e(TAG, "Select locale failed", e);
}
}
}
private void displayCurrentLocale() {
try {
IActivityManager am = ActivityManagerNative.getDefault();
Configuration config = am.getConfiguration();
if (config.locale != null) {
String text = String.format("%s - %s",
config.locale.toString(),
config.locale.getDisplayName());
mCurrentLocaleTextView.setText(text);
}
} catch (RemoteException e) {
if (DEBUG) {
Log.e(TAG, "get current locale failed", e);
}
}
}