Day13、Android中“情侣”控件之间表达“爱意”的Intent

Intent,可理解为信使,意图,通过它可以协助各个控件之间进行通讯,就等同于情侣之间所表达的爱意;在Android中,也将数据保存在Bundle对象中,就等同于撩妹高手的“情话”抽象和封装在一起;然后通过Intent提供的putExtras()方法将携带的数据(早保存在Bundle对象中的数据)保存在Intent对象中,这样便将”爱意”和”情话”相结合,在Activity之间运行复杂的逻辑,即这样才能完美告白。。。。。。

一、Intent实现页面跳转

1.startActivity(intent);
2.startActivityForResult(intent,requestCode);
a.在第一个Activity重写方法,接收回传数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
b.在第二个Activity重写方法,设置所要回传的数据
setResult(resultCode, data);

二、编写两个Activity来实现简单的页面跳转。

1.A_Activity.java和aactivity.xml代码
package com.oldtogether.intenttest;

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

public class A_Activity extends Activity {

    private Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aactivity);
        /*
         * 1、通过点击btn1实现页面跳转
         * 2、调用一个参数的startActivity方法实现页面跳转
         */

        btn1 = (Button) findViewById(R.id.btn_first);
        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(A_Activity.this, B_Activity.class);
                startActivity(intent);
            }
        });

    }
}
<?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" >

    <Button
        android:id="@+id/btn_first"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一种启动方式" />

    <Button
        android:id="@+id/btn_second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第二种启动方式" />

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="把第二个页面回传的数据显示出来"
        android:textColor="#0FF"
        android:textSize="32sp" />

</LinearLayout>
2.B_Activity.java和bactivity.xml代码
package com.oldtogether.intenttest;

import android.app.Activity;
import android.os.Bundle;

public class B_Activity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bactivity);
    }
}
<?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" >

    <Button 
        android:id="@+id/btn_back"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回"/>


</LinearLayout>
3.记得在AndroidManifest.xml文件中注册自定义的Activity,并将主接口给它。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.oldtogether.intenttest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".A_Activity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".B_Activity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>
4.运行结果

这里写图片描述
这里写图片描述

三、编写有返回值的页面跳转的代码

1.A_Activity.java和aactivity.xml代码
package com.oldtogether.intenttest;

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

public class A_Activity extends Activity {

    private Button btn1;
    private Button btn2;
    private TextView tvShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aactivity);
        /*
         * 1、通过点击btn1实现页面跳转
         * 2、调用一个A_Activity.this参数的startActivity方法实现页面跳转
         */

        tvShow = (TextView) findViewById(R.id.tv_show);
        btn1 = (Button) findViewById(R.id.btn_first);
        btn2 = (Button) findViewById(R.id.btn_second);
        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(A_Activity.this, B_Activity.class);
                startActivity(intent);
            }
        });
        btn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(A_Activity.this, B_Activity.class);
                /*
                 * 1、第一个参数:Intent对象 
                 * 2、第二个参数:请求的一个标识
                 */
                startActivityForResult(intent, 1);
            }
        });

    }
    /*
     *通过startActivityForResult跳转,接收返回数据的方法
     *requestCode:请求标识
     *resultCode:第二个页面返回的标识
     *data:第二个页面回传的数据(绑在意图对象中)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1 && resultCode==2){
            String content=data.getStringExtra("data");
            tvShow.setText(content);
        }
    }
}
<?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" >

    <Button
        android:id="@+id/btn_first"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一种启动方式" />

    <Button
        android:id="@+id/btn_second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第二种启动方式" />

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello,我是一个TextView!"
        android:textColor="#f00"
        android:textSize="32sp" />

</LinearLayout>
2.B_Activity.java和bactivity.xml代码
package com.oldtogether.intenttest;

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

public class B_Activity extends Activity{

    private Button btnBack;
    private String backContent="hi,这是来自第二个Activity的数据,并且返回到第二个页面。";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bactivity);
        /*
         * 1、什么时候给第二个页面回传数据,即返回点击按钮时
         * 2、所返回的数据:backContent
         * 3、将返回的数据绑在意图对象data中
         * 4、requestCode:作为一个标识,需要和调用它的那个Activity对接
         */

        btnBack = (Button) findViewById(R.id.btn_back);
        btnBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            Intent intent2 = new Intent();
            intent2.putExtra("data", backContent);
            setResult(2, intent2);
            //关闭当前Activity,回顾关闭Activity的两种方法
            finish();
            }
        });
    }
}
<?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" >

    <Button 
        android:id="@+id/btn_back"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回"/>

</LinearLayout>
3.同样在AndroidManifest.xml文件中注册,代码如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.oldtogether.intenttest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".A_Activity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".B_Activity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>
4.运行结果

这里写图片描述
这里写图片描述
这里写图片描述

四、心得总结

1.Android中独特的类Intent,和Android四大组件一样有这同样重要的地位,是开发中重要的一笔,因为有了意图的界面之间或控件之间进行通讯才能满足复杂的业务逻辑和客户需求。
2.在理解Intent对象时,我们将其可以看作是”快递包裹“,起作用是将你封装和定义的数据通过其自身的方法(putExtras)放到”快递包裹“里面,通过你的订单和电话号码在相应的地方取走你的快递即可。
3.写完这篇博文,可以开心的撸一把了,人活着,就是要简简单单,和一杯奶茶也行,静静的躺着听歌也行,打场篮球也行,千万别把全部的精力和时间放在没有可能的人身上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值