【Android开发学习20】多个Activity间的交互之Intent
来源:互联网 发布:sqoop mysql导入hive 编辑:程序博客网 时间:2024/05/07 09:32
一、基础知识:
1.一个Intent对象包含了一组信息:
1. Component name 指定启动的Activity
2. Action 要做什么
3. Data 传送数据
4. Category
5. Extras 键值对
6. Flags
2.Intent基本用法:
// 生成一个Intent对象Intent intent = new Intent();intent.putExtra("testIntent", "123");// 传递数据intent.setClass(Activity_02.this, OtherActivity.class);// 指定跳向哪一个Activity(第二个参数)//Activity_02.this.startActivity(intent);startActivity(intent);
// 接收Intent传过来的数据Intent intent = getIntent();String value = intent.getStringExtra("testIntent");// 接收Intent的数据myTextView = (TextView)findViewById(R.id.myTextView);//myTextView.setText(R.string.other);myTextView.setText(value);
3.按钮事件的注册:
private Button myButton = null;myButton = (Button)findViewById(R.id.myButton);myButton.setOnClickListener(new MyButtonListener());class MyButtonListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 生成一个Intent对象Intent intent = new Intent();intent.putExtra("testIntent", "123");// 传递数据intent.setClass(Activity_02.this, OtherActivity.class);// 指定跳向哪一个Activity(第二个参数)//Activity_02.this.startActivity(intent);startActivity(intent);/*Uri uri = Uri.parse("smsto:0800000123");Intent intent = new Intent(Intent.ACTION_SENDTO, uri);intent.putExtra("sms_body", "The SMS text");startActivity(intent); */}}
二、代码展示:
1."Activity_02\src\yan\activity_02\Activity_02.java"
package yan.activity_02;import android.net.Uri;import android.os.Bundle;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;public class Activity_02 extends Activity {private Button myButton = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_02);myButton = (Button)findViewById(R.id.myButton);myButton.setOnClickListener(new MyButtonListener());}class MyButtonListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 生成一个Intent对象Intent intent = new Intent();intent.putExtra("testIntent", "123");intent.setClass(Activity_02.this, OtherActivity.class);//Activity_02.this.startActivity(intent);startActivity(intent);/*Uri uri = Uri.parse("smsto:0800000123");Intent intent = new Intent(Intent.ACTION_SENDTO, uri);intent.putExtra("sms_body", "The SMS text");startActivity(intent); */}}}
2."Activity_02\src\yan\activity_02\OtherActivity.java"
package yan.activity_02;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class OtherActivity extends Activity{private TextView myTextView = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.other);Intent intent = getIntent();String value = intent.getStringExtra("testIntent");myTextView = (TextView)findViewById(R.id.myTextView);//myTextView.setText(R.string.other);myTextView.setText(value);}}
3."Activity_02\res\layout\activity_02.xml"
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
4."Activity_02\res\layout\other.xml"
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
5."Activity_02\res\values\strings.xml"
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">Activity_02</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string><string name="other">other string</string></resources>
6.“Activity_02\AndroidManifest.xml”
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="yan.activity_02" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="4" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="yan.activity_02.Activity_02" 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="yan.activity_02.OtherActivity" android:label="@string/other" > </activity> </application></manifest>
注意这个文件中的activity的声明:
<activity android:name="yan.activity_02.OtherActivity"
android:label="@string/other" >
</activity>
三、效果展示:
点击上面的Button之后-->> 跳转到另一个Activity。
本文完整代码下载地址: http://download.csdn.net/detail/ypist/5138078
本文博客源地址:http://blog.csdn.net/ypist
- 【Android开发学习20】多个Activity间的交互之Intent
- INTENT类型,多个Activity交互
- Android开发学习之路--Activity之Intent
- Android开发 --- 多个Activity,及之间数据传递 Intent
- android学习笔记之-利用Intent在多个Activity之间跳转
- Android学习笔记(七):多个Activity和Intent
- Android学习笔记(七):多个Activity和Intent
- Android开发笔记之《Activity与Intent》
- Android开发之关闭多个Activity
- Android:多个Activity和Intent
- Android基础知识之四大组件Activity(三)多个Activity交互的生命周期与设计思想
- Android开发,用于Activity的标准Intent
- 【Android开发】Activity 的 Intent Filter
- Intent之学习二(Activity间数据的传递)
- android开发学习笔记(9)Intent之在两个Activity之间切换
- android开发学习之路(1)---- activity及intent基本详解(1)
- android开发学习之路(1)---- activity及intent基本详解(2)
- 两个Activity间的交互startActivityForReuselt(intent,int)
- LINQ to SQL语句
- 黑马程序员—关于数组的应用
- [转]C++大数模板 卡特兰数
- 一张表格教你如何开好Scrum每日站会
- Delegate简单的小例子
- 【Android开发学习20】多个Activity间的交互之Intent
- 两个SqlDataReader共享同一个SqlConnection出错
- 从文件中读取成绩
- 色彩模式
- 括号匹配问题
- Oracle复制行记录的小技巧
- mac 下配置adb环境
- EXPAT usage
- 项目中常用的动画集锦(一直更新中……)