Android基础知识_Activity之间传递参数
2015-06-22 21:23
525 查看
运行绑定机制Intent在Android应用程序的开发中起着基础性的重要作用,在页面跳转、传递数据、调用外部程序时都会看到Intent的身影。编程时,一般通过Intent向Android系统发出某种请求,然后Android会根据请求查询各个组件声明的intent-filter,找到需要的组件并运行。
关于Intent的详细使用方法,会在后续的博文中详细写出。本博文主要通过1个工程 4个简单示例分别实现①传递简单数据、②传递数据包Bundle、③传递值对象、④获取Activity的返回参数。
示例工程SendArgsDemo的详细代码如下:
activity_main.xml主布局文件代码:
关于Intent的详细使用方法,会在后续的博文中详细写出。本博文主要通过1个工程 4个简单示例分别实现①传递简单数据、②传递数据包Bundle、③传递值对象、④获取Activity的返回参数。
示例工程SendArgsDemo的详细代码如下:
activity_main.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" > <Button android:id="@+id/btnStartAty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="49dp" android:text="启动另一个Activity" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btnStartAty" android:layout_centerHorizontal="true" android:layout_marginTop="77dp" android:text="TextView" /> </RelativeLayout>activity_baty.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/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="TextView" /> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Back" /> </LinearLayout>MainActivity.java文件代码:
package com.example.sendargsdemo; 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.TextView; public class MainActivity extends Activity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView); /* findViewById()取得布局上的Button控件, 处理单击事件 */ findViewById(R.id.btnStartAty).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /* 参数v事件发生的事件源 */ /* 第一个参数为context, 第二个参数为被启动的Activity的类的定义 */ Intent intent = new Intent(MainActivity.this, BAty.class); // /* 注释下面代码为: ①传递简单数据示例 // * putExtra()传递参数 // */ // intent.putExtra("data", "Hello World!"); // /* 注释下面代码为: ②传递数据包Bundle示例 // * 传递数据包Bundle // */ // Bundle bundle = new Bundle(); // bundle.putString("name", "SmallBundle"); // bundle.putInt("age", 2); // bundle.putString("name1", "iwen"); // /* 方式一 */ //// intent.putExtras(bundle); // /* 方式二 */ // intent.putExtra("data", bundle); /* 注释下面代码为: ③传递值对象示例 * */ intent.putExtra("user", new User("LiHua", 20)); // startActivity(intent); /* 注释下面代码为: ④获取 Activity的返回参数 * */ /* 接收被启动的Activity传回的参数 */ /* 第二个参数为请求代码, 请求代码传递给第二个Activity, 由第二个Activity再把请求代码传回来, 通过请求代码确定本次请求的意义, 实际开发中很有用 */ startActivityForResult(intent, 0); } }); } /* 注释下面代码为: ④获取 Activity的返回参数 * */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); textView.setText("另一个Activity返回的数据是: " + data.getStringExtra("data")); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }BAty.java另一个Activity的文件代码:
package com.example.sendargsdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.TextView; public class BAty extends Activity { private TextView tView; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_baty); tView = (TextView)findViewById(R.id.tv); /* 注释下面代码为: ④获取 Activity的返回参数 * */ editText = (EditText) findViewById(R.id.editText); /* BAty由Intent启动, 通过getIntent()方法获取与这个Activity相关的Intent */ Intent intent = getIntent(); // /* 注释下面代码为: ②传递数据包Bundle示例 // * 传递数据包Bundle // */ // /* 方式一 */ //// Bundle dataBundle = intent.getExtras(); // /* 方式二 */ // Bundle dataBundle = intent.getBundleExtra("data"); // /* 注释下面代码为: ①传递简单数据示例 // * 设置字符串:获取字符串名为data的数据 // */ // tView.setText(intent.getStringExtra("data")); // /* 注释下面代码为: ②传递数据包Bundle示例 // * */ // tView.setText(String.format("name = %s, age = %d, name1 = %s", dataBundle.getString("name"), dataBundle.getInt("age"), dataBundle.getString("name1", "leo"))); /* 注释下面代码为: ③传递值对象示例 * */ /* 方式一: Serializable */ // User user = (User)intent.getSerializableExtra("user"); /* 方式二: Parcelable */ User user = intent.getParcelableExtra("user"); tView.setText(String.format("User info(name = %s, age = %d)", user.getName(), user.getAge())); /* 注释下面代码为: ④获取 Activity的返回参数 * */ findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub /* 第一个参数返回状态码, 第二个参数Intent对象 */ Intent intent = new Intent(); intent.putExtra("data", editText.getText().toString()); setResult(1, intent); finish(); // 结束当前Activity } }); } }User.java文件代码:
package com.example.sendargsdemo; import java.io.Serializable; import javax.xml.transform.Source; import android.os.Parcel; import android.os.Parcelable; /* 方式一: 使用Java语言内置序列化对象接口; * 特点: 操作简单, 序列化操作由系统完成, 速度慢 * */ //public class User implements Serializable { /* 方式二: 使用Android平台提供的Parcelable序列化接口; * 特点: 操作复杂, 速度快 * */ public class User implements Parcelable { private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public User(String name, int age) { this.name = name; this.age = age; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } /* 系统自动执行此函数 */ @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeString(getName()); dest.writeInt(getAge()); } public static final Creator<User> CREATOR = new Creator<User>() { /* 通过Parcel创建对象 */ @Override public User createFromParcel(Parcel source) { // TODO Auto-generated method stub return new User(source.readString(), source.readInt()); } @Override public User[] newArray(int size) { // TODO Auto-generated method stub return new User[size]; } }; }AndroidManifest.xml文件代码:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sendargsdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sendargsdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".BAty" android:label="BAty" ></activity> </application> </manifest>通过 Ctrl + / 解除相关注释内容,使4个示例分别运行在***D中,可观察到实际效果。
相关文章推荐
- Android中scrollview和viewpage滑动冲突解决
- Google《Android性能优化》学习笔记
- 自己动手写一个测试Android事件传递机制的demo
- Android进程间通信(IPC)机制Binder简介和学习计划
- Android4.4的zygote进程
- android屏幕适配dip,dpi,px,sp相关
- java4Android(14)I/O流 、分类、目的、字节流重要类及方法FileInputStream
- Android ListView侧边字母索引栏SideBar控件的实现
- android中xml的pull解析
- Android Studio开发第四篇版本管理Git(下)
- Android OpenGL ES(二)----平滑着色
- Android-SQLitedatabase
- 使用android模拟器需要的设置(环境变量设置
- Android 友盟社会化组件-分享实现
- android PopupWindow消失后,将EditText的软键盘隐藏
- How to improve quality and syntax of your Android code
- Android Studio分模块自动化构建实战
- Android五子棋游戏设计与实现
- 有关Android category 相关的解释
- android 监听EditText 的变化