首先来看一个Activity当中启动另一个Activity,直接上代码说吧:

  (1)首先要多个Activity,那么首先在res-layout下新建一个 Other.xml,用来充当第二个Activity的布局文件

    

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/MyTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

  (2)加入一个 src 下加入一个类文件(java里是这么说的吗,我一直在搞c++,不太清楚java里面的属于怎么说)

    记住类一定要 继承Activity ( public class *** extends Activity ),然后重写 onCreate 方法

    

    

package com.example.cart;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; public class OtherActivity extends Activity
{
private TextView mytext = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.othery); mytext = (TextView)findViewById(R.id.MyTest);
mytext.setText("hello activity");
}
}

    这个Activity呢,其实很简单就是只有一个TextView,内容就是“hello activity”

  (3)接下来我们需要在 AndroidManifest.xml 当中注册这个Activity

    

  (4)我们需要在主的activity当中去调用,具体的做法是 首先在主Activity的布局是只有一个按钮,然后我们监听这个按钮按下事件,

     按下这个按钮就会通过Intent来调用另一个Activity,也就是我们上面创建的Activity,下面是代码:

package com.example.cart;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build; public class MainActivity extends Activity
{
public Button mybutton = null; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mybutton = (Button)findViewById(R.id.MyButton);
mybutton.setText("nihao-ha2ha");
mybutton.setOnClickListener(new MyButtonListen());
} class MyButtonListen implements OnClickListener
{
@Override
/* 如果遇到 aetOnclickListener报错的时候,按照下面来做:
1.把 onClick(DialogInterface arg0, int arg1) 改成 onClick(View v)
2.把 import android.content.DialogInterface.OnClickListener; 改成 import android.view.View.OnClickListener; */
        public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
MainActivity.this.startActivity(intent);
}
}
}

  (5) 运行程序之后,首先进入第一个Activity,点击了按钮了之后调转到了第二个Activity

       

  接下来尝试一个Activity当中启动另一个Activity的时候,传递数据给另一个Activity:

  Intent包含:ComponentName、Action、Data、Extras、Category、Flags,我们就将使用Extra来进行数据的传递

(1)主Activity当中这样调用

            Intent intent = new Intent();
intent.putExtra("testintent", "ccyy");
intent.setClass(MainActivity.this, OtherActivity.class);
MainActivity.this.startActivity(intent);

  (2)接受端是这样的

    protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.othery); Intent intent = getIntent();
String myvalue = intent.getStringExtra("testintent"); mytext = (TextView)findViewById(R.id.MyTest);
mytext.setText("hello activity" + myvalue);
}

  当然,Intent进行数据传递的时候,不仅可以在同一个应用程序的不用Activity直接进行传递,而且可以进行不同应用程序的调用和数据传递:

  下面通过Uri调用发短信的界面来进行说明,代码还是加到刚才的按钮相应中:

            Uri uri = Uri.parse("smsto:13008574656");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "hi,I am a student~");
MainActivity.this.startActivity(intent);

  运行之后,点击按钮进入到发短信的界面:

  

  

  这次学习主要理解了:

  1.多个Activity

  2.Intent的基本作用

  3.一个Activity当中启动另一个Activity

  4.使用Intent在Activity之间传递数据

【Android开发学习笔记】【第三课】Activity和Intent的更多相关文章

  1. android开发学习笔记000

    使用书籍:<疯狂android讲义>——李刚著,2011年7月出版 虽然现在已2014,可我挑来跳去,还是以这本书开始我的android之旅吧. “疯狂源自梦想,技术成就辉煌.” 让我这个 ...

  2. 【Android开发学习笔记】【第五课】Activity的生命周期-上

    今天学习Activity当中的七个生命周期函数: 首先得说一个事情,就是在代码当中如果加入了 System.out.println(" ------");之后,如何查看这里面的输出 ...

  3. 【Android开发学习笔记】【第四课】基础控件的学习

    通过一个简单的例子来学习下面几种控件: 1.TextView:简单的文本显示控件 2.EditText:可以编辑的文本框 3.Button:按钮 4.Menu:这里指的是系统的Menu 5.Toast ...

  4. Android开发学习笔记:浅谈GridView

    GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示图片,图片等内容,比如实现九宫格图,用GridView是首选,也是最简单的.主要用于设置Adapter. GridView常用的X ...

  5. android开发学习笔记系列(2)-android应用界面编程

    前言 本篇博客将会简要介绍andriod开发过程中的一些界面元素和编程的实现,我将大家走进安卓的XML世界,当然可能会涉及到java代码,当然本文主要是介绍XML文件的界面布局. 那么我们的XML存在 ...

  6. 【转】Android开发学习笔记(一)——初识Android

    对于一名程序员来说,“自顶向下”虽然是一种最普通不过的分析问题和解决问题的方式,但其却是简单且较为有效的一种.所以,将其应用到Android的学习中来,不至于将自己的冲动演变为一种盲目和不知所措. 根 ...

  7. Android开发学习笔记:Intent的简介以及属性的详解【转】

    一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...

  8. Android开发学习笔记--计时器的应用实例

    为了了解安卓计时器的用法,写了一个秒表的应用,正是这个秒表,让我对Android应用的速度大跌眼镜,我设置了一个计时器,10ms更新一次显示的时间,然后更标准的时间一比较发现,跑10s就有一秒的时间误 ...

  9. Android开发学习笔记--一个有界面A+B的计算器

    做了一个A+B的APP,虽然很简单,但是作为初学者还是弄了几个小时才弄好,什么东西都要看书或者百度,但最后成功了,还是很开心的,收货蛮大的.现在把过程写一下: 首先给出效果图: 一开始布局一直有问题, ...

随机推荐

  1. js:语言精髓笔记3----语句

    JS语句分类:(注意语句都是有返回值的) 声明语句: 变量声明语句: 标签声明语句: 函数声明语句: 表达式语句:(表达式加分号) 变量赋值语句:具有声明一个变量的隐式效果: 函数调用语句; 属性赋值 ...

  2. UVa10917 A Walk Through the Forest(SPFA+记忆化搜索)

    题目给一张有向图,问从起点1到终点2沿着合法的路走有种走法,合法的路指从u到v的路,v到终点的距离严格小于u到终点的距离. 先SPFA预处理出所有合法的路,然后这些路肯定形成一个DAG,然后DP一下就 ...

  3. ural 1219. Symbolic Sequence

    1219. Symbolic Sequence Time limit: 1.0 secondMemory limit: 64 MB Your program is to output a sequen ...

  4. BZOJ4009: [HNOI2015]接水果

    4009: [HNOI2015]接水果 Description 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果. 由于她已经DT FC 了The big black,  她 ...

  5. Linux_使用Linux之安装jdk 7

    工具/原料 jdk7源码安装压缩包 方法/步骤   卸载OpenJDK rpm -qa | grep java rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1 ...

  6. js的url中传递中文参数乱码,如何获取url中参数问题

    一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码: 1.传参页面Javascript代码: <script type=”text/javascript ...

  7. javamail发送邮件的简单实例

    今天学习了一下JavaMail,javamail发送邮件确实是一个比较麻烦的问题.为了以后使用方便,自己写了段代码,打成jar包,以方便以后使用.呵呵 以下三段代码是我的全部代码,朋友们如果想用,直接 ...

  8. 使用mybatis执行oracle存储过程

    存储过程在小公司用的不多,但是如果业务比较复杂或者性能要求比较苛刻的时候存储过程就派上用场了,ibatis的前期的一些版本貌似不支持存储过程因此我选择了mybatis来做实验. 1.无输入和输出参数的 ...

  9. PHP Execute Command Bypass Disable_functions

    先简单说一下php调用mail()函数的过程. 看到源码ext/mail.c 236行: char *sendmail_path = INI_STR("sendmail_path" ...

  10. Leetcode | Parentheses 相关

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...