Android Intent实现页面之间跳转

什么是Intent
Intent可以理解为信使(意图)
由Intent来协助完成Android各个组件之间的通讯
Intent实现页面逐渐的跳转
1.startActivity(inetnt)
2.startActivityForResult(intent, requestCode);
 onAcitivtyResult(int requestCode, int resultCode, Intent data)
 setResult(resultCode, data);
 
先创建两个xml文件firstactivity.xml和secondactivity.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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="通过Intent实现页面之间的跳转\n通过按钮1实现无返回结果的页面跳转\n通过按钮2实现有返回结果的页面跳转" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮2" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="页面跳转后的结果将会显示在这里" />

</LinearLayout>

firstactivity.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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击返回第一个Activity" />

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

</LinearLayout>

secondactivity.xml

在AndroidManifest.xml中注册这两个xml,并设置firstactivity.xml是启动的时候加载的xml。

        <activity
            android:name="com.example.intentdemo.FirstActivity"
            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="com.example.intentdemo.SecondActivity"
            android:label="@string/app_name" >
        </activity>

FirstActivity.java的buttton1用于通过startActivity()方法实现跳转。

        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

FirstActivity.java的buttton2用于通过startActivityWithResult()方法实现跳转。

        button2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                int requestCode = 1;    // 自己取的
                startActivityForResult(intent, requestCode);
            }

        });

第二个Activity返回结果并通过finish()销毁自己。

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                int resultCode = 2;
                Intent data = new Intent();
                data.putExtra("data", "这是第二个Activity返回的结果");
                setResult(resultCode, data);
                finish();
            }
        });
    }

第一个Activity通过onActivityResult()方法得到返回的结果并显示在TextView中。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 2) {
            textView.setText(data.getStringExtra("data"));
        }
    }

package com.example.intentdemo;

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 FirstActivity extends Activity {

    private Button button1;
    private Button button2;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstactivity);

        textView = (TextView) findViewById(R.id.textView1);

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

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

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

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                int requestCode = 1;    // 自己取的
                startActivityForResult(intent, requestCode);
            }

        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 2) {
            textView.setText(data.getStringExtra("data"));
        }
    }
}

FirstActivity.java

package com.example.intentdemo;

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 SecondActivity extends Activity {

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);

        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                int resultCode = 2;
                Intent data = new Intent();
                data.putExtra("data", "这是第二个Activity返回的结果");
                setResult(resultCode, data);
                finish();
            }
        });
    }

}

SecondActivity.java

效果:

时间: 2024-08-14 21:31:54

Android Intent实现页面之间跳转的相关文章

Android Studio 使用Intent实现页面的跳转(带参数)

不管是在APP,还是在网站中,页面之间的跳转都是很常见的,本文主要讲一下在APP中,如何通过Intent实现页面的跳转. 不带参数: 写在MainActivity页面的代码: 1 Intent intent = new Intent(); 2 intent.setClass(MainActivity.this, LoginActivity.class);//从MainActivity页面跳转至LoginActivity页面 3 this.startActivity(intent); 带参数: 写

Android两个页面之间的切换效果工具类

import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Build; import android.widget.Toast; public class ActivityAnimationUtil { private Context context; pr

Android Intent实现页面跳转

Intent可以来协助完成Android各个组件之间的通信 1:startActivity(intent);     //直接启动 /* * 通过监听点击事件跳转套第二个activity */ button=(Button) findViewById(R.id. button1); button.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Intent intent= ne

[Android]Intent 实现页面跳转的方式[转]

第一种方式,用action来跳转. 1.使用Action跳转,如果有一个程序的 AndroidManifest.xml中的某一个Activity的IntentFilter段中定义了包含了相同的Action那么这个Intent 就与这个目标Action匹配.如果这个IntentFilter段中没有定义 Type,Category,那么这个 Activity就匹配了.但是如果手机中有两个以上的程序匹配,那么就会弹出一个对话可框来提示说明. Action的值在Android中有很多预定义,如果你想直接

使用Intent实现页面之间的跳转

Intent extends Object implements Parcelable Cloneable 构造方法 Intent() :Intent(Intent o):Intent(Context packageContext, Class<?> cls) Intent(String action):Intent(String action, Uri uri): Intent(String action, Uri uri, Context packageContext, Class<

Android 安卓实现页面相互跳转并相互传递参数

一.对于两个页面之间相互传值,跳转的时候我们使用 startActivityForResult(intent,0),而不是startActivity(intent) 这个方法 第一个页面中在触发跳转的按钮中写上 Intent intent=new Intent(MainActivity.this,SecondActivity.class); //将text框中的值传入 intent.putExtra("name",text.getText().toString()); //为了接受Se

页面之间跳转传值

页面之间传值: a.html <html> <head> <title> New Document </title> <script> function to (){ var getval =document.getElementById("cc").value; document.location.href("b.html?cc="+getval); } </script> </head

js实现两个页面之间跳转参数传递

html在设计时,规定跳转地址后加"?"表示从此开始为跟随页面地址跳转的参数. 有时候,我们希望获得相应的跳转前页面里的内容,这时候我们就可以考虑将内容以参数形式放到地址中传过来,这里我建议将参数以变量形式传递. 代码案例如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>direct1.ht

android入门:activity之间跳转,并且回传参数

介绍: 两个activity进行跳转,在跳转过程中,将message由MainActivity传递到secondActivity,并且当secondActivity退回至MainActivity时,也传递消息给MainActivity. 首先是MainActivity的布局文件: activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=