【Android】Intent的使用及案例

1. 概述:

        在基于HTML的页面程序中,我们使用"超链接"来实现页面之间的跳转。之前我们了解到Android应用程序界面是由一个或多个Activity组成的,一个Activity相当于HTML的一个页面,而当一个Android应用程序具有多个相互联系的Activity时,它们之间是如何实现跳转的呢?就是Intent来实习的。

        Intent的作用不仅时实现Activity之间的跳转,它还是Android平台的各个部分之间实现信息沟通的桥梁。

2. Intent

        Android的Intent对象是联系各个Activity的关键对象。Intent,翻译成中文就是“意图”,我们可以这样来理解Intent:通过Intent对象,来告诉Android要做什么。

        Intent intent=new Intent(this,Activity02.class);
        this.startActivity(intent);

3. Intent的extra

        在通过Intent启动某个Activity运行时,有时可能需要传递一些附加的数据到被启动的Activity中,这可以使用extra。extra只作为传递给目标Activity的附加数据,不作为挑选Activity的匹配依据。extra是以“key/value"形式表示的数据,其中的key时String类型的”键“,value可以是Java基本数据类型也可以是实现了android.os.Parcelable接口的对象数据类型。Intent类提供了写入及读取基本数据类型数据的方法。例如,我们希望传递一个整数和一个字符串到目标Activity,则可以在创建的Intent对象上执行如下的代码。 

intent.putExtra("productName","iphone");
intent.putExtra("productAmount",100);

         在目标Activity中,通过获得打开该Activity的Intent对象来从中获取传递过来的数据,内容如下:

String pn=intent.getStringExtra("productName");
int pa=intent.getIntExtra("ProductAmount");

4. 获取Activity返回的结果

         想要获取被打开的Activity返回的结果,我们不是使用startActivity()方法而是使用startActivityForResult()方法

public void startActivityForResult(@SuppressLint("UnknownNullness") Intent intent,
            int requestCode)

5. 例子:

Main_Activity.java

package com.example.demo0419;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    public static int r1=0;
    public TextView t1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn=this.findViewById(R.id.bt1);
        btn.setOnClickListener( this);

        t1=this.findViewById(R.id.tv1);

    }

    @Override
    public void onClick(View view) {
        Intent intent=new Intent(this,a2.class);
        this.startActivityForResult(intent,MainActivity.r1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if ((requestCode == MainActivity.r1) &&(resultCode== Activity.RESULT_OK)){
            int which=data.getIntExtra("result",-1);
            t1.setText(which+" ");
            System.out.println(t1);

        }
    }
}

a2.java 另一个视图

package com.example.demo0419;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;

public class a2 extends Activity implements View.OnClickListener {
    private ImageView i1,i2;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.l2);

        i1=(ImageView)this.findViewById(R.id.img1);
        i1.setOnClickListener(this);
        i2=(ImageView)this.findViewById(R.id.img2);
        i2.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        int id= view.getId();
        if (id==R.id.img1){
            Intent intent=new Intent();
            intent.putExtra("result",1);
            this.setResult(Activity.RESULT_OK,intent);
        }else{
            Intent intent=new Intent();
            intent.putExtra("result",2);
            this.setResult(Activity.RESULT_OK,intent);
        }
        this.finish();
    }
}

main_avtvity.xml

<?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/bt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="点击"/>
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="23dp"
        android:layout_weight="1"/>

</LinearLayout>

l2.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">

    <ImageView
        android:id="@+id/img1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:scaleType="fitCenter"
        android:src="@drawable/m1"
        android:layout_weight="1"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="@color/black"
        />
    <ImageView
        android:id="@+id/img2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:scaleType="fitCenter"
        android:src="@drawable/m2"
        android:layout_weight="1"
        />

</LinearLayout>

注册a2的active

效果:点击不同照片返回不同的值

 比如点击上面那张图片

猜你喜欢

转载自blog.csdn.net/m0_56233309/article/details/124276102