基础复习——为activity补充活动信息——利用资源文件配置字符串——利用元数据传递配置信息——给页面注册快捷方式

 

 

 

 

 

res\values\strings.xml可用来配置字符串形式的参数。

 

配置的字符串参数例子如下:<string name="weather_str">晴天</string>

 

 

在活动页面的Java代码中,调用getString方法即可根据“R.string.参数名称”获得指定参数的字符串值。

 

获取代码示例如下:




private void showStringResource()                     // 显示字符串资源

{

             String value = getString(R.string.weather_str);               // 从strings.xml获取名叫weather_str的字符串值

            tv_resource.setText("来自字符串资源:今天的天气是"+value);       // 在文本视图上显示文字
}

 

 

 

 

 

 

 

 

 

 

strings.xml

 

<resources>
    <string name="app_name">My Application</string>
    <string name="hello">中国,您好</string>
    <string name="weather_str">今天,温度高</string>
</resources>

 

 

 

 

 

 

布局:

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_resource"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp" />

</LinearLayout>
复制代码

 

 

 

 

 

 

 

 

代码:

复制代码
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ReadStringActivity extends AppCompatActivity
{
    private TextView tv_resource;    // 声明一个文本视图对象

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_string);



        tv_resource = findViewById(R.id.tv_resource);  // 从布局文件中获取名叫tv_resource的文本视图

        showStringResource(); // 显示字符串资源
    }

    // 显示字符串资源
    private void showStringResource()
    {
        String value = getString(R.string.weather_str); // 从strings.xml获取名叫weather_str的字符串值

        tv_resource.setText("来自字符串资源:今天的天气是:    "+value); // 在文本视图上显示文字
    }
}
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

============================================================================================

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

元数据是一种描述其他数据的数据,它相当于描述固定活动的参数信息。

 


在activity节点内部添加meta-data标签,通过属性name指定元数据的名称,通过属性value指定元数据的值。

 

示例如下:


<activity android:name=".MetaDataActivity">
          <meta-data android:name="weather" android:value="晴天" />
</activity>

 

 

 

 


也可引用strings.xml已定义的字符串资源,举例如下:


<activity android:name=".MetaDataActivity">
           <meta-data android:name="weather" android:value="@string/weather_str" />
</activity>

 

 

 

 

 

 

 

 

 

 

 

 

 

在Java代码中,获取元数据信息的步骤分为下列三步:


(1)调用getPackageManager方法获得当前应用的包管理器;


(2)调用包管理器的getActivityInfo方法获得当前活动的信息对象;


(3)活动信息对象的metaData是Bundle包裹类型,调用包裹对象的getString即可获得指定名称的参数值;

 

 

 

 

 

 

 

布局:

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_meta"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp" />

</LinearLayout>
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

代码:

复制代码
package com.example.myapplication;

import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MetaDataActivity extends AppCompatActivity
{
    private TextView tv_meta; // 声明一个文本视图对象

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meta_data);

        // 从布局文件中获取名叫tv_meta的文本视图
        tv_meta = findViewById(R.id.tv_meta);

        showMetaData(); // 显示配置的元数据
    }

    // 显示配置的元数据
    private void showMetaData()
    {
        try
        {
            PackageManager pm = getPackageManager(); // 获取应用包管理器

            // 从应用包管理器中获取当前的活动信息
            ActivityInfo act = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);

            Bundle bundle = act.metaData; // 获取活动附加的元数据信息

            String value = bundle.getString("weather"); // 从包裹中取出名叫weather的字符串

            tv_meta.setText("来自元数据信息:今天的天气是:    "+value); // 在文本视图上显示文字
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
复制代码

 

 

 

 

 

 

 

 

 

 

strings.xml

<resources>
    <string name="app_name">My Application</string>
    <string name="hello">中国,您好</string>
    <string name="weather_str">今天,温度高</string>
</resources>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity android:name=".MetaDataActivity">
            <!-- <meta-data android:name="weather" android:value="晴天" /> -->
            <meta-data
                android:name="weather"
                android:value="@string/weather_str" />
        </activity>
        <activity
            android:name=".ReadStringActivity"
            android:exported="false" />
        <activity
            android:name=".ActionUriActivity"
            android:exported="false" />
        <activity
            android:name=".ActResponseActivity"
            android:exported="false" />
        <activity
            android:name=".ActRequestActivity"
            android:exported="false" />
        <activity
            android:name=".ActReceiveActivity"
            android:exported="false" />
        <activity
            android:name=".ActSendActivity"
            android:exported="false" />
        <activity
            android:name=".LoginSuccessActivity"
            android:exported="false" />
        <activity
            android:name=".LoginInputActivity"
            android:exported="false" />
        <activity
            android:name=".ActNextActivity"
            android:exported="false" />
        <activity
            android:name=".NextActivity"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
复制代码

 

 

 

 

 

 

 

 

另外:

 

 

 

 

 

 

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity android:name=".MetaDataActivity">
             <meta-data android:name="weather" android:value="晴天" />
<!--            <meta-data-->
<!--                android:name="weather"-->
<!--                android:value="@string/weather_str" />-->
        </activity>
        <activity
            android:name=".ReadStringActivity"
            android:exported="false" />
        <activity
            android:name=".ActionUriActivity"
            android:exported="false" />
        <activity
            android:name=".ActResponseActivity"
            android:exported="false" />
        <activity
            android:name=".ActRequestActivity"
            android:exported="false" />
        <activity
            android:name=".ActReceiveActivity"
            android:exported="false" />
        <activity
            android:name=".ActSendActivity"
            android:exported="false" />
        <activity
            android:name=".LoginSuccessActivity"
            android:exported="false" />
        <activity
            android:name=".LoginInputActivity"
            android:exported="false" />
        <activity
            android:name=".ActNextActivity"
            android:exported="false" />
        <activity
            android:name=".NextActivity"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

========================================================================================

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

元数据的meta-data标签除了前面说到的name属性和value属性,还拥有resource属性,该属性可指定一个XML文件,表示元数据想要的复杂信息保存于XML数据之中。

 

 

利用元数据配置快捷菜单的步骤如下所示:

 

 


(1)在res/values/strings.xml添加各个菜单项名称的字符串配置

 

 


(2)创建res/xml/shortcuts.xml,在该文件中填入各组菜单项的快捷方式定义(每个菜单对应哪个活动页面)。

 

 


(3)给activity节点注册元数据的快捷菜单配置,举例如下:


          <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

复制代码
完整的activity节点配置示例如下:


<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- 指定快捷方式。在桌面上长按应用图标,就会弹出shortcuts所描述的快捷菜单 --> <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" /> </activity>
复制代码

 

 

 

 

 

 

 

 

 

 

布局:

 

shortcuts.xml

 

 

复制代码
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="first"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/first_short"
        android:shortcutLongLabel="@string/first_long">
        <!-- targetClass指定了点击该项菜单后要打开哪个活动页面 -->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.myapplication"
            android:targetClass="com.example.myapplication.ActSendActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>

    <shortcut
        android:shortcutId="second"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/second_short"
        android:shortcutLongLabel="@string/second_long">
        <!-- targetClass指定了点击该项菜单后要打开哪个活动页面 -->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.myapplication"
            android:targetClass="com.example.myapplication.ActionUriActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>

    <shortcut
        android:shortcutId="third"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/third_short"
        android:shortcutLongLabel="@string/third_long">
        <!-- targetClass指定了点击该项菜单后要打开哪个活动页面 -->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.myapplication"
            android:targetClass="com.example.myapplication.LoginInputActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>
</shortcuts>
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

strings.xml

 

复制代码
<resources>
    <string name="app_name">My Application</string>
    <string name="hello">中国,您好</string>
    <string name="weather_str">今天,温度高</string>
    <string name="first_short">first</string>
    <string name="first_long">启停活动</string>
    <string name="second_short">second</string>
    <string name="second_long">来回跳转</string>
    <string name="third_short">third</string>
    <string name="third_long">登录返回</string>
</resources>
复制代码

 

 

 

 

 

 

 

 

 

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity android:name=".MetaDataActivity">
             <meta-data android:name="weather" android:value="晴天" />
<!--            <meta-data-->
<!--                android:name="weather"-->
<!--                android:value="@string/weather_str" />-->
        </activity>
        <activity
            android:name=".ReadStringActivity"
            android:exported="false" />
        <activity
            android:name=".ActionUriActivity"
            android:exported="false" />
        <activity
            android:name=".ActResponseActivity"
            android:exported="false" />
        <activity
            android:name=".ActRequestActivity"
            android:exported="false" />
        <activity
            android:name=".ActReceiveActivity"
            android:exported="false" />
        <activity
            android:name=".ActSendActivity"
            android:exported="false" />
        <activity
            android:name=".LoginSuccessActivity"
            android:exported="false" />
        <activity
            android:name=".LoginInputActivity"
            android:exported="false" />
        <activity
            android:name=".ActNextActivity"
            android:exported="false" />
        <activity
            android:name=".NextActivity"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!-- 指定快捷方式。在桌面上长按应用图标,就会弹出@xml/shortcuts所描述的快捷菜单 -->
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />
        </activity>
    </application>

</manifest>
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

PS:示例

 

 

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chapter04">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!-- 指定快捷方式。在桌面上长按应用图标,就会弹出@xml/shortcuts所描述的快捷菜单 -->
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />
        </activity>
        <activity android:name=".ActStartActivity" />
        <activity android:name=".ActFinishActivity" />
        <activity android:name=".ActLifeActivity" />
        <activity android:name=".ActNextActivity" />
        <activity
            android:name=".JumpFirstActivity"
            android:launchMode="standard" />
        <activity android:name=".JumpSecondActivity" />
        <activity android:name=".LoginInputActivity" />
        <activity android:name=".LoginSuccessActivity" />
        <activity android:name=".ActionUriActivity" />
        <activity android:name=".ActSendActivity" />
        <activity android:name=".ActReceiveActivity" />
        <activity android:name=".ActRequestActivity" />
        <activity android:name=".ActResponseActivity" />
        <activity android:name=".ReadStringActivity" />
        <activity android:name=".MetaDataActivity">

            <!-- <meta-data android:name="weather" android:value="晴天" /> -->
            <meta-data
                android:name="weather"
                android:value="@string/weather_str" />
        </activity>
    </application>

</manifest>
复制代码

 

posted @   小白龙白龙马  阅读(70)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2021-08-01 测试开发进阶——spring boot——MVC——HttpServletRespon设置返回header、返回cookie、返回体
2021-08-01 测试开发进阶——spring boot——MVC——get访问——post访问——方法
2021-08-01 测试开发进阶——spring boot——MVC——post访问——使用@RequestParam获取参数
2021-08-01 测试开发进阶——spring boot——MVC——post访问——通过Bean对象来获取前端页面参数
2021-08-01 测试开发进阶——spring boot——MVC——post访问——通过HttpServletRequest对象获取请求参数
2021-08-01 测试开发进阶——spring boot——MVC——post访问——无注解下获取参数
2021-08-01 测试开发进阶——spring boot——MVC——get访问——通过Bean对象来获取前端页面参数
点击右上角即可分享
微信分享提示