android_Intent对象初步(Activity传统的价值观念)
说明:初步Intent物。主要使用Intent对象在Activity之间传递数据的方法。
样例:由MainActivity→OtherActivity的跳转过程中,把数据传递给OtherActivity并显示出来。
在讲步骤之前,先来看看Intent到底是个什么东西先。
Intent对象的基本概念:
1、Intent对象是Android应用程序组件之中的一个;
2、Intent对象在Android系统其中表示一种意图;
3、Intent其中最重要的内容是action和data。
(还有Component name、Category、Extras、Flags。)
步骤:1.在MainActivity里面生成一个Intent对象,在Intent对象里面使用putExtra()系列方法,把数据放到Intent对象其中去。
activity_main.xml里面放置一个Button,点击跳转到OtherActivity。
<Button
android:id="@+id/btnId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动第二个Activity" />
然后生成Intent,使用putExtra()向Intent对象其中存储数据。
package com.away.b_04_intent; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
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.btnId);
button.setOnClickListener(new ButtonListener());
} class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class); intent.putExtra("com.away.b_04_intent.Age", 21);
intent.putExtra("com.away.b_04_intent.Name", "Chay");
startActivity(intent);
//startActivity(new Intent(MainActivity.this, OtherActivity.class).putExtra("name","Chay").put....);
}
}
}
2.在OtherActivity里面使用getXXXExtra()系列方法从Intent对象其中取出数据。
package com.away.b_04_intent; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; public class OtherActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other); Intent intent = getIntent();
int age = intent.getIntExtra("com.away.b_04_intent.Age", 10);
String name = intent.getStringExtra("com.away.b_04_intent.Name"); textView = (TextView) findViewById(R.id.textView);
textView.setText(name + ":" + age + "");
}
}
效果图:
上面是传递的数据是比較少的,假设数据比較多。就须要使用Bundle类了,代码例如以下:
MainActivity:
<pre name="code" class="java">Intent intent = new Intent(MainActivity.this, OtherActivity.class); /* 通过Bundle对象存储须要传递的数据 */
Bundle bundle = new Bundle();
/*字符、字符串、布尔、字节数组、浮点数等等。都能够传*/
bundle.putString("Name", "Away");
bundle.putBoolean("Ismale", true); /*把bundle对象assign给Intent*/
intent.putExtras(bundle);
//intent.putExtra("bundle", bundle); startActivity(intent);<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
OtherActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*载入页面*/
setContentView(R.layout.other);
/*获取Intent中的Bundle对象*/
Bundle bundle = this.getIntent().getExtras();
// Bundle bundle = getIntent().getBundleExtra("bundle");
/*获取Bundle中的数据,注意类型和key*/
String name = bundle.getString("Name");
boolean ismale = bundle.getBoolean("Ismale");
}
-----------------------------------------------割-----------------------------------------------
ps:有时。在页面跳转之后,须要返回到之前的页面,同一时候要保留用户之前输入的信息。这个时候该怎么办呢?
在页面跳转后,前一个Activity已经被destroy了。假设要返回并显示数据。就必须将前一个Activity再次唤醒,同一时候调用某个方法来获取并显示数据。
要实现这个效果,须要做下面几步:
1. 首先。从A页面跳转到B页面时。不能够使用“startActivity()”方法。而要使用“startActivityForResult()”方法。
2. 在A页面的Activity中,须要重写“onActivityResult()”方法
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
switch(requestCode){
case RESULT_OK:
/*取得来自B页面的数据,并显示到画面*/
Bundle bundle = data.getExtras(); /*获取Bundle中的数据,注意类型和key*/
String name = bundle.getString("Name");
boolean ismale = bundle.getBoolean("Ismale");
}
}
3. 在B页面上加一个返回button,并在事件写例如以下代码:
/*给上一个Activity返回结果*/
B.this.setResult(RESULT_OK,intent);
/*结束本Activity*/
B.this.finish();
比如。选择日期,然后返回。
。
欢迎交流 http://blog.csdn.net/ycwol/article/details/39859341
版权声明:本文博主原创文章,博客,未经同意不得转载。
android_Intent对象初步(Activity传统的价值观念)的更多相关文章
- struts2移除标签button的id传统的价值观念问题
<!--显示数据列表--> <tbody id="TableData" class="dataContainer" datakey=" ...
- Android 向Application对象添加Activity监听
可以建立对象把Application.ActivityLifecycleCallbacks接口中的函数实现,并利用public void registerActivityLifecycleCallba ...
- Activity生命周期,切换,参数传递,bundle(包),值对象,Activity参数返回,Activity的启动模式
Activity代表手机屏幕的一屏,或是平板电脑中的一个窗口.它是android应用中最重要的组成单元之一,提供了和用户交互的可视化界面.在一个Activity中,可以添加很多组件,这些组件负责具体的 ...
- Application对象、Session对象、Cookie对象、Server对象初步认识
Application对象:记录应用程序参数的对象 用于共享应用程序级信息,即多个用户共享一个Application对象.在第一个用户请求ASP.NET文件时,将启动应用程序并创建Applicatio ...
- 24.OGNL与ValueStack(VS)-集合对象初步
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 首先在LoginAction中增加如下字段并提供相应的get/set方法: ...
- 解析图书 XML
Java代码: package com.thinkgem.jeesite.test; import org.dom4j.Attribute; import org.dom4j.Document; im ...
- EasyUI Combobox 默认设置
/** *绑定运营商,默认设置, 演出CMCC, 传统的价值观念1 */ $('#operatingId').combobox({ url:'data_url', valueField:'id', t ...
- 三大分析法——SWOT、PEST、波特五力
SWOT分析法 「SWOT分析模型简介」 (也称TOWS分析法.道斯矩阵).在现在的战略规划报告里,SWOT分析应该算是一个众所周知的工具.来自于麦肯锡咨询公司的SWOT分析,包括分析企业的优势(St ...
- Android应用程序窗口(Activity)的视图对象(View)的创建过程分析
从前文可知道,每一个Activity组件都有一个关联的Window对象,用来描述一个应用程序窗口.每一个应用程序窗口内部又包含有一个View对象,用来描述应用程序窗口的视图.应用程序窗口视图是真正用来 ...
随机推荐
- 不是技术牛人,如何拿到国内IT巨头的Offer
原地址:http://blog.csdn.net/lsldd/article/details/13506263 不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些 ...
- [Leetcode][Python]54: Spiral Matrix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...
- 杭电oj 3361
Tips:字符在计算机中都是以ASCII码形式保存,直接以char形式输出ASCII码即可. #include<stdio.h> int main() { int T; scanf(&qu ...
- poj 1887 Testing the CATCHER_最长上升子序列
题意:题目太长没看,直接看输入输出猜出是最长下降子序列 用了以前的代码直接a了,做法类似贪心,把最小的顺序数存在数组里面,每次二分更新数组得出最长上升子序列 #include<iostream& ...
- Oracle password expire notices
/usr/local/webserver/tomcat6/logs/logbak/zsxxw.log.2015-03-21.txt:2015-03-22 00:47:26,366 ORA-28002: ...
- how to translate the text of push button
Background:In a project, the need to translate the buttons on the screen, as shown below,the followi ...
- js函数设计原则
一般认为函数指具有返回值的子程序,过程指没有返回值的子程序.C++中把所有子程序成为函数,其实那些返回值为void的 函数在语义上也是过程.函数与过程的区别更多是语义上的区别,而不是语法的区别. 语言 ...
- UVA 1613 K-Graph Oddity
题意; 在一个n个点的无向连通图中,n是奇数,k是使得所有点的度数不超过k的最小奇数,询问一种染色方案,使得相邻点的颜色不同. 分析: k=所有点中最大的入度.如果最大入度是偶数,则k+1.每个点可选 ...
- (四)Android中Context的理解与使用
一.Context的作用 Context可用于访问全局资源. public class MainActivity extends Activity { private TextView tv; @Ov ...
- web.config详解
在开发中经常会遇到这样的情况,在部署程序时为了保密起见并不将源代码随项目一同发布,而我们开发时的环境与部署环境可能不一致(比如数据库不一样),如果在代码中保存这些配置这些信息部署时需要到用户那里更改代 ...