2022-10-18学习内容
1.利用资源文件配置字符串
1.1activity_read_string.xml
<?xml version="1.0" encoding="utf-8"?> <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"/> </LinearLayout>
1.2strings.xml
<resources> <string name="app_name">chapter04</string> <string name="weather_str">晴天</string> </resources>
1.3ReadStringActivity.java
package com.example.chapter04; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class ReadStringActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read_string); TextView tv_resource = findViewById(R.id.tv_resource); // 从string.xml获取名叫weather_str的字符串值 String value = getString(R.string.weather_str); tv_resource.setText(value); } }
1.4效果:
2.利用元数据传递配置信息
2.1activity_meta_data.xml
<?xml version="1.0" encoding="utf-8"?> <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"/> </LinearLayout>
2.2AndroidManifest.xml
<?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/Theme.MyApplication"> <activity android:name=".ActResponseActivity" android:exported="false" /> <activity android:name=".ActReceiveActivity" android:exported="false" /> <activity android:name=".LoginSuccessActivity" android:exported="false" /> <activity android:name=".JumpSecondActivity" android:exported="false" /> <activity android:name=".ActFinishActivity" android:exported="false" /> <activity android:name=".MetaDataActivity" android:exported="true" android:launchMode="standard"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="weather" android:value="晴天"/> </activity> </application> </manifest>
2.3MetaDataActivity.java
package com.example.chapter04; import androidx.appcompat.app.AppCompatActivity; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.widget.TextView; public class MetaDataActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_meta_data); TextView tv_meta = findViewById(R.id.tv_meta); // 获取应用包管理器 PackageManager pm = getPackageManager(); try { // 从应用包管理器中获取当前的活动信息 ActivityInfo info = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA); // 获取活动附加的元数据信息 Bundle bundle = info.metaData; String weather = bundle.getString("weather"); tv_meta.setText(weather); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } }
2.4效果:
3.给应用页面注册快捷方式
3.1shortcuts.xml
<?xml version="1.0" encoding="utf-8"?> <shotcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:shortcutId="first" android:enabled="true" android:icon="@mipmap/ic_launcher_round" android:shortcutShortLabel="@string/first_short" android:shortcutLongLabel="@string/first_long"> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.example.chapter04" android:targetClass="com.example.chapter04.ActStartActivity"/> <categories android:name="android.shortcut.conversation" /> </shortcut> <shortcut android:shortcutId="second" android:enabled="true" android:icon="@mipmap/ic_launcher_round" android:shortcutShortLabel="@string/second_short" android:shortcutLongLabel="@string/second_long"> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.example.chapter04" android:targetClass="com.example.chapter04.JumpFirstActivity"/> <categories android:name="android.shortcut.conversation" /> </shortcut> <shortcut android:shortcutId="third" android:enabled="true" android:icon="@mipmap/ic_launcher_round" android:shortcutShortLabel="@string/third_short" android:shortcutLongLabel="@string/third_long"> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.example.chapter04" android:targetClass="com.example.chapter04.LoginInputActivity"/> <categories android:name="android.shortcut.conversation" /> </shortcut> </shotcuts>
3.2strings.xml
<resources> <string name="app_name">chapter04</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>
3.3AndroidManifest.xml
<?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/Theme.MyApplication"> <activity android:name=".ActResponseActivity" android:exported="false" /> <activity android:name=".ActReceiveActivity" android:exported="false" /> <activity android:name=".LoginSuccessActivity" android:exported="false" /> <activity android:name=".JumpSecondActivity" android:exported="false" /> <activity android:name=".JumpFirstActivity" android:exported="true" /> <activity android:name=".LoginInputActivity" android:exported="true" /> <activity android:name=".ActFinishActivity" android:exported="false" /> <activity android:name=".MetaDataActivity" android:exported="true" android:launchMode="standard"> <meta-data android:name="weather" android:value="晴天"/> </activity> <activity android:name=".ActStartActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.shortcuts" android:resource="@xml/showcuts"/> </activity> </application> </manifest>
3.4效果:
点快捷方式“启停活动”:
点快捷方式“来回跳转”:
点快捷方式“登录返回”: