在不论什么程序开发中,都会遇到页面之间跳转的情况,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" > <Button
android: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;
@Override
protected 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);
} }); } @Override
public 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;//发送button
private 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开发---(3)登陆页面布局

    这一节我们来看看登陆页面如何布局.对于刚接触到Android开发的童鞋来说.Android的布局感觉比較棘手.须要结合各种属性进行设置,接下来我们由点入面来 了解安卓中页面如何布局,登陆页面非常eas ...

  2. .Net程序猿玩转Android开发---(8)表格布局TableLayout

    表格布局TableLayout是Android中比較经常使用的一个布局控件,既然是表格,肯定有行和列,TableLayout中的行有TableRow组成.列依据每行控件的数量来确定 假如第一行有3个控 ...

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

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

  4. .Net程序猿玩转Android开发---(6)线性布局LinearLayout

                                LinearLayout控件是Android中重要的布局控件,是一个线性控件,所谓线性控件的意思是指该控件里面的内容仅仅能水平或垂直排列.也就 ...

  5. .Net程序员玩转Android开发--ListView单击事件

    public class ListViewClickActivity extends Activity {         private ListView lv;        SimpleAdap ...

  6. Android开发之页面跳转传递list集合

    这篇随笔这里详细记录两个activity之间如何传递list集合中的数据. 1.首先要对javabean进行序列化处理,即实现Serializable. package com.anhua.bean; ...

  7. .Net程序员玩转Android系列之一~Java快速入门

    前言 前段时间受公司业务发展需要,探索性进入Android开发领域.一切从零开始,java基础,Java进阶,Android框架学习,Eclipse熟悉,最终到第一个即时通讯App完成,历经一个月的时 ...

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

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

  9. Android开发 navigation的跳转动画实现

    前言 此篇博客只简短的介绍navigation如何添加跳转页面的动画属性,如果你还为接触了解过navigation.建议你看我另一篇博客Android开发 navigation入门详解 创建动画xml ...

随机推荐

  1. [置顶] 利用Global.asax的Application_BeginRequest 实现url 重写 无后缀

    利用Global.asax的Application_BeginRequest 实现url 重写 无后缀 <%@ Application Language="C#" %> ...

  2. 开学了!这些Linux认证你要知道

    大家好,今天我们将认识一些非常有价值的全球认可的Linux认证.Linux认证是不同的Linux专业机构在全球范围内进行的认证程序.Linux认证可以让Linux专业人才可以在服务器领域或相关公司等等 ...

  3. hadoop fs:du & count统计hdfs文件(目录下文件)大小的用法

    hadoop fs 更多用法,请参考官网:http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_shell.html 以下是我的使用hadoop fs -du统计文 ...

  4. C++的四种初始化形式以及类型转换

      C++中有如下的方式来初始化一个变量. 但当进行类型转换时,只有两种方式可用,其他两种方式会报错.

  5. 8. 利用反射机制, ListArray,intent来实现多Activity的切换

    package com.example.thenewboston; import android.app.ListActivity; import android.content.Intent; im ...

  6. 深入研究嵌入式web服务器的视频监控应用

    http://embed.chinaitlab.com/pc/776136.html uCLinux下,主要有3个Web Server:Httpd.Thttpd和BOA.Httpd是最简单的一个Web ...

  7. php之快速入门学习-13(PHP 循环 - While 循环)

    PHP 循环 - While 循环 循环执行代码块指定的次数,或者当指定的条件为真时循环执行代码块. PHP 循环 在您编写代码时,您经常需要让相同的代码块一次又一次地重复运行.我们可以在代码中使用循 ...

  8. Effective C++ 38-42

    38.绝不要又一次定义继承而来的缺省參数值. 又一次定义函数缺省參数值意味着又一次定义函数.而非虚函数不能又一次定义,所以将就考虑不能又一次定义虚函数的缺省參数值的原因:虚函数是动态绑定的而缺省參数值 ...

  9. 玩转oracle学习第五天

     1.上节回想 2.维护数据的完整性 3.管理索引 4.管理权限和角色 1.掌握维护oracle数据完整性的技巧  2.理解索引的概念,会建立索引  3.管理oracle的权限和角色   介绍:维 ...

  10. Ubuntu12.04+OpenERP7.0安装笔记

    不经意的一次看到OpenERP这个开源ERP,就被其丰富的功能,简洁的画面,熟悉的语言所吸引.迫不及待的多方查询资料,自己架设一个测试环境来进行了解.以下为测试安装时候的步骤说明,以备查询,并供有需要 ...