使用RadioButton Android设置语言环境

时间:2018-10-25 05:32:23

标签: android radio-button locale radio-group

我正在尝试使用语言环境在App中更改语言。我在radioButton中有两个RadioGroup。当我尝试更改语言时,setOnCheckedChangeListener继续保持通话状态。如果我删除了区域设置代码,就可以正常工作

 Locale locale;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.toggle);


    Toast.makeText(this, ""+getString(R.string.app_name), Toast.LENGTH_SHORT).show();

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton rb = (RadioButton) group.findViewById(checkedId);
            Toast.makeText(MainActivity.this, "Inside", Toast.LENGTH_SHORT).show();
            if (rb!=null){
                switch(checkedId) {
                    case R.id.rdEnglish:
                        // do operations specific to this selection
                        locale = new Locale("en");
                        rb.setChecked(true);
                        setLocale();
                        break;
                    case R.id.rdTamil:
                        // do operations specific to this selection
                        locale = new Locale("ta");
                        setLocale();
                        break;

                    default:
                        locale = new Locale("en");
                         setLocale();
                }

            }
        }
    });

}



public void setLocale() {

    Locale.setDefault(locale);
    Configuration configuration = new Configuration();
    configuration.locale = locale;
    getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
    recreate();
}

}

2 个答案:

答案 0 :(得分:0)

rb.setChecked(true);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());

Intent i = new Intent(this, MainActivity.class);
startActivity(i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | 
Intent.FLAG_ACTIVITY_NEW_TASK));

您正在onCheckedChanged中更改配置,这就是为什么它一次又一次调用的原因。将上面提到的代码放在单独的方法中,并在中断之前在大小写内调用该方法。在这种情况下,它将起作用。

答案 1 :(得分:0)

更好地使用Helper类

android{
 packagingOptions {

        exclude 'lib/arm64-v8a/libclasifier_jni.so'
        exclude 'lib/armeabi/libclasifier_jni.so'
        exclude 'lib/armeabi-v7a/libclasifier_jni.so'
        exclude 'lib/mips/libclasifier_jni.so'
        exclude 'lib/mips64/libclasifier_jni.so'
        exclude 'lib/x86/libclasifier_jni.so'
        exclude 'lib/x86_64/libclasifier_jni.so'

    }
}

单击单选按钮时

public class LocaleHelper {

    private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

    public static Context onAttach(Context context) {
        String lang = getPersistedData(context, Locale.getDefault().getLanguage());
        return setLocale(context, lang);
    }

    public static Context onAttach(Context context, String defaultLanguage) {
        String lang = getPersistedData(context, defaultLanguage);
        return setLocale(context, lang);
    }

    public static String getLanguage(Context context) {
        return getPersistedData(context, Locale.getDefault().getLanguage());
    }

    public static Context setLocale(Context context, String language) {
        persist(context, language);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, language);
        }

        return updateResourcesLegacy(context, language);
    }

    private static String getPersistedData(Context context, String defaultLanguage) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
    }

    private static void persist(Context context, String language) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putString(SELECTED_LANGUAGE, language);
        editor.apply();
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Configuration configuration = context.getResources().getConfiguration();
        configuration.setLocale(locale);
        configuration.setLayoutDirection(locale);

        return context.createConfigurationContext(configuration);
    }

    @SuppressWarnings("deprecation")
    private static Context updateResourcesLegacy(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLayoutDirection(locale);
        }

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());

        return context;
    }
}

它将管理LocaleHelper.setLocale(HomeActivity.this, "en"); 保持连续通话