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

先看下效果图:

1.登录界面:



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

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

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

activity_main.xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"
android:textSize="25sp"/>
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tv1"
android:layout_alignBottom ="@id/tv1"
android:hint="请输入username"
android:singleLine="true"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv1"
android:layout_alignRight="@id/tv1"
android:layout_alignLeft="@id/tv1"
android:gravity="center"
android:text="password"
android:textSize="25sp"/>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tv2"
android:layout_alignBottom ="@id/tv2"
android:hint="请输入password"
android:password="true"
android:singleLine="true"/>
<Button
android:id="@+id/bt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv2"
android:text="登录"/>
</RelativeLayout>

activity_main.xml2代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/bv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:text="登录成功" />
</RelativeLayout>

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

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidactivitytiaozhuan"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> //注冊SecondActivity,因为不是主活动,因此不须要配置intent-filter标签里的内容
<activity
android:name=".SecondActivity" >
</activity>
</application>
</manifest>

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

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

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

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

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

MainActivity.java代码例如以下:

package com.example.androidactivitytiaozhuan;

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 {
private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.bt1); button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//显示方式声明Intent。直接启动SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}

SecondActivity.java代码例如以下:

package com.example.androidactivitytiaozhuan;

import android.app.Activity;
import android.os.Bundle; public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}

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. LayUI加载js无效问题

    在部署系统的时候,本地调试一切正常,layer.js均能正常加载.然而部署到服务器之后,经常性的出现layer.js无法加载问题.导致页面弹框无法使用. 一开始以为是Google浏览器问题,因为刚刚更 ...

  2. BZOJ 2119 股市的预测(后缀数组)

    首先要差分+离散化. 然后就是求形如ABA的串有多少,其中B的长度确定为k. 我们用到了设置关键点的思想.我们枚举A的长度L.然后在\(1,1+L,1+L*2,1+L*3...\)设置关键点.然后我们 ...

  3. 紫书 习题 8-2 UVa 1610 (暴力出奇迹)

    这道题我真的想的非常的复杂, 拿草稿纸一直在找规律,推公式, 然后总有一些特殊的情况. 然后就WA了N次.无奈之下看了别人的博客, 然后就惊了.直接暴力枚举两个相邻字符串 里面的所有可能就可以了--真 ...

  4. js 函数基础(方便复习使用)

    // 函数声明: function bbq(){ // ..... } // 函数表达式: // 1.命名函数表达式 var test = function abc(){ document.write ...

  5. HDU 5607 graph(矩阵优化+概率DP)

    该题非常easy想到求概率的转移方程:用d[i][j]表示第i步,走到j点的概率. 可是该题的k高达1e9.所以依照套路.要用矩阵相乘来优化. 第一次写矩阵相乘. 大概的意思就是利用矩阵实现递推. 而 ...

  6. Java8 Lamdba表达式 002

    本篇将讲述lamdba表达式的排序,本例包括一个Player对象的集合[稍后定义],通过每一个player的分数高低对列表的player进行排序.类定义001例如以下 public class Sor ...

  7. C++ double转string类型以及MFC控件简单使用方法

    这两天项目须要,測试c++库里面内容.生成jar再给Android调用.我没有学过C++,如今開始记录C++简单使用方法.測试时候一般都是使用mfc程序来測试.要输入值.显示结果吗.我用的编译环境vs ...

  8. [leetcode][math] Add Digits

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  9. HDU1232 畅通project 并查集

    这道题跟HDU 1213 How Many Tables 并查集很接近,都是赤裸裸的并查集的题. 思路:如果还须要建n-1条路.每并一次就自减1. 參考代码: #include<stdio.h& ...

  10. ★★★【卡法 常用js库】: js汇合 表单验证 cookie设置 日期格式 电话手机号码 email 整数 小数 金额 检查参数长度

    [卡法 常用js库]: js汇合 表单验证  cookie设置  日期格式  电话手机号码  email  整数  小数  金额   检查参数长度 // +---------------------- ...