【正文】

这里以按钮实现活动跳转为例,为实现这个功能,我们需要三个步骤:

1.点击按钮才发生页面跳转,因此,第一步我们先要找到要点击的按钮

如何拿到按钮对象呢?通过资源id,前面我们提到过,在R.id.xxx 中会有我们的资源id,但button按钮是在layout 中创建的,系统不会为其创建资源id,所以我们需要在layout 设置 button 时自己加上id,、,具体方法如下:

在Activity_main.xml中

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点我点我!"
android:textSize="25sp"/>

可以看到设置id 的方法是 id = "@+id/button1",这里button1 即我们将使用的资源id。

2.找到按钮之后,点击按钮之后才会发生跳转,所以我们需要给这个按钮绑定事件监听器

3.当有点击事件产生后,事件监听器就会监听到点击事件,然后去回调事件监听其中的onClick方法实现跳转

package cn.com.farsight.activity02;

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; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); /*
* 点击一个按钮,完成从一个页面跳转到另外一个页面
*/ // 1.点击按钮才发生页面跳转,因此,第一步我们先要找到要点击的按钮
Button button = (Button) findViewById(R.id.button1); // 2.找到按钮之后,点击按钮之后才会发生跳转,所以我们需要给这个按钮绑定事件监听器 button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//3.当有点击事件产生后,事件监听器就会监听到点击事件,然后去回调事件监听其中的onClick方法
//在这里,我们就需要完成页面跳转了
//构建了一个Intent对象,来完成页面跳转
Intent intent = new Intent(MainActivity.this, Second.class);
startActivity(intent);
}
});
}
}

二、使用 Intent 实现活动的显示跳转

这里我们以按钮实现活动跳转为例,为实现这个功能,我们需要三个步骤:

1、拿到按钮对象

如何拿到按钮对象呢?通过资源id,前面我们提到过,在R.id.xxx 中会有我们的资源id,但button按钮是在layout 中创建的,系统不会为其创建资源id,所以我们需要在layout 设置 button 时自己加上id,、,具体方法如下:

[java] view plain copy

 
  1. <Button
  2. android:id="@+id/button1"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:text="点我点我!"
  6. android:textSize="25sp"/>

可以看到设置id 的方法是 id = "@+id/button1",这里button1 即我们将使用的资源id。

2、为此按钮设定点击监听事件

这样每当点击按钮时,就会执行监听器中的onClick()方法,我们只需要在这个方法中加入待处理的逻辑就行了;

具体代码如下:

[java] view plain copy

 
  1. public class MainActivity extends Activity {
  2. private Button button;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. button = (Button) findViewById(R.id.button);
  8. button.setOnClickListener(new OnClickListener() {
  9. @Override
  10. public void onClick(View v) {
  11. // 在此处添加逻辑
  12. }
  13. });
  14. }
  15. }

3、实现跳转

当然这是最重要的一步了,通过Intent 实现,我们先来了解一下Intent 函数;

Intent  意图,告诉系统我们要干什么,连接四大组件的纽带,可以启动活动、启动服务、发送广播;

公共构造函数:

1)、Intent() 空构造函数

2)、Intent(Intent o) 拷贝构造函数

3)、Intent(String action) 指定action类型的构造函数

4)、Intent(String action, Uri uri) 指定Action类型和Uri的构造函数,URI主要是结合程序之间的数据共享ContentProvider

5)、Intent(Context packageContext, Class<?> cls) 传入组件的构造函数,也就是上文提到的

6)、Intent(String action, Uri uri, Context packageContext, Class<?> cls) 前两种结合体

Intent有六种构造函数,3、4、5是最常用的,并不是其他没用!

Intent(String action, Uri uri)  的action就是对应在AndroidMainfest.xml中的action节点的name属性值。在Intent类中定义了很多的Action和Category常量。

下面,我们来具体实现:

1)、创建Intent 对象

[java] view plain copy

 
  1. Intent intent = new Intent();

2)、把我们的意图封装进Intent 对象中

这里我们需要先了解 context :应用程序上下文,就是表示当前对象的一个语境,访问全局信息 的API

这里使用了Intent 的 setclass 方法,我们来看看其定义:

[java] view plain copy

 
  1. /**
  2. * Convenience for calling {@link #setComponent(ComponentName)} with the
  3. * name returned by a {@link Class} object.
  4. *
  5. * @param packageContext A Context of the application package implementing
  6. * this class.
  7. * @param cls The class name to set, equivalent to
  8. *            <code>setClassName(context, cls.getName())</code>.
  9. *
  10. * @return Returns the same Intent object, for chaining multiple calls
  11. * into a single statement.
  12. *
  13. * @see #setComponent
  14. */
  15. public Intent setClass(Context packageContext, Class<?> cls) {
  16. mComponent = new ComponentName(packageContext, cls);
  17. return this;
  18. }

这里 packageContext 即我们现在的 activity ,而Class<?> cls 则是我们的目的activity ,我们看看具体实现:

[java] view plain copy

 
  1. intent.setClass(MainActivity.this,SecondActivity.class);

3)告诉系统执行操作

[java] view plain copy

 
  1. startActivity(intent);

实现这三步就能基本实现活动的跳转了;

使用Intent实现Activity的显式跳转的更多相关文章

  1. 使用Intent实现Activity的隐式跳转

    相比于显式Intent,隐式Intent 则含蓄了许多,它并不明确指出我们想要启动哪一个活动,而是指定了一系列更为抽象的action 和category 等信息,然后交由系统去分析这个Intent,并 ...

  2. Activity的显式跳转和隐式挑战

    安卓中Activity的跳转几乎是每一个APP都会用到的技术点.而且他的使用时十分简单的. 这里我们先说一下主要的技术要点: 1.在清单文件中注册新的Activity 2.通过意图跳转 这里我们看一下 ...

  3. 第一行Kotlin系列(二)Intent隐式显式跳转及向下传值

    1.Intent显式跳转页面 val button5 = findViewById<Button>(R.id.mButton5) button5.setOnClickListener { ...

  4. [转]Activity详解 Intent显式跳转和隐式跳转

    Activity 生命周期             显式 Intent 调用          1     //创建一个显式的 Intent 对象(方法一:在构造函数中指定)  2      Inte ...

  5. 【Android】6.0 添加Menu菜单组件、Intent启动活动、显式Intent、隐式Intent

    1.0 在helloworld项目基础上创建活动SecondActivity: 2.0 其中main.xml: <?xml version="1.0" encoding=&q ...

  6. 安卓--使用Intent实现Activity之间传值与跳转

    http://blog.csdn.net/cjjky/article/details/6337447 在一个Android的应用程序中,很少只存在一个Activity,一般都有多个Activity,如 ...

  7. 使用Intent实现Activity之间传值与跳转(转)

    转:http://blog.csdn.net/cjjky/article/details/6337447 在一个Android的应用程序中,很少只存在一个Activity,一般都有多个Activity ...

  8. Activity跳转显式方法及隐式方法

    1 public class AActivity extends AppCompatActivity { 2 private Button btnJump; 3 @Override 4 protect ...

  9. (转载)Android理解:显式和隐式Intent

    Intent分两种:显式(Explicit intent)和隐式(Implicit intent). 一.显式(设置Component) 显式,即直接指定需要打开的activity对应的类. 以下多种 ...

随机推荐

  1. js apply

    1.作用 函数的apply方法的作用与call方法类似,也是改变this指向,然后再调用该函数.唯一的区别就是,它接收一个数组作为函数执行时的参数 Fn.apply(obj, [arg1, arg2, ...

  2. 解决Mac上Android开发时adb连接不到手机问题

    今天在Mac OS上进行Android开发的时候,打开eclipse连接不到手机MX4问题 1. 插入手机打开 Terminal,输入 system_profiler  SPUSBDataType 2 ...

  3. connect network is unreachable 解决办法

    详细教程见:http://blog.csdn.net/liukun321/article/details/6662950 1.虚拟机中的centos系统要连接外网,我们需要使用桥接网络 2.正常配置e ...

  4. 较详细的sqlserver数据库备份、恢复(转)

    C#实现SQL数据库备份与恢复 有两种方法,都是保存为.bak文件.一种是直接用Sql语句执行,另一种是通过引用SQL Server的SQLDMO组件来实现: .通过执行Sql语句来实现 注意,用Sq ...

  5. 我牵头,你做事——C#委托实践

     我牵头,你做事——C#委托实践一 2007-09-05 23:54:54 标签:委托 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http ...

  6. jQuery图片提示示例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Python文件处理之文件指针(四)

    当我们读取文件内容时,并不能重复的读取,比如一个blogCblog.txt文件里有blogCblog内容,用两个read()方法读取blogCblog.txt的内容,会发现,第一个返回文件内容,第二个 ...

  8. powerpoint取色器有什么用|ppt取色器使用教程

    在使用powerpoint过程中常常发现一些功能我们很少用到,其实是自己不会用的原因,关于powerpoint取色器有什么用呢?接下来我们一起来学一下ppt取色器使用教程. powerpoint取色器 ...

  9. Centos6.x 64位 安装JDK

    JDK下载地址: http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk7-downloads-1880260-zhs.html ...

  10. spring restful 中文乱码问题

    进行如下配置: @RequestMapping( value="/zzs/xgm", produces="application/json;charset=utf-8&q ...