单应用多语言切换(语言国际化)

news2024/10/6 16:22:33

目录

编写语言管理类

编写Activity 的父类

DEMO 实验界面--首页Activity

DEMO 实验界面--设置语言Activity

Demo 语言资源文件

 参考连接


编写语言管理类

package com.example.languageapplication

import android.content.Context
import android.content.ContextWrapper
import android.content.res.Configuration
import android.os.Build
import android.os.LocaleList
import android.util.Log
import java.util.Locale

class LanguageContextWrapper {
    companion object {
        const val TAG = "LanguageContextWrapper"
        //对应方式一
         var curAppLanguageNew:String? = null;
        //对应方式二
         var curAppLanguageLocaleNew:Locale? = null;


        /**
         * 方式一:设置字符串的方式
         * */
        fun wrapString(context: Context): Context {
            if(curAppLanguageNew == null){
                return context
            }
            val config: Configuration = context.resources.configuration
            val localeItem = Locale(curAppLanguageNew)
            val mContext = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocale(localeItem)
                val localeList = LocaleList(localeItem)
                LocaleList.setDefault(localeList)
                config.setLocales(localeList)
                context.createConfigurationContext(config)
            } else {
                config.setLocale(localeItem)
                context.createConfigurationContext(config)
            }
            return ContextWrapper(mContext)
        }

        /**
         * 方式二:设置Locale的方式
         * */
        fun wrapLocale(context: Context): Context {
            if(curAppLanguageLocaleNew == null){
                return context
            }
            val config: Configuration = context.resources.configuration
            val mContext = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocale(curAppLanguageLocaleNew)
                val localeList = LocaleList(curAppLanguageLocaleNew)
                LocaleList.setDefault(localeList)
                config.setLocales(localeList)
                context.createConfigurationContext(config)
            } else {
                config.setLocale(curAppLanguageLocaleNew)
                context.createConfigurationContext(config)
            }
            return ContextWrapper(mContext)
        }



        fun getCurLanguage(context: Context): String {
            var languageCode = "";
            var curLocale: Locale;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                context.resources;
                curLocale = context.resources.configuration.locales.get(0);
            } else {
                curLocale = context.resources.configuration.locale;
            }
            if (curLocale != null) {
                languageCode = curLocale.language;
            }
            Log.d(TAG, "当前语言:$languageCode")
            return languageCode;
        }
    }
}

下面用的是方式一

编写Activity 的父类

package com.example.languageapplication

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.os.PersistableBundle


open class BaseActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
    }
    override fun attachBaseContext(newBase: Context) {
        if(LanguageContextWrapper.curAppLanguageNew != null){
            super.attachBaseContext(LanguageContextWrapper.wrapString(newBase))
        }else{
            super.attachBaseContext(newBase);
        }

    }
}

DEMO 实验界面--首页Activity

package com.example.languageapplication

import android.content.Intent
import android.os.Bundle
import android.widget.Button

class MainActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.button_two_activity).setOnClickListener{
            val intent = Intent(this, TwoActivity::class.java)
            startActivity(intent)
        }

    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_two_activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/demo_start_text"
        tools:layout_editor_absoluteX="38dp"
        tools:layout_editor_absoluteY="91dp"
        tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

DEMO 实验界面--设置语言Activity

package com.example.languageapplication

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import java.util.Locale


class TwoActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_two)
        findViewById<Button>(R.id.button_switch).setOnClickListener {
            if(LanguageContextWrapper.getCurLanguage(TwoActivity@this) == Locale.ENGLISH.language){
                LanguageContextWrapper.curAppLanguageNew = Locale.CHINESE.language;
            }else {
                LanguageContextWrapper.curAppLanguageNew = Locale.ENGLISH.language;
            }
            val intent = Intent(this, MainActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            startActivity(intent)
        }
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TwoActivity">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/demo_btn_text"
        tools:layout_editor_absoluteX="34dp"
        tools:layout_editor_absoluteY="37dp"
        tools:ignore=",MissingConstraints" />

    <Button
        android:id="@+id/button_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/demo_switch_text"
        tools:layout_editor_absoluteX="38dp"
        tools:layout_editor_absoluteY="91dp"
        tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

Demo 语言资源文件

<resources>
    <string name="app_name">LanguageApplication</string>
    <string name="demo_switch_text">switch language</string>
    <string name="demo_start_text">Start the second Activity</string>
    <string name="demo_btn_text">english button</string>
</resources>
<resources>
    <string name="app_name">语言APP</string>
    <string name="demo_switch_text">切换语言</string>
    <string name="demo_start_text">启动第二个Activity</string>
    <string name="demo_btn_text">中文按钮</string>
</resources>

 参考连接

locale - Android context.getResources.updateConfiguration() deprecated - Stack Overflow

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1189020.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

code:-9907磁盘不足如何处理?帮你整理了几个必备的!

错误代码-9907表示磁盘空间不足。这意味着您的设备上的磁盘空间不足以完成当前的下载或存储任务。我们可以用这些方法解决这个问题。 一、对大文件进行压缩 可以使用压缩软件将一些文件进行压缩&#xff0c;以减少它们占用的磁盘空间。下面以嗨格式压缩大师作为操作示例。 1、…

URI 和 URL 的区别

URI包括URL和URN两个类别&#xff0c;URL是URI的子集&#xff0c;所以URL一定是URI&#xff0c;而URI不一定是URL URI Universal Resource Identifier 统一资源标志符&#xff0c;用来标识抽象或物理资源的一个紧凑字符串。 通过使用位置&#xff0c;名称或两者来标识Interne…

超好用!在线即可制作电子产品图册

​电子产品图册是展示产品特点、功能和外观的重要方式之一。通过图册&#xff0c;可以让客户更好地了解产品&#xff0c;增强信任感&#xff0c;从而促进销售。同时&#xff0c;对于企业来说&#xff0c;制作精美的电子产品图册也是展示企业文化和品牌形象的重要手段之一。 一、…

Matter学习笔记(2)——数据模型和设备类型

一、设备数据模型 Matter 中的设备具有明确定义的 数据模型(DM)&#xff0c;它是设备功能的分层建模。使用 属性(Attribute)、命令(Command) 和 事件(Event) 的概念描述 Matter 节点支持的远程操作&#xff0c;并分组为称为集群的逻辑块。Matter 应用集群规范中包含的集群具有…

提升绘图效率不再难,看看这8款AI流程图软件,一键快速生成流程图!

流程图是表示流程、系统和思想的重要视觉辅助工具。在当今数字时代&#xff0c;AI技术的出现已经彻底改变了制作流程图的方式。 在本文中&#xff0c;我们将与各位分享8款好用的AI流程图软件&#xff0c;借助每款软件内置的AI能力&#xff0c;可以快速绘制出一份完整的流程图&…

Java修仙传之神奇的ES2(巧妙的查询及结果处理篇)

SDL语句查询 查询的基本语法 GET /indexName/_search {"query": {"查询类型": {"查询条件": "条件值"}} } 根据文档id查询 #查询文档 GET hotel/_doc/36934 查询所有 会弹出该索引库下所有文档// 查询所有 GET /indexName/_searc…

乐园要吸引儿童还是家长?万达宝贝王2000万会员的求精之路

2023年6月&#xff0c;万达宝贝王正式迈入“400店时代”。 万达宝贝王在全国200多座城市&#xff0c;以游乐设施、主题活动、成长课程服务10亿多用户&#xff0c;拥有2000多万名会员&#xff0c;是真正的国内儿童乐园领跑者。 当流量时代变成“留量”时代&#xff0c;用户增长…

利用RoboBrowser库和爬虫代理实现微博视频的爬取

技术概述 微博是一个社交媒体平台&#xff0c;用户可以在上面发布和分享各种内容&#xff0c;包括文字、图片、音频和视频。微博视频是微博上的一种重要的内容形式&#xff0c;有时我们可能想要下载微博视频到本地&#xff0c;以便于观看或分析。但是&#xff0c;微博视频并没…

2023年阿里云腾讯云双11活动优惠券,阿里云最高省2400元,腾讯云最高省3600元

阿里云腾讯云2023年双11活动优惠券都已经出炉了&#xff0c;阿里云优惠券总额8940元&#xff0c;最高可省2400元&#xff0c;腾讯云优惠券总额9999元&#xff0c;最高可省3600元。阿里云和腾讯云的优惠券旨在帮助用户进一步减少上云成本&#xff0c;推荐大家先领券后购买。 一…

提升服务器性能相关

目录 查看是否开启超线程 显示所有逻辑 CPU 及其 HT 对关系 查看NUMA Isolcpus 隔离 禁止使用中断均衡服务 设置线程名称 设置线程亲和性 taskset工具 使用代码绑定 绑定core需要注意以下几点 查看是否开启超线程 lscpu | grep Thread 显示所有逻辑 CPU 及其 HT 对关…

功能更新|Leangoo领歌免费敏捷工具支持SAFe大规模敏捷框架

Leangoo领歌是一款永久免费的专业的敏捷开发管理工具&#xff0c;提供端到端敏捷研发管理解决方案&#xff0c;涵盖敏捷需求管理、任务协同、进展跟踪、统计度量等。 
 Leangoo可以支持敏捷研发管理全流程&#xff0c;包括小型团队敏捷开发&#xff0c;规模化敏捷SAFe&#xf…

umi4 React项目使用icon集合

umi项目中使用icon集合。 icon集合&#xff1a;https://icones.js.org/ 测试使用这个ion .umirc.ts文件 icons:{autoInstall:{iconify-json/ion: true,//自动安装iconify-json/ion},include: [ion:social-windows-outline]&#xff0c;//要使用的必须把icon类名加到include中…

数据集笔记:Telecom Shanghai Dataset

0 数据地址 &#x1f4f1;Telecom Shanghai Dataset (kaggle.com) 1 数据描述 该数据集由上海电信提供&#xff0c;包含超过720万条记录&#xff0c;记录了9481部手机通过3233个基站访问互联网的情况&#xff0c;时间跨度为六个月。例如&#xff0c;下图显示了基站的分布情况…

Primavera P6 Calendars 全局日历和项目日历Calendar

Primavera P6 日历基本介绍 Primavera P6 日历特性和功能是项目和进度管理的强大工具。在这里我们将讨论 Primavera P6 中三种最常见的日历类型中的两种&#xff1a;全局日历和项目日历。 Primavera P6 日历的第三种类型是资源日历&#xff0c;它非常专业&#xff0c;在项目中…

数字时代的法律前沿:TikTok与政府监管的博弈

在当今数字化时代&#xff0c;社交媒体已经成为了人们生活的一部分&#xff0c;扮演着沟通、娱乐和信息获取的重要角色。TikTok&#xff0c;作为一款短视频分享应用&#xff0c;在全球范围内迅速崭露头角&#xff0c;吸引了数亿用户。 然而&#xff0c;随着TikTok的崛起&#…

【C++】单例模式【两种实现方式】

目录 一、了解单例模式前的基础题 1、设计一个类&#xff0c;不能被拷贝 2、设计一个类&#xff0c;只能在堆上创建对象 3、设计一个类&#xff0c;只能在栈上创建对象 4、设计一个类&#xff0c;不能被继承 二、单例模式 1、单例模式的概念 2、单例模式的两种实现方式 …

低代码平台,业务开发的“银弹”

目录 一、为什么需要低代码平台 二、低代码平台的搭建能力 三、低代码其他能力 四、写在最后 随着互联网和信息技术的快速发展&#xff0c;各行各业都在积极拥抱数字化转型。在这个过程中&#xff0c;软件开发成为企业实现数字化转型的关键环节。然而&#xff0c;传统的软件开发…

交流信号继电器 DX-31BJ/AC220V JOSEF约瑟 电压启动 面板嵌入式安装

DX系列信号继电器由矩形脉冲激磁&#xff0c;磁钢保持。本继电器为双绕组。工作线圈可为电压型&#xff0c;亦可为电流型。复归线圈为电压型。继电器的工作电流或工作电压为长脉冲&#xff0c;亦可为脉冲不小于20mS的短脉冲。 系列型号 DX-31B信号继电器DX-31BJ信号继电器 D…

【VS2019 Qt5 VTK9.2】临时解决配置相关问题的简单方法

配置报错 编译报错提示&#xff08;LNK2019或LNK2001&#xff09; 严重性 代码 说明 项目 文件 行 禁止显示状态 错误 LNK2019 无法解析的外部符号 “__declspec(dllimport) public: __cdecl QVTKOpenGLNativeWidget::QVTKOpenGLNativeWidget(class QWidget *,class QFlags)(_i…

华为gre over ipsec配置案例

除了物理口加入安全域zone外&#xff0c;tunnel也得加入到安全域 一定记得tunnel也得加入zone&#xff0c;这个总爱忘记。 [fw1]firewall zone dmz [fw1-zone-dmz]add interface Tunnel 1 [fw2]firewall zone dmz [fw2-zone-dmz]add interface Tunnel 1