详细解释: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. windows下配置ssh(FreeSSHD + putty)

    windows下配置ssh(FreeSSHD + putty): 1.关于配置过程找到一篇很好的博客,推荐大家先好好看一下,这篇博文解决了大方向问题. 地址:http://blog.csdn.net/ ...

  2. ASP.NET与MVC架构区别总结

    1)ASP.NET Webforms Behind Code利于快速开发,方便可视化操作. 2)ASP.NET 使用了“基于视图”的解决方案去应对“基于行为”的需求,它处理了客户端的请求,IIS将请求 ...

  3. Hibernate-数据库更新操作

    /* Session接口下操作存在以下问题: 数据更新操作: 1.更新的时候必须要有主键; 2.若只更新部分字段内容,则未设置的字段将被设置为Null(全表更新) 3.update()没有返回值,即不 ...

  4. ZOJ3673:1729

    1729 is the natural number following 1728 and preceding 1730. It is also known as the Hardy-Ramanuja ...

  5. ELMAH--Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components 77 out of 90 rated th

    MSDN===http://msdn.microsoft.com/en-us/library/aa479332.aspx PROJECT==https://code.google.com/p/elma ...

  6. 使用jquery加载部分视图02-使用$.ajax()

    本篇体验使用$.ajax()加载部分视图.与加载部分视图相关的包括: RenderPartial和RenderAction区别   使用jquery加载部分视图01-使用$.get()       □ ...

  7. 【docker】关于docker中挂载的解释

    现在有这么一个命令: docker run -p 33061:3306 --name mysql --restart=always -e MYSQL_ROOT_PASSWORD=pisen -v /e ...

  8. Android平台上优秀的开源项目

    软件名:gaeproxy 软件作用:Android手机配置GoAgent. 项目地址:https://github.com/madeye/gaeproxy.git 软件名:ProxyDroid 软件作 ...

  9. Android之Volley使用

    转自:http://blog.csdn.net/lfdfhl/article/details/12223345 稍微做了一点儿修改 /** * 利用NetworkImageView显示网络图片 */ ...

  10. MySql_34道经典Sql试题

    MySql_34道经典Sql试题   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xiaouncle/article/details/799390 ...