首先来看一个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. BZOJ1834 [ZJOI2010]network 网络扩容(最小费用最大流)

    挺直白的构图..最小费用最大流的定义. #include<cstdio> #include<cstring> #include<queue> #include< ...

  2. 寒冰王座[HDU1248]

    寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. Robotium 测试方法

    1.检查CheckBox 是否选上,用solo.isCheckBoxChecked( “text” ). 有时候checkBox 没有相关的text,这时要用solo.isCheckBoxChecke ...

  4. linux开启ssh服务

    本文概略:1)ubuntu发行版开启ssh.2)centos发行版开启ssh 1.ubuntu发行版安装/开启ssh服务 1.1 安装ssh服务端 sudo apt-get install opens ...

  5. 去掉地址栏中的jsessionid

    原来我在index.jsp中的编码是 <c:redirect url="/sys/login.shtm"/> 结果每次第一次登录都会在地址栏上出现了jsessionid ...

  6. windows渗透被人忽视的一些小命令

    cmdkey /list 可以列出域内网之间可用的凭据. wmic   useraccount get name,sid gpresult /r

  7. 在client类中设置访问属性 address,business和individua

    php 5.4中的traits,是新引入的特性,中文还真不知道如何准确翻译好.其实际的目的, 是为了有的场合想用多继承,但PHP又没多继承 ,于是就发明了这样的一个东西. Traits可以理解为一组能 ...

  8. VSS错误自动修复

    公司项目开发源代码管理一直用vss,从vss6.0用到vss8.0(vss2005),在近两年的试用中碰到一些大大小小的问题:1:vss服务迁移,这个比较好办,直接将整个vss目录拷贝过去,加上相应的 ...

  9. Robocopy

    用法: http://technet.microsoft.com/zh-cn/library/cc733145%28v=ws.10%29.aspx   图形化工具: http://sourceforg ...

  10. 在Copy-Item中集成认证信息以拷贝文件

    $source = "c:\XXX.XXX" $pw = ConvertTo-SecureString '密码' -AsPlainText -Force $Creds = New- ...