首页 > 编程知识 正文

安卓开发语言有哪些,安卓开发主流什么语言

时间:2023-05-03 05:16:38 阅读:238448 作者:2435

1.监听系统语言切换广播

广播监听系统切换语言变换_xlhb的专栏-CSDN博客广播监听系统切换语言变换静态注册: android:name="com.ulucu.receiver.LOCALEReceiver" android:enabled="true"> public class LOCALEReceiver extends BroadcastReceiver {https://blog.csdn.net/lib739449500/article/details/50581115

1.application和activity代码中监听

Android 系统语言切换监听和设置_枫的专栏-CSDN博客_android监听系统语言切换最近项目上产品经理提了个需求,要求关闭语言国际化,不管手机系统设置那个国家的语言,都要显示汉语,好吧,既然有需求,那就做吧。但是项目中已经有英文的配置了,且是作为默认String提供的,这么多翻译好的文字,直接删除掉替换成中文为默认String又感觉弃之可惜。故网上Google下解决方案。就开始往下看吧。一、代码中动态设置应用显示语言(手动控制使用values-zh-rCN下字符串)这个方https://blog.csdn.net/q919233914/article/details/52237472

1.

切换语言回到前台 activity会重走生命周期,语言自动改变了

2.

为了不走生命周期

还是得每个Activity标签下都去设置

android:configChanges="locale|layoutDirection,

而且这种方式,无需在AndroidManifest中增加配置,我测试过,在Application中重写onConfigurationChanged,Activity的onConfigurationChanged

配置了manifest后生命周期不会重走了。但是也监听不到语言切换的广播了  需要重新手动设置

activity基类

@Overrideprotected void onCreate(Bundle savedInstanceState) { ParachuteLog.i(TAG, "onCreate: invoked!"); super.onCreate(savedInstanceState); mLanguage = getLanguage();

}

@Overridepublic void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); String newLanguage = getLanguage(newConfig.locale); if (!TextUtils.equals(newLanguage, mLanguage)) {//判断是语言变化 mLanguage = newLanguage; languageChangedUpdateUi(newLanguage); }} protected void languageChangedUpdateUi(String language) { mtitle.setText(R.string.title);}

public static String getLanguage() { Locale locale; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { locale = LocaleList.getDefault().get(0); } else { locale = Locale.getDefault(); } String language = locale.getLanguage() + "-" + locale.getCountry();//locale.getLanguage() 中文简体和繁体切换的时候,都是zh 所以分辨不出是切换了中文简体还是繁体,英文也是也有多国的,所以加上-和locale.getCountry()区分 return language;}

public static String getLanguage(Locale locale) { String language = ""; if (TextUtils.isEmpty(locale.getLanguage())) { language = "-"; } else if (TextUtils.isEmpty(locale.getCountry())) { language = locale.getLanguage() + "-"; } else { language = locale.getLanguage() + "-" + locale.getCountry(); } return language;}

3.不想设置基类,可以设置application

我们可以在 Application中重写onConfigurationChanged
@Override
public void onConfigurationChanged(Configuration newConfig) {
    Log.e("MyApplication","onConfigurationChanged");
    super.onConfigurationChanged(newConfig);
    SystemUtil.initAppLanguage(getBaseContext());
}

public static void initAppLanguage(Context context) {
    if (context == null) {
        return;
    }
    Locale.setDefault(Locale.CHINA);
    Configuration config = context.getResources().getConfiguration();
    config.locale = Locale.CHINA;//这个是固定成中文了
    context.getResources().updateConfiguration(config
            , context.getResources().getDisplayMetrics());
}
 

app内部语言切换

Android 国际化之多语言切换_xinyang_code的博客-CSDN博客前言android的国际化很简单,大家都知道创建对应国家编码的values文件夹就好啦。可是很多人不知道如何手动切换app的语言,而不是只能跟随系统语言变化,下面就介绍一下啦!直接贴代码吧/** * 设置当前语言 * * @param language */ private void setLanguage(String language) {https://blog.csdn.net/qq_36376387/article/details/73069347?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1.no_search_link

值得注意的是,繁体有台湾、香港和澳门的用getLanguage()方法获取的都是zh开头地区不同后面的后缀不一样

所以判断简体和繁体的时候,中文简体就是 zh-CN 全部转成小写判断  中文繁体全部是zh开头

英文全部是en开头 

另外Java层只区分简体和繁体的话只需要配置台湾繁体(values-zh-rTW)就行,切换到香港和澳门 app会自动读取台湾配置的繁体语言

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。