下面列出了怎么用android.os.LocaleList的API类实例代码及写法,或者点击链接到github查看源代码。
@RequiresApi(api = 24)
public static void changeAppLanguage(Context context, Locale setLocale) {
Resources resources = context.getApplicationContext().getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
Locale locale = getLocaleByLanguage(setLocale);
config.locale = locale;
if (VERSION.SDK_INT >= 24) {
LocaleList localeList = new LocaleList(new Locale[]{locale});
LocaleList.setDefault(localeList);
config.setLocales(localeList);
context.getApplicationContext().createConfigurationContext(config);
Locale.setDefault(locale);
}
resources.updateConfiguration(config, dm);
}
public static void setLocale(@NonNull Context context) {
Locale locale = Preferences.get(context).getCurrentLocale();
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList.setDefault(new LocaleList(locale));
configuration.setLocales(new LocaleList(locale));
configuration.setLocale(locale);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
} else {
configuration.locale = locale;
}
//Todo:
// Find out a solution to use context.createConfigurationContext(configuration);
// It breaks onConfigurationChanged()
// Still can't find a way to fix that
// No other options, better use deprecated code for now
context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
}
/**
* 设置应用默认语言
*/
public void setApplicationLanguage() {
Resources resources = getApplicationContext().getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
Locale locale = getLanguageLocale();
config.locale = locale;
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocales(localeList);
getApplicationContext().createConfigurationContext(config);
}
resources.updateConfiguration(config, dm);
}
/**
* Used to package this object into a {@link Parcel}.
*
* @param dest The {@link Parcel} to be written.
* @param flags The flags used for parceling.
*/
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(inputType);
dest.writeInt(imeOptions);
dest.writeString(privateImeOptions);
TextUtils.writeToParcel(actionLabel, dest, flags);
dest.writeInt(actionId);
dest.writeInt(initialSelStart);
dest.writeInt(initialSelEnd);
dest.writeInt(initialCapsMode);
TextUtils.writeToParcel(hintText, dest, flags);
TextUtils.writeToParcel(label, dest, flags);
dest.writeString(packageName);
dest.writeInt(fieldId);
dest.writeString(fieldName);
dest.writeBundle(extras);
if (hintLocales != null) {
hintLocales.writeToParcel(dest, flags);
} else {
LocaleList.getEmptyLocaleList().writeToParcel(dest, flags);
}
dest.writeStringArray(contentMimeTypes);
}
public EditorInfo createFromParcel(Parcel source) {
EditorInfo res = new EditorInfo();
res.inputType = source.readInt();
res.imeOptions = source.readInt();
res.privateImeOptions = source.readString();
res.actionLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
res.actionId = source.readInt();
res.initialSelStart = source.readInt();
res.initialSelEnd = source.readInt();
res.initialCapsMode = source.readInt();
res.hintText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
res.label = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
res.packageName = source.readString();
res.fieldId = source.readInt();
res.fieldName = source.readString();
res.extras = source.readBundle();
LocaleList hintLocales = LocaleList.CREATOR.createFromParcel(source);
res.hintLocales = hintLocales.isEmpty() ? null : hintLocales;
res.contentMimeTypes = source.readStringArray();
return res;
}
private TextClassifierImplNative getNative(LocaleList localeList)
throws FileNotFoundException {
synchronized (mLock) {
localeList = localeList == null ? LocaleList.getEmptyLocaleList() : localeList;
final ModelFile bestModel = findBestModelLocked(localeList);
if (bestModel == null) {
throw new FileNotFoundException("No model for " + localeList.toLanguageTags());
}
if (mNative == null || mNative.isClosed() || !Objects.equals(mModel, bestModel)) {
Log.d(DEFAULT_LOG_TAG, "Loading " + bestModel);
final ParcelFileDescriptor fd = ParcelFileDescriptor.open(
new File(bestModel.getPath()), ParcelFileDescriptor.MODE_READ_ONLY);
mNative = new TextClassifierImplNative(fd.getFd());
mNativeCleaner = Cleaner.create(this, new NativeCloser(mNative));
closeAndLogError(fd);
mModel = bestModel;
}
return mNative;
}
}
private static void initLocale(Context c) {
Crashlytics.setString("lang", Preferences.LANGUAGE.get());
Crashlytics.setString("digits", Preferences.DIGITS.get());
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = getLocales();
LocaleList.setDefault(localeList);
config.setLocales(localeList);
} else {
Locale locale = getLocale();
config.setLocale(locale);
Locale.setDefault(locale);
}
c.getResources().updateConfiguration(config, c.getResources().getDisplayMetrics());
c.getApplicationContext().getResources().updateConfiguration(config, c.getResources().getDisplayMetrics());
}
public static void changeLocalLanguage(Context context, Locale locale) {
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//8.0及以上系统
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocale(locale);
config.setLocales(localeList);
context.createConfigurationContext(config);//对于8.0系统必须先调用该语句,否则后面的updateConfiguration不起作用
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4及以上系统
config.setLocale(locale);
} else {
config.locale = locale;
}
resources.updateConfiguration(config, dm);//所有的系统都需要调用该API
}
@SuppressWarnings("PMD.AvoidReassigningParameters")
@NonNull
private static Context applyNewLocaleConfig(@NonNull Context context, @NonNull final Locale newLocale) {
final Configuration newLocaleConfig = context.getResources().getConfiguration();
final Resources res = context.getResources();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
final LocaleList localeList = new LocaleList(newLocale);
newLocaleConfig.setLocales(localeList);
newLocaleConfig.setLocale(newLocale);
context = context.createConfigurationContext(newLocaleConfig);
} else {
newLocaleConfig.locale = newLocale;
}
Locale.setDefault(newLocale);
res.updateConfiguration(newLocaleConfig, res.getDisplayMetrics());
return context;
}
public static Context setLanguage(Context context, Locale locale) {
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
config.setLocale(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocales(localeList);
return context.createConfigurationContext(config);
} else {
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
context.getResources().updateConfiguration(config, displayMetrics);
return context;
}
}
public static Context wrapContext(Context context) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(getLocale());
LocaleList localeList = getLocales();
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else {
configuration.setLocale(getLocale());
context = context.createConfigurationContext(configuration);
}
return new ContextWrapper(context);
}
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context, Locale newLocale)
{
final Resources res = context.getResources();
final Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
}
else
{
configuration.locale = newLocale;
res.updateConfiguration(configuration, res.getDisplayMetrics());
}
return new ContextWrapper(context);
}
public static ContextLocalWrapper wrap(Context context, Locale newLocale) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
} else {
configuration.locale = newLocale;
res.updateConfiguration(configuration, res.getDisplayMetrics());
}
return new ContextLocalWrapper(context);
}
/**
* 设置语言类型
*/
public static void setApplicationLanguage(Context context) {
Resources resources = context.getApplicationContext().getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
Locale locale = getSetLanguageLocale(context);
config.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocales(localeList);
context.getApplicationContext().createConfigurationContext(config);
Locale.setDefault(locale);
}
resources.updateConfiguration(config, dm);
}
private WritableArray getLocaleList() {
WritableArray array = Arguments.createArray();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList locales = getReactApplicationContext()
.getResources().getConfiguration().getLocales();
for (int i = 0; i < locales.size(); i++) {
array.pushString(this.toLanguageTag(locales.get(i)));
}
} else {
array.pushString(this.toLanguageTag(getReactApplicationContext()
.getResources().getConfiguration().locale));
}
return array;
}
public static ContextWrapper wrap(Context context, Locale newLocale) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
} else {
configuration.locale = newLocale;
res.updateConfiguration(configuration, res.getDisplayMetrics());
}
return new ContextWrapper(context);
}
public static Context wrap(Context context, Locale locale) {
Context newContext = context;
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//8.0及以上系统
config.setLocale(locale);
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocales(localeList);
newContext = context.createConfigurationContext(config);//对于8.0系统必须先调用该语句,否则后面的updateConfiguration不起作用
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4及以上系统
config.setLocale(locale);
newContext = context.createConfigurationContext(config);
} else {
config.locale = locale;
}
return new QuickNoteContextWrapper(newContext);
}
private void setDefaultKeyboard() {
KeyboardInterface keyboard = getKeyboardForLocale(SettingsStore.getInstance(getContext()).getKeyboardLocale());
if (keyboard != null) {
handleLanguageChange(keyboard);
return;
}
// If the user has not selected any keyboard, find the best match from system locales.
LocaleList localeList = getResources().getConfiguration().getLocales();
String[] supportedLocales = new String[mKeyboards.size()];
for (int i = 0; i < mKeyboards.size(); ++i) {
supportedLocales[i] = mKeyboards.get(i).getLocale().toLanguageTag();
}
Locale bestMatch = localeList.getFirstMatch(supportedLocales);
keyboard = getKeyboardForLocale(bestMatch);
if (keyboard == null) {
// Fall back to english.
keyboard = getKeyboardForLocale(Locale.ENGLISH);
}
handleLanguageChange(keyboard);
}
/**
* @see android.text.style.LocaleSpan#LocaleSpan(LocaleList)
*/
@RequiresApi(api = Build.VERSION_CODES.N)
public static Span locale(@NonNull final LocaleList localeList) {
return new Span(new SpanBuilder() {
@Override
public Object build() {
return new LocaleSpan(localeList);
}
});
}
public static Locale getPrimaryHintLocale(final EditorInfo editorInfo) {
if (editorInfo == null) {
return null;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = editorInfo.hintLocales;
if (localeList != null && !localeList.isEmpty())
return localeList.get(0);
}
return null;
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context) {
Resources resources = context.getResources();
Locale locale = LauncherApplication.getInstance().getLanguageLocale();
Configuration configuration = resources.getConfiguration();
configuration.setLocale(locale);
configuration.setLocales(new LocaleList(locale));
return context.createConfigurationContext(configuration);
}
public static ContextWrapper wrap(Context context, Locale newLocale) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
}
return new ContextWrapper(context);
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context) {
Resources resources = context.getResources();
Locale locale = getLocaleByLanguage(context);
Configuration configuration = resources.getConfiguration();
configuration.setLocale(locale);
configuration.setLocales(new LocaleList(locale));
return context.createConfigurationContext(configuration);
}
private TimeZone getTimeZone() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList locales = context.getResources().getConfiguration().getLocales();
if (!locales.isEmpty()) {
Locale locale = locales.get(0);
return Calendar.getInstance(locale).getTimeZone();
}
}
return Calendar.getInstance().getTimeZone();
}
/**
* Converts LocaleList object to the comma separated BCP 47 compliant string format.
*
* @return a well-formed IETF BCP 47 language tag with language and country code that
* represents this locale list.
*/
@TargetApi(Build.VERSION_CODES.N)
public static String toLanguageTags(LocaleList localeList) {
ArrayList<String> newLocaleList = new ArrayList<>();
for (int i = 0; i < localeList.size(); i++) {
Locale locale = getUpdatedLocaleForChromium(localeList.get(i));
newLocaleList.add(toLanguageTag(locale));
}
return TextUtils.join(",", newLocaleList);
}
@NonNull
private static LocaleList getLocalesFromLanguageTags(String languageTags) {
if (TextUtils.isEmpty(languageTags)) {
return LocaleList.getEmptyLocaleList();
}
return LocaleList.forLanguageTags(languageTags.replace('|', ','));
}
private Request(
CharSequence text,
LocaleList defaultLocales,
TextClassifier.EntityConfig entityConfig,
boolean legacyFallback,
String callingPackageName) {
mText = text;
mDefaultLocales = defaultLocales;
mEntityConfig = entityConfig;
mLegacyFallback = legacyFallback;
mCallingPackageName = callingPackageName;
}
private Locale getLocale() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList locales = getConfiguration().getLocales();
if (locales.size() > 0) {
return locales.get(0);
}
}
return getConfiguration().locale;
}
private Request(Parcel in) {
mText = in.readString();
mDefaultLocales = in.readInt() == 0 ? null : LocaleList.CREATOR.createFromParcel(in);
mEntityConfig = in.readInt() == 0
? null : TextClassifier.EntityConfig.CREATOR.createFromParcel(in);
mLegacyFallback = true;
mCallingPackageName = in.readString();
}
public Locale getSysLocale() {
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else {
locale = Locale.getDefault();
}
return locale;
}