android使用Intent实现页面跳转--startActivity()与startActivityForResult()
来源:互联网 发布:数据如何脱敏 编辑:程序博客网 时间:2024/06/07 08:57
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>
运行效果:
阅读全文
0 0
- android使用Intent实现页面跳转--startActivity()与startActivityForResult()
- 【Android】startActivityForResult与startActivity
- Android使用Intent实现页面跳转
- Activity间的跳转,startActivity与startActivityForResult
- 使用Intent实现页面跳转
- 使用Intent实现页面跳转
- 使用Intent实现页面跳转
- 使用Intent实现页面跳转
- 使用Intent实现页面跳转
- Android页面跳转Intent使用
- android开发基础:Intent与Bundle的使用,实现页面跳转及信息传递
- android中startActivityForResult( )与startActivity( )的不同之处
- Android 中startActivity 与startActivityForResult区别
- Android编程学习笔记 之 使用Intent实现页面跳转
- Android开发 第13课 使用Intent实现页面跳转
- Android使用Webview显示页面以及点击跳转startActivity问题
- Android中Activity跳转 startActivity和startActivityForResult之间的区别
- startActivityForResult与startActivity
- Centos7和RHEL7最小安装中替换ifconfig和netstat的命令
- C语言处理二值图像并计算面积和周长
- Hbase基础架构应用
- 6.10
- You need to use a Theme.AppCompat theme (or descendant) with this activity.
- android使用Intent实现页面跳转--startActivity()与startActivityForResult()
- Java基本知识
- C++学习之路(20)---C++ 类成员函数后面加 const关键字
- 关于智能指针!
- Maven之(七)pom.xml配置文件详解
- 学习笔记-链表查询优化
- java中面向对象思想和三大特性的理解
- 查找程序的bug
- spring aop学习10:Aop的坑(Aop对于内部调用无效)