Activity基本跳转
详细解释: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基本跳转的更多相关文章
- Android课程---Activity的跳转与传值(转自网上)
Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据. Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...
- Android activity界面跳转动画
实现activity界面跳转动画 1.在startActivity方法之后加入: overridePendingTransition(R.anim.pull_in_right, R.anim.pull ...
- Activity的跳转与传值(转载)
Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据. Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...
- android入门:activity之间跳转,并且回传参数
介绍: 两个activity进行跳转,在跳转过程中,将message由MainActivity传递到secondActivity,并且当secondActivity退回至MainAct ...
- Activity的跳转与传值
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://android.blog.51cto.com/268543/323982 Acti ...
- Activity A 跳转到Activity B 生命周期
又被生命周期折磨了一段时间,这次是被onPause 和 onStop 折磨了,一直认为Activity A 跳转到到 Activity B的生命周期是onPause(A),onStop(A),onCr ...
- Android Activity间跳转与传递数据
1 概述 Activity之间的跳转主要使用 startActivity(Intent intent); startActivityForResult(Intent intent,int reques ...
- 5.10学习总结——Activity的跳转和传值
使用sharedpreference是对信息的存储,也可以进行传值,今天通过查找资料,学习了Activity的跳转和传值方法. 跳转 1.显示跳转 4种方法 1 2 3 4 5 6 7 8 9 10 ...
- 关于从Activity A跳转到Activity B ,其中Activity A中有一个VideoView,Activity B中有一个MediaPlayer。
两个不同的视频的跳转, 前面我是在onStop()方法中销毁VideoView(因为MediaPlayer是全局共用的,而VideoView内包含MediaPlayer),但是每次进入Activity ...
随机推荐
- eclipse转idea, 快捷键设置
设置快捷键的途径: 打开idea的配置,找到Keymap,设置为eclipse 另外还要手动设置某些快捷键 上下移动 点击类打开 代码提示 查询 重命名 快速实现接口 回到上一次光标处
- .net mvc控制器传递方法到视图
很多人都是在视图里面定义方法,然后再使用.我个人也是这么干的.但是为了验证是否可以将方法从控制器传递到视图,所以做了个测试.结果真的可以.原理是利用了委托(delegate),因为委托本身就是一种类型 ...
- [Dynamic Language] Python非子包引用
Python非子包引用 python的搜索路径其实是一个列表(sys.path) 导入模块时python会自动去找搜索这个列表当中的路径,如果路径中存在要导入的模块文件则导入成功. 在项目中如果要引用 ...
- HDU 2089 不要62(数位DP·记忆化搜索)
题意 中文 最基础的数位DP 这题好像也能够直接暴力来做 令dp[i][j]表示以 j 开头的 i 位数有多少个满足条件 那么非常easy有状态转移方程 dp[i][j] = sum{ dp[ ...
- MVC扩展ModelBinder,通过继承DefaultModelBinder把表单数据封装成类作为action参数
把视图省.市.街道表单数据,封装成一个类,作为action参数.如下: action方法参数类型: namespace MvcApplication1.Models{ public class ...
- ARC 特性
ResultVC *myResultVC = [[ResultVC alloc]initWithNibName:@"ResultVC" bundle:nil]; [self.vie ...
- Accepting PayPal in games(完整的Paypal在Unity的支付)
Hello and welcome back to my blog! In this article I’m going to talk about the process of acceptin ...
- git打pach包
在开发中,我们发出的基线版本号常常会有一些bug须要修复,假设採用本地上库,然后再给用户新的版本号,可能会费时费力,而假设给用户我们改动后的代码让用户一行一行合入本地,也显的比較落后,假设用户那边也使 ...
- Android图片加载框架最全解析(八),带你全面了解Glide 4的用法
本篇将是我们这个Glide系列的最后一篇文章. 其实在写这个系列第一篇文章的时候,Glide就推出4.0.0的RC版了.那个时候因为我一直研究的都是Glide 3.7.0版本,再加上RC版本还不太稳定 ...
- MATLAB SVM
clc;clear;close all; traindata = [1,0; 3,10; 2,2; 2,3; -1,-1; -6,-4; -4,-1; -1.5, -3];group = [1 1 1 ...