详细解释:http://blog.csdn.net/xiazdong/article/details/7664757

简单介绍activity的跳转,通过intent实现,详细的注释在代码中。涉及到activity跳转,intent调用,button事件处理,xml文件编辑等知识。麻雀虽小,五脏俱全!废话少说,开始了......

先看一下运行效果图:

点击first跳转到第二个activity

点击second会返回到第一个activity,并带回返回信息,如下图:

1 新建project,大家注意我的命名,前面都加前缀了,对于初学者来说,可以在程序运行时明白这些变量的实际命名到那儿去了,运行时大家可以再返回看这些名字!

2 开发时一般先设置界面,打开main.xml文件

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

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="第一个Activity"

/>

<Button

android:id="@+id/callButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="first"

/>

</LinearLayout>

3 设置另外一个activity,我们的效果是点击上面的buttong时跳转到地第二个activity.利用android xml 向导建立一个xml文件,利用向导的好处是很多默认值都已经给设置好了。

注意这个文件名,就是等会我们要设置的类名。

这个文件的内容如下

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

>

<TextView

android:id="@+id/secondTextview"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="第二个Activity"

/>

<Button

android:id="@+id/secondButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="second"

/>

</LinearLayout>

 

4 新建类secondActivity.java。注意超类的选择

这个类的代码如下

package com.fly.intent;

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.TextView;

public class secondActivity extends Activity {

public static final int RESULT_CODE=1;

private TextView tv=null;

private Button btnSecondButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.second);

//从第一个activity接收参数

Intent intent = getIntent();

Bundle bundle = intent.getExtras();

String str = bundle.getString("str");

tv=(TextView)findViewById(R.id.secondTextview);

tv.setText(str);//显示接收到参数

//点击时返回参数到第一个activity

btnSecondButton = (Button)findViewById(R.id.secondButton);

btnSecondButton.setOnClickListener(listener);

}

private OnClickListener listener = new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent = new Intent();

intent.putExtra("back", "come from second activiy");

setResult(RESULT_CODE, intent);

finish();

}

};

}

 

5 将这个新建的activity要加入到AndroidManifest.xml,非常重要!

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.fly.intent"

android:versionCode="1"

android:versionName="1.0">

<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".IntentDemoActivity"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name=".secondActivity"

android:label="second Activity">

</activity>

</application>

<uses-permission android:name="android.permission.CALL_PHONE"/>

</manifest>

至此界面的设置工作就完成了,接下来就需要设置button的事件了。

6 打开IntentDemoActivity.java文件,输入下面的代码

package com.fly.intent;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class IntentDemoActivity extends Activity {

private Button callButton = null;

private static final int REQUEST_CODE=1;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

callButton = (Button)findViewById(R.id.callButton);

callButton.setOnClickListener(listener);

}

private OnClickListener listener = new OnClickListener()

{

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent = new Intent();

intent.setClass(IntentDemoActivity.this, secondActivity.class);

intent.putExtra("str", "come from first activity");

//startActivity(intent);//无返回值的调用,启动一个明确的activity

startActivityForResult(intent, REQUEST_CODE);

/*调用打电话的intent,启动一个未指明的activity,由android去寻找

* Intent intent = new Intent();

intent.setAction(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:10086"));

startActivity(intent);

Log.i("aa", "bb");*/

}

};

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

super.onActivityResult(requestCode, resultCode, data);

if(requestCode == REQUEST_CODE)

{

if(resultCode==secondActivity.RESULT_CODE)

{

Bundle bundle = data.getExtras();

String str = bundle.getString("back");

Toast.makeText(IntentDemoActivity.this,str,Toast.LENGTH_LONG).show();

}

}

}

}

Activity基本跳转的更多相关文章

  1. Android课程---Activity的跳转与传值(转自网上)

    Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据.   Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...

  2. Android activity界面跳转动画

    实现activity界面跳转动画 1.在startActivity方法之后加入: overridePendingTransition(R.anim.pull_in_right, R.anim.pull ...

  3. Activity的跳转与传值(转载)

    Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据.   Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...

  4. android入门:activity之间跳转,并且回传参数

    介绍:         两个activity进行跳转,在跳转过程中,将message由MainActivity传递到secondActivity,并且当secondActivity退回至MainAct ...

  5. Activity的跳转与传值

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://android.blog.51cto.com/268543/323982 Acti ...

  6. Activity A 跳转到Activity B 生命周期

    又被生命周期折磨了一段时间,这次是被onPause 和 onStop 折磨了,一直认为Activity A 跳转到到 Activity B的生命周期是onPause(A),onStop(A),onCr ...

  7. Android Activity间跳转与传递数据

    1 概述 Activity之间的跳转主要使用 startActivity(Intent intent); startActivityForResult(Intent intent,int reques ...

  8. 5.10学习总结——Activity的跳转和传值

    使用sharedpreference是对信息的存储,也可以进行传值,今天通过查找资料,学习了Activity的跳转和传值方法. 跳转 1.显示跳转 4种方法 1 2 3 4 5 6 7 8 9 10 ...

  9. 关于从Activity A跳转到Activity B ,其中Activity A中有一个VideoView,Activity B中有一个MediaPlayer。

    两个不同的视频的跳转, 前面我是在onStop()方法中销毁VideoView(因为MediaPlayer是全局共用的,而VideoView内包含MediaPlayer),但是每次进入Activity ...

随机推荐

  1. 【10.17校内测试】【二进制数位DP】【博弈论/预处理】【玄学(?)DP】

    Solution 几乎是秒想到的水题叻! 异或很容易想到每一位单独做贡献,所以我们需要统计的是区间内每一位上做的贡献,就是统计区间内每一位是1的数的数量. 所以就写数位dp辣!(昨天才做了数字统计不要 ...

  2. Shell 学习笔记之条件语句

    条件语句 if # if if condition then command fi # if else if condition then command else command fi # if e ...

  3. 5、Redis中对Set类型的操作命令

    写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- ------------ ...

  4. mysqlslap 一个MySQL数据库压力测试工具

    在Xen/KVM虚拟化中,一般来说CPU.内存.网络I/O的虚拟化效率都非常高了,而磁盘I/O虚拟化效率较低,从而磁盘可能会是瓶颈.一般来说,数据库对磁盘I/O要求比较高的应用,可以衡量一下在客户机中 ...

  5. js跨域请求提示函数未定义的问题

    我的代码是这么写的 window.onload=function(){ function sendRequest(){ var script=document.getElementById(" ...

  6. systemtap 2.8 安装说明书

    systemtap: a linux trace/probe tool Visit the project web site at <http://sourceware.org/systemta ...

  7. MVC打印表格,把表格内容放到部分视图打印

    假设在一个页面上有众多内容,而我们只想把该页面上的表格内容打印出来,window.print()方法会把整个页面的内容打印出来,如何做到只打印表格内容呢? 既然window.print()只会打印整页 ...

  8. NSLog 不打印中文 - 解决

    解决方案:将项目的Debugger模式设置为 GDB 即可.(LLDB下不打印中文) 第一步: 第二步:

  9. DirectX

    DirectX 9.0 Complete Software Development Kit (SDK) :(2002-12-19) 点击下载 DirectX 9.0 SDK Update - (Sum ...

  10. java静态初始化数据

    1.通过静态成员变量和静态方法组合(比较单一) public class A{ private static String t=getInit(); private static String get ...