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


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

    配置的字符串参数例子如下:晴天

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

    获取代码示例如下:

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

    {             String value = getString(R.string.weather_str);               // 从strings.xml获取名叫weather_str的字符串值            tv_resource.setText("来自字符串资源:今天的天气是"+value);       // 在文本视图上显示文字 }

    strings.xml

    1. <resources>
    2. <string name="app_name">My Applicationstring>
    3. <string name="hello">中国,您好string>
    4. <string name="weather_str">今天,温度高string>
    5. resources>

    布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical">
    5. <TextView
    6. android:id="@+id/tv_resource"
    7. android:layout_width="match_parent"
    8. android:layout_height="wrap_content"
    9. android:padding="5dp"
    10. android:gravity="center"
    11. android:textColor="#000000"
    12. android:textSize="17sp" />
    13. LinearLayout>

    代码:

    1. package com.example.myapplication;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.widget.TextView;
    5. public class ReadStringActivity extends AppCompatActivity
    6. {
    7. private TextView tv_resource; // 声明一个文本视图对象
    8. @Override
    9. protected void onCreate(Bundle savedInstanceState)
    10. {
    11. super.onCreate(savedInstanceState);
    12. setContentView(R.layout.activity_read_string);
    13. tv_resource = findViewById(R.id.tv_resource); // 从布局文件中获取名叫tv_resource的文本视图
    14. showStringResource(); // 显示字符串资源
    15. }
    16. // 显示字符串资源
    17. private void showStringResource()
    18. {
    19. String value = getString(R.string.weather_str); // 从strings.xml获取名叫weather_str的字符串值
    20. tv_resource.setText("来自字符串资源:今天的天气是: "+value); // 在文本视图上显示文字
    21. }
    22. }

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

     

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

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

    示例如下:

             

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

               

     

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

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

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

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

    布局:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:orientation="vertical">
    5. <TextView
    6. android:id="@+id/tv_meta"
    7. android:layout_width="match_parent"
    8. android:layout_height="wrap_content"
    9. android:padding="5dp"
    10. android:gravity="center"
    11. android:textColor="#000000"
    12. android:textSize="17sp" />
    13. LinearLayout>

    代码:

    1. package com.example.myapplication;
    2. import android.content.pm.ActivityInfo;
    3. import android.content.pm.PackageManager;
    4. import android.os.Bundle;
    5. import android.widget.TextView;
    6. import androidx.appcompat.app.AppCompatActivity;
    7. public class MetaDataActivity extends AppCompatActivity
    8. {
    9. private TextView tv_meta; // 声明一个文本视图对象
    10. @Override
    11. protected void onCreate(Bundle savedInstanceState)
    12. {
    13. super.onCreate(savedInstanceState);
    14. setContentView(R.layout.activity_meta_data);
    15. // 从布局文件中获取名叫tv_meta的文本视图
    16. tv_meta = findViewById(R.id.tv_meta);
    17. showMetaData(); // 显示配置的元数据
    18. }
    19. // 显示配置的元数据
    20. private void showMetaData()
    21. {
    22. try
    23. {
    24. PackageManager pm = getPackageManager(); // 获取应用包管理器
    25. // 从应用包管理器中获取当前的活动信息
    26. ActivityInfo act = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
    27. Bundle bundle = act.metaData; // 获取活动附加的元数据信息
    28. String value = bundle.getString("weather"); // 从包裹中取出名叫weather的字符串
    29. tv_meta.setText("来自元数据信息:今天的天气是: "+value); // 在文本视图上显示文字
    30. }
    31. catch (Exception e)
    32. {
    33. e.printStackTrace();
    34. }
    35. }
    36. }

     

    strings.xml

    1. <resources>
    2. <string name="app_name">My Applicationstring>
    3. <string name="hello">中国,您好string>
    4. <string name="weather_str">今天,温度高string>
    5. resources>

    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:tools="http://schemas.android.com/tools"
    3. package="com.example.myapplication">
    4. <application
    5. android:allowBackup="true"
    6. android:dataExtractionRules="@xml/data_extraction_rules"
    7. android:fullBackupContent="@xml/backup_rules"
    8. android:icon="@mipmap/ic_launcher"
    9. android:label="@string/app_name"
    10. android:roundIcon="@mipmap/ic_launcher_round"
    11. android:supportsRtl="true"
    12. android:theme="@style/Theme.MyApplication"
    13. tools:targetApi="31">
    14. <activity android:name=".MetaDataActivity">
    15. <meta-data
    16. android:name="weather"
    17. android:value="@string/weather_str" />
    18. activity>
    19. <activity
    20. android:name=".ReadStringActivity"
    21. android:exported="false" />
    22. <activity
    23. android:name=".ActionUriActivity"
    24. android:exported="false" />
    25. <activity
    26. android:name=".ActResponseActivity"
    27. android:exported="false" />
    28. <activity
    29. android:name=".ActRequestActivity"
    30. android:exported="false" />
    31. <activity
    32. android:name=".ActReceiveActivity"
    33. android:exported="false" />
    34. <activity
    35. android:name=".ActSendActivity"
    36. android:exported="false" />
    37. <activity
    38. android:name=".LoginSuccessActivity"
    39. android:exported="false" />
    40. <activity
    41. android:name=".LoginInputActivity"
    42. android:exported="false" />
    43. <activity
    44. android:name=".ActNextActivity"
    45. android:exported="false" />
    46. <activity
    47. android:name=".NextActivity"
    48. android:exported="false" />
    49. <activity
    50. android:name=".MainActivity"
    51. android:exported="true">
    52. <intent-filter>
    53. <action android:name="android.intent.action.MAIN" />
    54. <category android:name="android.intent.category.LAUNCHER" />
    55. intent-filter>
    56. activity>
    57. application>
    58. manifest>

    另外:

    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:tools="http://schemas.android.com/tools"
    3. package="com.example.myapplication">
    4. <application
    5. android:allowBackup="true"
    6. android:dataExtractionRules="@xml/data_extraction_rules"
    7. android:fullBackupContent="@xml/backup_rules"
    8. android:icon="@mipmap/ic_launcher"
    9. android:label="@string/app_name"
    10. android:roundIcon="@mipmap/ic_launcher_round"
    11. android:supportsRtl="true"
    12. android:theme="@style/Theme.MyApplication"
    13. tools:targetApi="31">
    14. <activity android:name=".MetaDataActivity">
    15. <meta-data android:name="weather" android:value="晴天" />
    16. activity>
    17. <activity
    18. android:name=".ReadStringActivity"
    19. android:exported="false" />
    20. <activity
    21. android:name=".ActionUriActivity"
    22. android:exported="false" />
    23. <activity
    24. android:name=".ActResponseActivity"
    25. android:exported="false" />
    26. <activity
    27. android:name=".ActRequestActivity"
    28. android:exported="false" />
    29. <activity
    30. android:name=".ActReceiveActivity"
    31. android:exported="false" />
    32. <activity
    33. android:name=".ActSendActivity"
    34. android:exported="false" />
    35. <activity
    36. android:name=".LoginSuccessActivity"
    37. android:exported="false" />
    38. <activity
    39. android:name=".LoginInputActivity"
    40. android:exported="false" />
    41. <activity
    42. android:name=".ActNextActivity"
    43. android:exported="false" />
    44. <activity
    45. android:name=".NextActivity"
    46. android:exported="false" />
    47. <activity
    48. android:name=".MainActivity"
    49. android:exported="true">
    50. <intent-filter>
    51. <action android:name="android.intent.action.MAIN" />
    52. <category android:name="android.intent.category.LAUNCHER" />
    53. intent-filter>
    54. activity>
    55. application>
    56. manifest>

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

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

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

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

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

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

             

    1. 完整的activity节点配置示例如下:
    2. <activity android:name=".MainActivity">
    3. <intent-filter>
    4. <action android:name="android.intent.action.MAIN" />
    5. <category android:name="android.intent.category.LAUNCHER" />
    6. intent-filter>
    7. <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
    8. activity>

    布局:

    shortcuts.xml
    
    1. <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    2. <shortcut
    3. android:shortcutId="first"
    4. android:enabled="true"
    5. android:icon="@mipmap/ic_launcher"
    6. android:shortcutShortLabel="@string/first_short"
    7. android:shortcutLongLabel="@string/first_long">
    8. <intent
    9. android:action="android.intent.action.VIEW"
    10. android:targetPackage="com.example.myapplication"
    11. android:targetClass="com.example.myapplication.ActSendActivity" />
    12. <categories android:name="android.shortcut.conversation"/>
    13. shortcut>
    14. <shortcut
    15. android:shortcutId="second"
    16. android:enabled="true"
    17. android:icon="@mipmap/ic_launcher"
    18. android:shortcutShortLabel="@string/second_short"
    19. android:shortcutLongLabel="@string/second_long">
    20. <intent
    21. android:action="android.intent.action.VIEW"
    22. android:targetPackage="com.example.myapplication"
    23. android:targetClass="com.example.myapplication.ActionUriActivity" />
    24. <categories android:name="android.shortcut.conversation"/>
    25. shortcut>
    26. <shortcut
    27. android:shortcutId="third"
    28. android:enabled="true"
    29. android:icon="@mipmap/ic_launcher"
    30. android:shortcutShortLabel="@string/third_short"
    31. android:shortcutLongLabel="@string/third_long">
    32. <intent
    33. android:action="android.intent.action.VIEW"
    34. android:targetPackage="com.example.myapplication"
    35. android:targetClass="com.example.myapplication.LoginInputActivity" />
    36. <categories android:name="android.shortcut.conversation"/>
    37. shortcut>
    38. shortcuts>

     

    strings.xml

    1. <resources>
    2. <string name="app_name">My Applicationstring>
    3. <string name="hello">中国,您好string>
    4. <string name="weather_str">今天,温度高string>
    5. <string name="first_short">firststring>
    6. <string name="first_long">启停活动string>
    7. <string name="second_short">secondstring>
    8. <string name="second_long">来回跳转string>
    9. <string name="third_short">thirdstring>
    10. <string name="third_long">登录返回string>
    11. resources>

    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:tools="http://schemas.android.com/tools"
    3. package="com.example.myapplication">
    4. <application
    5. android:allowBackup="true"
    6. android:dataExtractionRules="@xml/data_extraction_rules"
    7. android:fullBackupContent="@xml/backup_rules"
    8. android:icon="@mipmap/ic_launcher"
    9. android:label="@string/app_name"
    10. android:roundIcon="@mipmap/ic_launcher_round"
    11. android:supportsRtl="true"
    12. android:theme="@style/Theme.MyApplication"
    13. tools:targetApi="31">
    14. <activity android:name=".MetaDataActivity">
    15. <meta-data android:name="weather" android:value="晴天" />
    16. activity>
    17. <activity
    18. android:name=".ReadStringActivity"
    19. android:exported="false" />
    20. <activity
    21. android:name=".ActionUriActivity"
    22. android:exported="false" />
    23. <activity
    24. android:name=".ActResponseActivity"
    25. android:exported="false" />
    26. <activity
    27. android:name=".ActRequestActivity"
    28. android:exported="false" />
    29. <activity
    30. android:name=".ActReceiveActivity"
    31. android:exported="false" />
    32. <activity
    33. android:name=".ActSendActivity"
    34. android:exported="false" />
    35. <activity
    36. android:name=".LoginSuccessActivity"
    37. android:exported="false" />
    38. <activity
    39. android:name=".LoginInputActivity"
    40. android:exported="false" />
    41. <activity
    42. android:name=".ActNextActivity"
    43. android:exported="false" />
    44. <activity
    45. android:name=".NextActivity"
    46. android:exported="false" />
    47. <activity
    48. android:name=".MainActivity"
    49. android:exported="true">
    50. <intent-filter>
    51. <action android:name="android.intent.action.MAIN" />
    52. <category android:name="android.intent.category.LAUNCHER" />
    53. intent-filter>
    54. <meta-data
    55. android:name="android.app.shortcuts"
    56. android:resource="@xml/shortcuts" />
    57. activity>
    58. application>
    59. manifest>

     

    PS:示例

    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    2. package="com.example.chapter04">
    3. <application
    4. android:allowBackup="true"
    5. android:icon="@mipmap/ic_launcher"
    6. android:label="@string/app_name"
    7. android:roundIcon="@mipmap/ic_launcher_round"
    8. android:supportsRtl="true"
    9. android:theme="@style/AppTheme">
    10. <activity android:name=".MainActivity">
    11. <intent-filter>
    12. <action android:name="android.intent.action.MAIN" />
    13. <category android:name="android.intent.category.LAUNCHER" />
    14. intent-filter>
    15. <meta-data
    16. android:name="android.app.shortcuts"
    17. android:resource="@xml/shortcuts" />
    18. activity>
    19. <activity android:name=".ActStartActivity" />
    20. <activity android:name=".ActFinishActivity" />
    21. <activity android:name=".ActLifeActivity" />
    22. <activity android:name=".ActNextActivity" />
    23. <activity
    24. android:name=".JumpFirstActivity"
    25. android:launchMode="standard" />
    26. <activity android:name=".JumpSecondActivity" />
    27. <activity android:name=".LoginInputActivity" />
    28. <activity android:name=".LoginSuccessActivity" />
    29. <activity android:name=".ActionUriActivity" />
    30. <activity android:name=".ActSendActivity" />
    31. <activity android:name=".ActReceiveActivity" />
    32. <activity android:name=".ActRequestActivity" />
    33. <activity android:name=".ActResponseActivity" />
    34. <activity android:name=".ReadStringActivity" />
    35. <activity android:name=".MetaDataActivity">
    36. <meta-data
    37. android:name="weather"
    38. android:value="@string/weather_str" />
    39. activity>
    40. application>
    41. manifest>

  • 相关阅读:
    第2-4-3章 规则引擎Drools基础语法-业务规则管理系统-组件化-中台
    01 背包
    Python实现ACO蚁群优化算法优化支持向量机分类模型(SVC算法)项目实战
    深度学习环境配置(Anaconda+pytorch+pycharm+cuda)
    【C++】哈希表特性总结及unordered_map和unordered_set的模拟实现
    数据库系统原理与实践 笔记 #7
    功能测试常用的测试用例大全
    nginx学习第一天
    arm-none-eabi-gcc下实现printf的两种方式
    关于 byte 的范围
  • 原文地址:https://blog.csdn.net/m0_61442607/article/details/126113391