详细解释: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. dll文件反编译,c#、vb动态库反编译

    最近开发遇到一个项目,对方提供一个c#编写的动态库,图片处理需要调用该动态库方法,发现一张图片处理起来需要5s时间,对方无法提供有效解决手段,抱着试一试的想法反编译的对方的动态库,发现其中问题. 一下 ...

  2. UVALive 4425 Another Brick in the Wall 暴力

    C - Another Brick in the Wall Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  3. SyncTrayzor -- Windows tray utility / filesystem watcher / launcher for syncthing

    SyncTrayzor is a little tray utility for Syncthing on Windows. It hosts and wraps Syncthing, making ...

  4. PostgreSQL SystemTap on Linux 转

    PostgreSQL 支持动态跟踪, 可以通过dtrace或者systemtap工具统计相关探针的信息. 安装systemtap yum install systemtap kernel-debugi ...

  5. MYSQL 源代码编绎脚本

    http://blog.csdn.net/hopingwhite/article/details/5808101

  6. CHANGE USER WHEN I CONNECT TO TEAM FOUNDATION SERVER

    Question: I USE TEAM EXPLORER TO CONECT TO TEAM FOUNDATION SERVER 2010, BUT I DO NOT LOGIN OUT, AND ...

  7. 使用Bootstrap 3开发响应式网站实践02,轮播

    本篇体验图片轮播.html部分为: <div class="carousel slide" id="myCarousel" > <!--Ind ...

  8. Android SDK目录及版本号区别

    来自:http://www.2cto.com/kf/201604/496917.html 今天又有人问Tools,Build-Tools,Platform-tools有什么区别,是干嘛的? 现在对SD ...

  9. Ext.Toolbar.Fill()

    tbar : ['-',new Ext.form.Label({ text : '产品代码:' }),new Ext.form.TextField({ id : 'cpdm', name : 'cpd ...

  10. FreeCMS开发过程问题总结(持续更新中)

    正在做freecms的二次开发,特对开发过程中遇到的问题及原因分析进行总结分享,共勉. 2014/4/25 错误提示:静态化处理失败,原因:Expression answerList is undef ...