Android:国际化弹出框

news/2025/2/7 8:18:18/

3.13 风格与主题、国际化

1、应用国际化

应用国际化,通过修改系统语言,应用显示语言跟着改变。

选择Locale,点击>>符号。

创建多个国家,地区strings.xml文件,有一个默认strings.xml文件,各个stirngs.xml中<string>标签中保持一致。

示例:

创建t_language.xml文件,

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:text="@string/header"android:layout_width="match_parent"android:layout_height="wrap_content"></TextView></LinearLayout>

默认strings.xml文件:

<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">app name</string><string name="header">Local English</string></resources>

国际化中文:

<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">应用名</string><string name="header">简体中文</string></resources>

国际化繁体中文:

<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">應用名</string><string name="header">繁体中文</string></resources>

在Activity中直接更新语言,最好在执行setContentView方法之前。

//在Activity中设置语言//获取资源对象Resources resources=getResources();//获取设置对象Configuration configuration= resources.getConfiguration();//获取屏幕参数DisplayMetrics display=resources.getDisplayMetrics();//设置语言configuration.locale=Locale.CHINA;//configuration.locale=Locale.ENGLISH;//configuration.locale=Locale.TAIWAN;resources.updateConfiguration(configuration,display);

设置风格,将layout中重复的样式,整合成一种风格,直接设置。

在src/values/styles.xml中创建<style>

<style name="back"><item name="android:background">#FF003377</item></style>

在layout中设置style,设置style属性,将我们配置的style设置给组件。

<TextViewandroid:text="@string/header"android:layout_width="match_parent"android:layout_height="wrap_content"style="@style/back"></TextView>

2、风格与主题

设置应用主题

在src/values/styles.xml文件创建

<!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item></style>

修改配置文件AndroidManifest.xml

android:theme="@style/AppTheme"

3、弹出对话框

    使用AlertDialog.Builder类,建造者模式创建dialog。

示例1:

//创建open Dialog,public void openDialog(){//AlertDialog.Builder:构建弹窗AlertDialog.Builder builder=new AlertDialog.Builder(this);//设置titlebuilder.setTitle("提示");//设置提示信息builder.setMessage("是否退出");//设置确定按钮builder.setPositiveButton("是", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {//关闭AcitivityLanguageActivity.this.finish();//关闭dialog//dialog.dismiss();}});//设置取消按钮builder.setNegativeButton("否",null);//创建弹窗AlertDialog alertDialog=builder.create();//显示弹窗alertDialog.show();}

示例2:

带单选项的弹窗

//单选项弹窗public void openSingleChoiceDialog(){String[] choices=new String[]{"选项1","选项2","选项3"};//创建弹窗//setSingleChoiceItems方法,设置单选项,checkedItem:设置默认选择Item;AlertDialog.Builder builder= new AlertDialog.Builder(this).setTitle("单选").setSingleChoiceItems(choices, 1, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(mContext,"选择"+which,Toast.LENGTH_SHORT).show();}}).setPositiveButton("确认",null);AlertDialog alertDialog= builder.create();alertDialog.show();}

示例3:

带多选项的弹窗

//多选框弹窗public void openMultiChoiceDialog(){final String[] choices=new String[]{"选项1","选项2","选项3"};final boolean[] checkeds=new boolean[]{false,false,false};//创建弹窗//setSingleChoiceItems方法,设置单选项,checkedItem:设置默认选择Item;AlertDialog.Builder builder= new AlertDialog.Builder(this);builder.setTitle("多选");builder.setMultiChoiceItems(choices, checkeds, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {checkeds[which]=isChecked;}});builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {String text="";for (int i=0;i<choices.length;i++){if (checkeds[i]){text +=choices[i];}}Toast.makeText(mContext,text,Toast.LENGTH_SHORT).show();}});AlertDialog alertDialog= builder.create();alertDialog.show();}

http://www.ppmy.cn/news/1346238.html

相关文章

电子电器架构 —— 网关测试脚本分析

电子电器架构 —— 网关测试 我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师(Wechat:gongkenan2013)。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何 消耗你的人和事,多看一眼都是你的不对。非…

APIfox自动化编排场景(二)

测试流程控制条件 你可以在测试场景中新增流程控制条件&#xff08;循环、判断、等待、分组&#xff09;等。进一步满足了更复杂的测试场景/流程配置的使用&#xff0c;最终借助自动化测试功能解决复杂场景的测试工作。 分组​ 当测试流程中多个步骤存在相关联关系时&#xf…

【Java】ArrayList和LinkedList的区别是什么

目录 1. 数据结构 2. 性能特点 3. 源码分析 4. 代码演示 5. 细节和使用场景 ArrayList 和 LinkedList 分别代表了两类不同的数据结构&#xff1a;动态数组和链表。它们都实现了 Java 的 List 接口&#xff0c;但是有着各自独特的特点和性能表现。 1. 数据结构 ArrayList…

已解决:tpm2_createpriimay: command not found

出现错误如下&#xff1a; ERROR: Could not change hierarchy for Owner. TPM Error:0x9a2 ERROR: Could not change hierarchy for Endorsement. TPM Error:0x9a2 ERROR: Could not change hierarchy for Lockout. TPM Error:0x98e ERROR: Unable to run tpm2_takeownership…

【http】2、http request header Origin 属性、跨域 CORS、同源、nginx 反向代理、预检请求

文章目录 一、Origin 含义二、跨源资源共享&#xff1a;**Cross-Origin Resource Sharing** CORS2.1 跨域的定义2.2 功能概述2.3 场景示例2.3.1 简单请求2.3.2 Preflighted requests&#xff1a;预检请求 2.4 header2.4.1 http request header2.4.1.1 Origin2.4.1.2 Access-Con…

机器视觉系列之【硬件知识】-工业相机

目录 前言 常见面试题目 机器视觉能否代替裸眼检测 算法原理

【Linux系统学习】2.Linux基础命令

Linux基础命令 Linux的目录结构 Linux命令入门 目录切换相关命令(cd/pwd) 相对路径、绝对路径和特殊路径符 创建目录命令(mkdir) 文件操作命令part1(touch、cat、more&#xff09; 文件操作命令part2(cp、mv、rm&#xff09; 查找命令(which、find&#xff09; grep、wc和管道符…

vue-cli引入本地json数据:封装为js文件,无需请求直接读取

vue-cli引入本地json数据 1、新建js文件&#xff08;路径自定义&#xff09;&#xff0c;写入JSON数据 /* jsonData.js */export let jsonData { // 声明变量&#xff0c;存储数据// JSON源数据 }2、组件内引入js文件&#xff0c;读取数据 /* Example.vue */import { json…