在Android中打开窗口有两种方式,第一种是不需要返回值的,第二种是带返回值的。

Main.xml文件,程序从这个窗口开始执行。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btn_open1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一种方式打开" /> <Button
android:id="@+id/btn_open2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二种方式打开" /> </LinearLayout>

FirstActivity.xml,第一个页面文件

<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个窗口" /> </LinearLayout>

SecendActivity.xml

<?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/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二个窗口" /> <Button
android:id="@+id/btnresult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="回传数据"/> </LinearLayout>

main.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity { Button btn_open1;
Button btn_open2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //第一种方式打开窗口,无返回值
btn_open1 = (Button) findViewById(R.id.btn_open1);
btn_open1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 第一个为当前类的上下文参数,不能直接使用this,需要使用类名.this
// 第二个参数为目标文件的反射对象
Intent intent = new Intent(MainActivity.this, FirstActivity.class);
// 启动新窗口,不需要返回值
startActivity(intent);
}
}); //第二种打开方式,带返回值得
btn_open2 = (Button) findViewById(R.id.btn_open2);
btn_open2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 第一个为当前类的上下文参数,不能直接使用this,需要使用类名.this
// 第二个参数为目标文件的反射对象
Intent intent = new Intent(MainActivity.this, SecendActivity.class); // 第一个参数为intent
// 第二个参数为请求的标志,用来区别提交的activity
startActivityForResult(intent, 1);
}
});
} /**
* 通过这个方法用来接收新页面的返回数据,返回内容为Intent对象
* 第一个参数为请求的id,第一个页面传递过来的标志,用来区别是哪个activity传递过来的。 第二个参数为结果的id,第二个页面返回的标志
* 第三个参数为返回的intent对象,通过他获取跳转页面的返回内容
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data); //requestCode判断是哪个按钮提交的请求,用来区分提交的请求
//resultCode判断是哪个页面返回的结果,用来区分返回的页面请求。
if (requestCode == 1 && resultCode == 2) {
String content = data.getStringExtra("data");
Toast.makeText(this, content, 1).show(); //弹出消息框
}
} }

SecendActivity.java

package com.example.demo;

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 SecendActivity extends Activity { Button btn;
String content = "第二个窗口返回的数据"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secend_activity); btn = (Button) findViewById(R.id.btnresult); btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent data = new Intent(); //创建返回的Intent对象
data.putExtra("data", content); //为intent对象设置值
setResult(2, data); //设置回传结果 finish(); //关闭窗口
}
}); }
}

点击第一个按钮会打开第一个窗口。

低级第二个按钮会打开第二个窗口,在第二个窗口中点击回传按钮,返回数据到第一个窗口,并关闭当前窗口。

Android学习(八) 打开Activity的更多相关文章

  1. android学习四(Activity的生命周期)

    要学好活动(Activity).就必需要了解android中Activity的声明周期.灵活的使用生命周期.能够开发出更好的程序,在android中是使用任务来管理活动的,一个任务就是一组存放在栈里的 ...

  2. Android学习笔记之Activity详解

    1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...

  3. android学习笔记26——Activity

    Activity ==> android中四大组件:Activity.Service.BroadcastReceiver.ContentProvider Activity组件用于对用户呈现操作界 ...

  4. android学习二(Activity)

    前面我简单的介绍了android的一些基础知识,当作热身吧,接下来接触android的四大组件的activity活动. 1.活动Activity是是一种保护用户界面的组件,主要用于和用户进行交互. 活 ...

  5. Android学习笔记:Activity生命周期详解

    进行android的开发,必须深入了解Activity的生命周期.而对这个讲述最权威.最好的莫过于google的开发文档了. 本文的讲述主要是对 http://developer.android.co ...

  6. Android学习整理之Activity篇

    一.Activity概念介绍 activity属于android的四大组件之一(其他的三个: Content provider,Broadcast receiver,Service),它可以理解为一个 ...

  7. Android学习——Fragment与Activity通信(一)

    学会了在Activity中加载Fragment的方法之后,接下来便需要学习Activity和Fragment之间的通信.这一节先学习如何把Activity中的信息传递给Fragment. 基本过程 在 ...

  8. Android学习八:获取网络图片

    看到QQ群里有个朋友说加载图片内存溢出的问题,所以就按照自己的想法试试的.但是按照他的方法,不知道为何没有发生内存溢出,不知道什么情况. 写这篇文章主要有三个目的: 1.多线程的学习 2.图片加载的学 ...

  9. Android学习整理之Activity生命周期篇

    一.Activity生命周期说明   Activity的四种状态: ⒈活动状态(Active or Running):也称为运行状态,处于Activity栈顶,在用户界面中最上层,完全能被用户看到,能 ...

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

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

随机推荐

  1. 一种机制,与js类似

    我们知道,当两个条件进行逻辑与操作的时候,其中任何一个条件为假,则表达式的结果为假.所以,遇到(A 且 B)这种表达式,如果A为假的话,B是不是真假都无所谓了,当遇到一个假条件的时候,程序也就没有必要 ...

  2. Java I/O 笔记

    1. Java常用I/O类概述 2. 文件I/O 你可以根据该文件是二进制文件还是文本文件来选择使用FileInputStream(FileOutputStream)或者FileReader(File ...

  3. Kubernetes网络配置

    #flannel#所有node都安装#下载https://github.com/coreos/flannel/releases#解压并把flanneld和mk-codker-opts.sh复制到/us ...

  4. This Android SDK requires Android Developer Toolkit version 20.0.0 or above

    本人最近在操作更新ANDROID SDK时出现类似于题目中的错误,是一启动ECLIPSE时.但是,我现在只是想恢复到原先的开发环境.于是找到本文,方法有效!!! windows 下面安装Android ...

  5. 【linux高级程序设计】(第十一章)System V进程间通信 3

    信号量通信机制 可以看到,跟消息队列类似,也是包括两个结构. int semget (key_t __key, int __nsems, int __semflg) : 创建信号量集合 第一个参数:f ...

  6. 爬虫练习二:GUI+下载百思不得姐网站视频

    环境 python2.7 pycharm 课题:Python爬取视频(桌面版)---爬虫,桌面程序应用 优点:语法简洁,入门快,代码少,开发效率高,第三方库 1.图形用户界面---GUI 2.爬虫,爬 ...

  7. 详解TCP的三次握手四次断开

    本文将分别讲解经典的TCP协议建立连接(所谓的“3次握手”)和断开连接(所谓的“4次挥手”)的过程. 尽管TCP和UDP都使用相同的网络层(IP),TCP却向应用层提供与UDP完全不同的服务.TCP提 ...

  8. 解析转换json xml 集合 ado

    json提取 string str = "[{\"JUDGE_RESULT\":\"B类\",\"JUDGE_RESULT\":\ ...

  9. #请用索引取出下面list的指定元素:

    #!/usr/bin/python # -*- coding: utf-8 -*- L = [ ['Apple', 'Google', 'Microsoft'], ['Java', 'Python', ...

  10. [android]加载大量图片避免OOM

    原理是事先取得图片的长宽,直接读出缩略图. BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferr ...