android使用Intent实现页面跳转--startActivity()与startActivityForResult()

android页面间的页面跳转,类似于<a href="">标签,不同的是android有自己独特的第二种方式,也就是有返回值的startActivityForResult()方式。

1.第一种startActivity()比较简单,只需要在参数列表里传一个Intent对象,指明跳转前后的页

/*有返回值的跳转,还需要一个方法来接受返回值第二个参数为请求码,用于区分不同的跳转,可以根据业务需求自己编号*/
startActivityForResult(intent,1);

面就行了:

Intent intent = new Intent(mContext,IntentTestReturnActivity.class);
startActivity(intent);
Intent的参数列表中要传两个上下文对象,可以直接用  类名.class的形式 ,也可以定义Conext对象,将其传的参数列表中

private Context mContext;
mContext = this;
然后就可以利用Button来实现Activity间的跳转了。

写完回来补充点内容。。

在页面跳转时,后面的页面可以有返回值,同样在跳转时,页面也可以传递给要跳转页面特定的值。

实现:

第一个页面Activity代码:

Intent intent = new Intent(mContext,IntentTestReturnActivity.class);
                /*
                    像后一个页面传值
                 */
                String ns = "前一个页面传递的消息";
                intent.putExtra("primess",ns);
                startActivity(intent);

跳转目标页面的代码:

//前一个页面传过来的值
Intent intent = getIntent();
String bdata = intent.getStringExtra("primess");


2.带参数的startActivityForResult()跳转比第一种要复杂,毕竟要涉及参数的传递

大体的过程就是在第一个页面定义好要跳转的两个Activity(这里与第一种类似,用Intent对象指定,然后用startActivityForResult传一个Intent就行了

Intent intent = new Intent(mContext,IntentTestReturnActivity.class);
startActivityForResult(intent,1);
然后就要指定传递的数据了。android规定要用两个鉴别码来区别不同的跳转(一个app中可能会有多个待参数的跳转,活着一个页面也可能给多个不同的页面返回不同的值,所以要有区分)

/*
有返回值的跳转,还需要一个方法来接受返回值第二个参数为请求码,用于区分不同的跳转,可以根据业务需求自己编号*/
startActivityForResult(intent,1);
在第一个页面中定义onActivityResult()方法来获取返回的值,在这个方法中就要用到 两个鉴别码,通过if判断它们来获取值

在第二个页面来指定返回的值(页面之间的返回值实际为一个Intent对象所以在接受值的时候还有用到getStringExtra()等类似的方法),用setResult()方法来指定返回值的鉴别码和返回它

Intent data = new Intent();
data.putExtra("data",content);//封装好数据
setResult(2,data);


完整代码:

Activity1:

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class IntentTestActivity extends AppCompatActivity {
    Button but1,but2;
    TextView tv;
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_test);
        but1 = (Button)findViewById(R.id.itentbutton1);
        but2 = (Button)findViewById(R.id.itentbutton2);
        tv = (TextView)findViewById(R.id.itenttv);
        mContext = this;
        but1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                mContext:上下文对象,这个方法指定从哪个Activity跳转到哪个,也可以直接用IntentTestActivity.this,但是不能直接用this
                 */
                Intent intent = new Intent(mContext,IntentTestReturnActivity.class);
                startActivity(intent);
            }
        });
        but2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                mContext:上下文对象,这个方法指定从哪个Activity跳转到哪个,也可以直接用IntentTestActivity.this,但是不能直接用this
                 */
                Intent intent = new Intent(mContext,IntentTestReturnActivity.class);
                /*
                有返回值的跳转,还需要一个方法来接受返回值
                第二个参数为请求码,用于区分不同的跳转,可以根据业务需求自己编号
                 */
                startActivityForResult(intent,1);
            }
        });
    }
     /*
     通过startActivityForResult的跳转,接受返回数据的方法
      */

    @Override
    /*
    requestCode:请求的标志
    resultCode:第二个页面返回的标志
    data:第二个页面回传的数据
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == 2){//通过请求码和返回码区分不同的返回
            String content = data.getStringExtra("data");//data:后一个页面putExtra()中设置的键名
            tv.setText(content);
        }
    }
}

layout1:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.wangchong.myfirst.IntentTestActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/itentbutton1"
        android:layout_width="433dp"
        android:layout_height="wrap_content"
        android:text="第一种跳转方式"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="16dp" />

    <Button
        android:id="@+id/itentbutton2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第二种跳转方式"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="77dp" />

    <TextView
        android:id="@+id/itenttv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:gravity="center"
        android:textSize="25dp"
        tools:layout_editor_absoluteX="17dp"
        tools:layout_editor_absoluteY="149dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Activity2:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class IntentTestReturnActivity extends AppCompatActivity {
    Button but;
    private String content = "返回的值";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_test_return);
        but = (Button)findViewById(R.id.itentreturn);
        /*
        第二个页面返回给第一个页面的实际上是一个Intent对象
         */
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                返回的Intent
                Intent data = new Intent();
                //键名=>键值的形式
                data.putExtra("data",content);//封装好数据
                /*
                resultContent:返回标识符,区分不同的返回
                data:返回的Intent对象(数据)
                 */
                 setResult(2,data);
//                 结束当前页面
                 finish();
//                 至此返回值已经就绪,需要在前一个页面去接受
            }
        });
    }
}

layout2:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.wangchong.myfirst.IntentTestReturnActivity">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">



    <Button
        android:id="@+id/itentreturn"
        android:layout_width="407dp"
        android:layout_height="wrap_content"
        android:text="返回数据"
        tools:layout_editor_absoluteX="2dp"
        tools:layout_editor_absoluteY="16dp" />
</LinearLayout>

</android.support.constraint.ConstraintLayout>


运行效果:


 
 








  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值