在不论什么程序开发中,都会遇到页面之间跳转的情况,Android开发也不例外.这一节,我们来认识下Android项目中如何进行页面跳转.页面跳转分为有參数和无參数页面跳转,已经接受还有一个页面的返回值等。Android中页面跳转经常使用到的是Intent ,可是Intent不仅用做页面跳转,还能够做其它事情,比如拨打电话,发送短信,调用其它程序等。这节我们主要认识下如何通过Intent进行页面跳转.

1.页面跳转

我们首先简单认识下Intent,Intent有有几个重载构造函数。我们使用当中一个构造函数进行页面跳转。

Intent intent = new Intent(MainActivity.this,SecondActivity.class);  有两个參数。第一个參数代表当前页面对象,第二个參数代表要跳转到的目标对象。

创建完Intent后,我们使用startActivity进行启动。、

以下我们看下这个效果

启动页面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="74dp"android:text="跳转到下一个页面" /></RelativeLayout>
package com.example.hellotest;import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class MainActivity extends Activity {private Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn=(Button)findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener() //绑定注冊button单击事件   {  @Override  public void onClick(View arg0) {  // button跳转   Intent intent = new Intent(MainActivity.this,SecondActivity.class);  startActivity(intent);  }  });  }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

目标页面

<?

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:id="@+id/textView1" android:layout_width="219dp" android:layout_height="wrap_content" android:layout_weight="0.19" android:text="第二个页面" /> </LinearLayout>

2.带參数页面跳转

这个演示样例,我们来看下页面跳转并传值,首先A页面跳转到B页面,并传递值,然后B页面返回A页面。同一时候向A页面传递值。

效果图例如以下:

发送内容到B页面

点击返回到A页面。并把页面输入内容传递到A页面

A页面布局和代码

<?xml version="1.0" encoding="utf-8"?

> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_weight="0.9" android:layout_height="fill_parent">    </LinearLayout>    <LinearLayout android:layout_width="fill_parent" android:layout_weight="0.1" android:orientation="vertical" android:layout_height="fill_parent">   <LinearLayout android:layout_marginLeft="10px" android:layout_marginRight="10px" android:gravity="center" android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">    <TextView android:textSize="8pt" android:text="发送内容:" android:id="@+id/tvSend" android:layout_weight="0.7" android:layout_width="fill_parent" android:layout_height="wrap_content">       </TextView>       <EditText android:layout_weight="0.3" android:layout_width="fill_parent" android:text="" android:id="@+id/etmsg" android:layout_height="wrap_content">       </EditText>    </LinearLayout>    <LinearLayout android:layout_marginLeft="10px" android:layout_marginRight="10px" android:gravity="center" android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">    <Button android:text="发送" android:textSize="9pt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/btnSend" > </Button>    </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_weight="0.9" android:layout_height="fill_parent">    <TextView android:textSize="8pt" android:text="接受返回内容:" android:id="@+id/tvreturn" android:layout_weight="0.7" android:layout_width="fill_parent" android:layout_height="wrap_content">       </TextView> </LinearLayout>     </LinearLayout> </LinearLayout>

package com.example.hellotest;import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;public class OrginLayOut  extends Activity {private Button btn;//发送buttonprivate EditText edtiText;//发送内容private TextView tvReturn;//接受子页面返回显示的内容protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.orginlayout);btn=(Button)findViewById(R.id.btnSend);//获取button对象edtiText=(EditText)findViewById(R.id.etmsg);//获取文本框对象tvReturn=(TextView)findViewById(R.id.tvreturn);btn.setOnClickListener(new OnClickListener() //绑定注冊button单击事件   {  @Override  public void onClick(View arg0) {  // button跳转   Intent intent = new Intent(); intent.setClass(OrginLayOut.this, TargetActivity.class);Bundle bundle = new Bundle();bundle.putString("msg",edtiText.getText().toString());//传值intent.putExtras(bundle);// startActivity(intent);startActivityForResult(intent, 0);}  });  }//接受页面的返回值@Override//requestCode请求标识   //resultCode 返回标识protected void onActivityResult(int requestCode, int resultCode, Intent data) {if(requestCode == 0) {if(resultCode == Activity.RESULT_OK) {String content=data.getStringExtra("returnmsg");tvReturn.setText("接受返回内容:"+content);}}}
}

B页面布局和代码

<?

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_weight="0.9" android:layout_height="fill_parent">    </LinearLayout>    <LinearLayout android:layout_width="fill_parent" android:layout_weight="0.1" android:orientation="vertical" android:layout_height="fill_parent">  <LinearLayout android:layout_marginLeft="10px" android:layout_marginRight="10px" android:gravity="center" android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <TextView android:textSize="8pt" android:text="接受内容:" android:id="@+id/tvreceivemsg" android:layout_weight="0.7" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> </LinearLayout>   <LinearLayout android:layout_marginLeft="10px" android:layout_marginRight="10px" android:gravity="center" android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">    <TextView android:textSize="8pt" android:text="返回内容:" android:layout_weight="0.7" android:layout_width="fill_parent" android:layout_height="wrap_content">       </TextView>       <EditText android:layout_weight="0.3" android:layout_width="fill_parent" android:text="" android:id="@+id/etreturnmsg" android:layout_height="wrap_content">       </EditText>    </LinearLayout>    <LinearLayout android:layout_marginLeft="10px" android:layout_marginRight="10px" android:gravity="center" android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">    <Button android:text="返回" android:textSize="9pt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/btnreturn" > </Button>    </LinearLayout>     </LinearLayout> </LinearLayout>

package com.example.hellotest;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.EditText;
import android.widget.TextView;public class TargetActivity extends Activity {private TextView tv;private Button btn;private EditText returnText;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.targetlayout);tv=(TextView)findViewById(R.id.tvreceivemsg);btn=(Button)findViewById(R.id.btnreturn);//获取button对象returnText=(EditText)findViewById(R.id.etreturnmsg);//获取文本框对象Bundle bunde = this.getIntent().getExtras();String strs="接受内容:"+bunde.getString("msg").toString();tv.setText(strs);btn.setOnClickListener(new OnClickListener() //绑定注冊button单击事件   {  @Override  public void onClick(View arg0) {  // button跳转   Intent  data= new Intent(); data.putExtra("returnmsg",returnText.getText().toString());setResult(Activity.RESULT_OK,data);finish();}  });  }
}

这里有几个方法我们简单说明下:
                             startActivityForResult 假设父页面想接受子页面的返回值,使用这种方法启动子页面。方法第一个參数代表启动的子页面信息。第二个參数代表请求码,

是整数类型。 请求码requestCode的意思是假设一个子页面有多个父页面能够启动,通过请求码能够推断来自哪个父页面。

onActivityResult(int requestCode, int resultCode, Intent data) 用来接收父页面返回的内容的方法,第一个參数是请求码,第二个參数是返回结果标识resultCode, resultCode主要用来推断哪个子页面返回。 第三个參数就是返回数据

setResult(Activity.RESULT_OK,data); 这种方法主要用于子页面。第一个參数是返回标识,第二个參数是返回数据

下载:http://download.csdn.net/detail/zx13525079024/8136253

.Net程序猿玩转Android开发---(11)页面跳转相关推荐

  1. .Net程序猿玩转Android开发---(7)相对布局RelativeLayout

                 相对布局RelativeLayout是Android布局中一个比較经常使用的控件,使用该控件能够布局出适合各种屏幕分辨率的布局,RelativeLayout採用相对位置进行 ...

  2. 刷题小程序【程序猿面试宝典】开发(二)| 页面创建、页面配置、全局配置

    文章目录 1.创建页面 2.设置 tabBar 3.设置全局配置 window 4.设置页面相关配置 5.自定义全局CSS样式 6.自定义公共class样式 7.小试牛刀,全局设置页面背景色 8.结束 ...

  3. SAP UI5 应用开发教程之八十二 - 采用 OPA5 开发支持页面跳转的 SAP UI5 集成测试用例试读版

    一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 应用开发教程之一:Hello World SAP UI5 应用开发教程之二:SAP U ...

  4. 82. 采用 OPA5 开发支持页面跳转的 SAP UI5 集成测试用例

    SAP UI5 应用开发教程之八十二 - 采用 OPA5 开发支持页面跳转的 SAP UI5 集成测试用例 本教程的前一步骤,我们介绍了如何使用 OPA5 对一个包含表格控件的 SAP UI5 视图进 ...

  5. Android开发11年,分享一下我眼中程序员的三六九等,太现实了

    高级 对于高级,就不仅限于功能和业务开发,还需要深入理解Android系统的运行原理,达到融会贯通,部分可够作为Android端架构师.这一阶段的人大多工作5-8年,具备开源库设计能力,同时对于And ...

  6. Android开发11年,分享一下我眼中程序员的三六九等,2021最新阿里Android面试流程

    高级 对于高级,就不仅限于功能和业务开发,还需要深入理解Android系统的运行原理,达到融会贯通,部分可够作为Android端架构师.这一阶段的人大多工作5-8年,具备开源库设计能力,同时对于And ...

  7. 刷题小程序【程序猿面试宝典】开发(一)| 项目概述与前期准备

    文章目录 1.项目概述 1.概述与主要功能 2.实现技术 2.前期准备 1.注册微信小程序 2.创建普通小程序项目 3.开通云服务 4.测试云服务 5.优化小程序目录结构 3.本期源码 微信公众号[C ...

  8. Java程序员如何转Android开发

    最近几日偷偷的发现部分Java程序员想转安卓开发,故此加紧补充知识,为大家搜集资料,积极整理前人的经验,希望可以给正处于困惑中的你,带来些许的帮助. 啰哩啰嗦的说说Java和Android程序的区别: ...

  9. 作为一个程序员怎么通过android开发赚钱

    上面是一个程序员通过Android开发每天的收入,信则有! 自己学安卓差不多,有一年了.我本来是从事javaweb开发的,可能学习安卓上手会快点.

最新文章

  1. 苹果竟放出“流氓” APP
  2. Linux多进程开发(三)进程创建之守护进程的学习
  3. 如何解决get和post乱码问题?
  4. GDCM:LCNumeric的测试程序
  5. PHP+AJAX 投票器功能
  6. img设置宽高不生效_便宜 好用 不掉盘 保姆级粒子云刷机攻略
  7. solr7.4.0+mysql+solrj(简而优美)
  8. winform的UI设计关键属性汇总
  9. 摩根溪创始人:Coinbase应该购买纽约证券交易所
  10. 【树莓派之旅】第01期:一根网线搞定树莓派可视化界面
  11. delphi 访问https 接口
  12. 目标检测经典算法集锦
  13. 夜间红外图像行人检测学习笔记
  14. 接口先决条件失败412
  15. 万字详解加拿大央行CBDC分析报告
  16. Windows Defender无法关闭的红叉
  17. Data truncation: Data too long for column 'xxx' at row 1
  18. 多个containers 共用一个pvc_长兴PVC废料回收一斤多少钱,ps废料回收
  19. Xen Introduction
  20. CentOS 7.3上图数据库Neo4j的安装和测试

热门文章

  1. MYSQL—— 启动MYSQL 57 报错“The service MYSQL57 failed the most recent........等”的问题解决方式!...
  2. 使用mysql innodb 使用5.7的json类型遇到的坑和解决办法
  3. Servlet 3.0对上传的支持
  4. HDU-4035 Maze 概率DP
  5. 汇编和c语言函数的参数传递,c文件汇编后函数参数传递的不同之处
  6. java 连接ftp 测试_ftp,ftp怎么进行连接,附上在Java环境下的配置教程
  7. 护考人机对话用计算机吗,2018护士执业资格考试人机对话怎么考 有什么注意事项...
  8. linux nmon 进程io,Linux服务器用iotop命令分析服务器磁盘IO情况
  9. java horizontalbarchart_Android-MPChart,HorizontalBarChart 水平柱状图颜色渐变
  10. java 示例_Java最终关键字示例