学了之前的Android控件以及布局,我们就能够做一些UI的设计了,这里我结合之前的知识。以一个小的登录项目来解说下Activity之间跳转。

先看下效果图:

1.登录界面:



2.点击登录按钮跳转到另外一个Activity的界面,这个界面非常easy,就一个TextView:

首先我们须要建好两个Activity和两个xml布局文件,android程序启动会载入開始默认指定的MainActivity.java以及activity_main.xml。我们首先要做的是分别在两个Activity文件里设置好须要载入的xml布局以及须要处理的事情。

这里先直接给出两个xml布局文件的代码,用的是相对布局。须要自己熟悉这样的布局并进行对应的调整,能够參考我之前的博客:

activity_main.xml代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:id="@+id/tv1"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="username"
  10. android:textSize="25sp"/>
  11. <EditText
  12. android:id="@+id/et1"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_toRightOf="@id/tv1"
  16. android:layout_alignBottom ="@id/tv1"
  17. android:hint="请输入username"
  18. android:singleLine="true"/>
  19. <TextView
  20. android:id="@+id/tv2"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_below="@id/tv1"
  24. android:layout_alignRight="@id/tv1"
  25. android:layout_alignLeft="@id/tv1"
  26. android:gravity="center"
  27. android:text="password"
  28. android:textSize="25sp"/>
  29. <EditText
  30. android:id="@+id/et2"
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:layout_toRightOf="@id/tv2"
  34. android:layout_alignBottom ="@id/tv2"
  35. android:hint="请输入password"
  36. android:password="true"
  37. android:singleLine="true"/>
  38. <Button
  39. android:id="@+id/bt1"
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:layout_below="@id/tv2"
  43. android:text="登录"/>
  44. </RelativeLayout>

activity_main.xml2代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:id="@+id/bv3"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:gravity="center"
  10. android:textSize="20sp"
  11. android:text="登录成功" />
  12. </RelativeLayout>

然后对于2个Activity,先在 AndroidManifest.xml 中为 SecondActivity进行注冊:

这里给出AndroidManifest.xml中的代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.androidactivitytiaozhuan"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="16"
  8. android:targetSdkVersion="22" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name=".MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. //注冊SecondActivity,因为不是主活动,因此不须要配置intent-filter标签里的内容
  23. <activity
  24. android:name=".SecondActivity" >
  25. </activity>
  26. </application>
  27. </manifest>

接下来我们学习了解下Intent。

Intent是Android程序中各组件之间进行交互的一种重要方式,它不仅能够指明当前组件想要运行的动作,还能够在不同组件之间传递数据。Intent 一般可被用于启动活动、启动服务、以及发送广播等场景, 这里先仅仅讲下用Intent实现界面的跳转。

有非常多方法实现Activity之间的跳转,这里我仅仅给出一种使用显示的Intent(通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,通常是在同样的应用程序内部实现的。

)方式,很多其它的能够參考浅谈显示Intent和隐式Intent。我们首先构建出了一个 Intent,传入MainActivity.this作为上下文,传入SecondActivity.class 作为目标活动,这样我们的“意图”就非常明显了,即在 MainActivity 这个活动的基础上打 开 SecondActivity 这个活动。

然后通过 startActivity()方法来运行这个 Intent。以下给出两个Activity中的代码。

MainActivity.java代码例如以下:

  1. package com.example.androidactivitytiaozhuan;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. public class MainActivity extends Activity {
  9. private Button button;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. button = (Button) findViewById(R.id.bt1);
  15. button.setOnClickListener(new OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. //显示方式声明Intent。直接启动SecondActivity
  19. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  20. startActivity(intent);
  21. }
  22. });
  23. }
  24. }

SecondActivity.java代码例如以下:

  1. package com.example.androidactivitytiaozhuan;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class SecondActivity extends Activity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main2);
  9. }
  10. }

Android笔记---Intent实现Activity跳转的更多相关文章

  1. Android - 通过Intent启动Activity

    通过Intent启动Activity 本文地址: http://blog.csdn.net/caroline_wendy 为了动态关联Activity界面,使用Intent启动.能够灵活绑定. 在In ...

  2. Android组件系列----当前Activity跳转到另一个Activity的详细过程

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  3. Android之Intent和Activity

    Intent能够说是Android的灵魂,程序跳转和传递数据的时候基本上就是靠Intent了.Intent在Android应用中是相当重要的,理解Intent相应用编程非常有帮助.在Android的官 ...

  4. Android 笔记 Intent and Bundle day7

    学习了Intent与Bundle的使用,进行应用中的交互 package com.example.intent; import android.app.Activity; import android ...

  5. Android学习手记(1) Activity跳转

    新建Project,并将主页命名为MainActivity. 创建一个Activity 在App上“右键->New->Activity->Empty Activity”, 将新建的A ...

  6. [转]android笔记--Intent和IntentFilter详解

    Intent用于启动Activity, Service, 以及BroadcastReceiver三种组件, 同时还是组件之间通信的重要媒介. 使用Intent启动组件的优势1, Intent为组件的启 ...

  7. android笔记--Intent和IntentFilter详解

    本文转载自:https://www.cnblogs.com/liushengjie/archive/2012/08/30/2663066.html 本文转载自:https://www.cnblogs. ...

  8. Android 笔记-Fragment 与 Activity之间传递数据

    Fragment 与 Activity之间传递数据有两种方法.一种是使用setArgument,一种是使用接口回调.以下先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这 ...

  9. android笔记5——同一个Activity中Fragment的切换

    今天来模拟一个注冊的界面过程: 点击"下一步"之后: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZW5zb24xNjg1NQ==/f ...

随机推荐

  1. js实现数组的去重

    function filterRepat(arr){ if(Array.isArray(arr) && arr.length){ var arr = arr.filter(functi ...

  2. 3ds Max做的卡通狗教程

    使用软件::3ds Max 软件下载:http://www.xy3dsmax.com/xiazai.html 全教程完,学完记得交作业.如果本教程对您有所帮助,请推荐给你的朋友.

  3. Ini配置文件操作

    package cn.com.szhtkj.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOE ...

  4. 路飞学城Python-Day15(模块二思维导图)

  5. [USACO16DEC]Cities and States省市

    题目:洛谷P3405. 题目大意:给你一些省市的名称(大写)和所在省的名称(两个大写字母),求有多少对城市满足:A城市的名字的前两个字母等于B城市所在省的名称,且A所在省的名称等于B城市的名字的前两个 ...

  6. 2018秋寒假作业4——PTA编辑总结1

    #include<stdio.h> #include<math.h> int main(void) { int n,i,j,p,m,ge,N,k; char op; ){ sc ...

  7. poj3134 Power Calculus IDA*

    好端端的一道搜索题目,,,硬生生的被我弄成了乱搞题,,,枚举当前的maxd,深搜结果,然而想到的剪枝方法都没有太好的效果,,,最后用一个贪心乱搞弄出来了,,, 贪心:每次必用上一次做出来的数字与其他数 ...

  8. ASP.NET-本地化、全球化

    在<system.web>中加入一个全球化的标识,网站就可以自适应全球化了 也可以将出错信息全球化 上面的这种方式测试过对google浏览器好像没用,但是对IE内核的是可行的,可能goog ...

  9. Java NIO笔记(一):NIO介绍

    Java NIO即Java Non-blocking IO(Java非堵塞I/O),由于是在Jdk1.4之后添加的一套新的操作I/O工具包,所以通常会被叫做Java New IO.NIO是为提供I/O ...

  10. iOS自动布局高级用法 && 纯代码约束写法

    本文主要介绍几个我遇到的总结的高级用法(当然我相信肯定有不少比这还高级的). 简单的storyboard中上下左右约束,固定宽高啥的用法在这里就不做赘述了. autolayout自动布局是iOS6以后 ...