Android——Intent,Bundle
- Intent——切换activity
intent.setClass(first.this,second.class);
startActivity(intent);
- Bundle——切换时数据传递
//跳转
Intent intent = new Intent();
//跳转时数据传输
Bundle bundle = new Bundle();
intent.setClass(MainActivity.this, SecondActivity.class);
bundle.putString("message", str);
intent.putExtras(bundle);
startActivity(intent);Bundle bundle = this.getIntent().getExtras();
String message = bundle.getString("message");
Source Code:
package com.example.intent; import android.os.Bundle;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity { private EditText myText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button button = (Button)findViewById(R.id.button1);
myText = (EditText)findViewById(R.id.editText1); button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { String str = myText.getText().toString();
//跳转
Intent intent = new Intent();
//跳转时数据传输
Bundle bundle = new Bundle(); intent.setClass(MainActivity.this, SecondActivity.class);
bundle.putString("message", str);
intent.putExtras(bundle); startActivity(intent); }
}); } }
MainActiviy.java
package com.example.intent; import android.app.Activity;
import android.os.Bundle;
import android.view.TextureView;
import android.widget.TextView; public class SecondActivity extends Activity{ @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second); Bundle bundle = this.getIntent().getExtras();
String message = bundle.getString("message"); TextView myText = (TextView)findViewById(R.id.textView2);
myText.setText(message); } }
SecondAcitivity.java
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">Intent</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Tree">TreeDream</string> </resources>
strings.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="@string/Tree" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/x" /> </LinearLayout>
second.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="51dp"
android:text="Turn to the secondActivity." /> <EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:ems="10" > <requestFocus />
</EditText> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/textView1"
android:src="@drawable/y" /> </RelativeLayout>
activity_main.xml
Android——Intent,Bundle的更多相关文章
- Android中Bundle和Intent的区别
Bundle的作用,以及和Intent的区别: 一.Bundle: A mapping from String values to various Parcelable types 键值对的集合 类继 ...
- android Intent介绍
Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 ...
- Android Intent (可通过URL启动 Activity)
Intent分为两大类: (1)显性的(Explicit) (2)隐性的(Implicit) 对于隐性意图,在某些时候, 应用程序只是想启动具有某种特征的组件, 并不想和某个特定的组件耦合. 使用In ...
- Android之Bundle类
API文档说明 1.介绍 用于不同Activity之间的数据传递 1.重要方法 clear():清除此Bundle映射中的所有保存的数据. clone():克隆当前Bundle containsKey ...
- android Intent.ACTION_SEND
ACTION_SEND intent 可以把自己的应用添加到系统的发送(分享)列表中. <intent-filter> <action android:name="andr ...
- [Android] Intent详解
[转]http://www.cnblogs.com/engine1984/p/4146621.html [正文] Intent组件虽然不是四大组件,但却是连接四大组件的桥梁,学习好这个知识,也非常的重 ...
- 关于android中Bundle的使用
1.Android using Bundle for sharing variables 注:android中使用Bundle来共享变量,下例中Activity1和Activity2通过bundl ...
- Android Intent 用法全面总结
[代码全屏查看]-Android Intent 用法全面总结 // [1].[代码] 调用拨号程序 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] / ...
- 【转】Android Intent Action 大全
String ADD_SHORTCUT_ACTION 动作:在系统中添加一个快捷方式.. “android.intent.action.ADD_SHORTCUT” String ALL_APPS_AC ...
随机推荐
- es6 Null 传导运算符
Null 传导运算符 程实务中,如果读取对象内部的某个属性,往往需要判断一下该对象是否存在.比如,要读取message.body.user.firstName,安全的写法是写成下面这样. const ...
- 发送请求时params和data的区别
在使用axios时,注意到配置选项中包含params和data两者,以为他们是相同的,实则不然. 因为params是添加到url的请求字符串中的,用于get请求. 而data是添加到请求体(body) ...
- 牛客网Java刷题知识点之数组、链表、哈希表、 红黑二叉树
不多说,直接上干货! 首先来说一个非常形象的例子,来说明下数组和链表. 上体育课的时候,老师说:你们站一队,每个人记住自己是第几个,我喊到几,那个人就举手,这就是数组. 老师说,你们每个人记住自己前面 ...
- Beam概念学习系列之Pipeline 数据处理流水线
不多说,直接上干货! Pipeline 数据处理流水线 Pipeline将Source PCollection ParDo.Sink组织在一起形成了一个完整的数据处理的过程. Beam概念学习系列之P ...
- mac上k8s学习踩坑
本文学习k8s参考内容:http://docs.kubernetes.org.cn/126.html,学习过程中遇到一些坑,记录如下: -------------------------------- ...
- Visual Studio 创建封装自己的代码段(C#)
https://www.cnblogs.com/awaTangjay/p/6644952.html 1.打开vs2012--工具--代码段管理器 2.进入代码管理器之后,语言选择Visual C#,然 ...
- 解决eclipse中org.w3c.dom.Node类老报The method getTextContent() is undefined 问题
http://www.cnblogs.com/itspy007/articles/4431581.html
- c# xml API操作
LoginInfo loginInfo = new LoginInfo(); xmlNode = _xml.SelectSingleNode(loginUrl); loginInfo.LoginUrl ...
- [转]ASP.NET Core 中文文档 第四章 MVC(4.3)过滤器
本文转自:http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-4_4_3-filters.html 原文:Filters 作者:Steve Smith 翻 ...
- shell命令修改文件内容
有个 test.txt 文件内容为 hello tom,现在修改成 hello jerry,并保存到test2.txt sed 's/tom/jerry/g' test.txt >test2. ...