• Android基础——隐式intent和intent过滤器


    当不直接在intent中指定组件名时,就是隐式使用intent,此时要配合过滤器来使用,找到目标的组件

    注册文件

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myintentiii">
    
        <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=".DetailActivity">
                <intent-filter>
                    <action android:name="android.intent.action.VIEW"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                </intent-filter>
            </activity>
    
            <activity android:name=".MainActivity">
                <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"?>
    <LinearLayout 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"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示图片" />
    
    </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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=".DetailActivity">
    
        <ImageView
            android:id="@+id/image"
            android:src="@drawable/b"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    
    </LinearLayout>

    java代码

    package com.example.myintentiii;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button button = (Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    // 隐式创建Intent,由于不知道调用的是谁,
                    // 所以对应的要给Detail增加intent过滤器
                    Intent intent = new Intent();
                    intent.setAction(intent.ACTION_VIEW);
                    startActivity(intent);
                }
            });
        }
    }
  • 相关阅读:
    [转]myeclipse 生成JAR包并引入第三方包
    Composer 基本指令操作使用
    Laravel Eloquent ORM
    [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
    C# IoC 容器
    【转载】laravel的MVC
    [转]Laravel 4之Eloquent ORM
    [转]Laravel 4之数据库操作
    svn unable to connect to a repository at url 执行上下文错误 不能访问SVN服务器问题
    Make a travel blog by Blogabond the theme of wordpress
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12237769.html
Copyright © 2020-2023  润新知