您的位置:首页 > 职场人生

.Net程序员玩转Android开发---(11)页面跳转

2014-11-26 14:35 316 查看
在任何程序开发中,都会遇到页面之间跳转的情况,Android开发也不例外.这一节,我们来认识下Android项目中怎样进行页面跳转.页面跳转分为有参数和无参数页面跳转,已经接受另一个页面的返回值等。Android中页面跳转常用到的是Intent ,但是Intent不仅用做页面跳转,还可以做其他事情,例如拨打电话,发送短信,调用其他程序等。这节我们主要认识下怎样通过Intent进行页面跳转.


1.页面跳转

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

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

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

下面我们看下这个效果

启动页面



[html] view
plaincopy





<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>

[java] view
plaincopy





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() //绑定注册按钮单击事件

{

@Override

public void onClick(View arg0) {

// 按钮跳转

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;

}

}

目标页面

[html] view
plaincopy





<?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页面布局和代码

[html] view
plaincopy





<?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>

[java] view
plaincopy





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;//发送按钮

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() //绑定注册按钮单击事件

{

@Override

public void onClick(View arg0) {

// 按钮跳转

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页面布局和代码

[html] view
plaincopy





<?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>

[java] view
plaincopy





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() //绑定注册按钮单击事件

{

@Override

public void onClick(View arg0) {

// 按钮跳转

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: