- 浏览: 65996 次
- 性别:
- 来自: Mercury
最新评论
一、多个Activity之间的关系
当从Activity01跳转到Activity02时,必须要启动Activity01的startActivity方法,该方法传递一个Intent对象到Activity02中,所以Intent在多个Activity之间起到了至关重要的作用。
二、Intent的基本作用
Intent是一个对象,且包含了一组信息:
1)*Component name:指定需要启动的另一个Activity的名字
2)*Action:指定当前Activity启动了另一个Activity时需要做哪些动作,如ACTION_CALL,ACTION_EDIT等等
3)*Data:Activity之间传输的数据,是一个URL
4)Category
5)*Extras:额外添加的信息,是一些键值对
6)Flags
三、在一个Activity当中启动另一个Activity的方法
新建项目Activity02:
Activity02.java——初始activity
package com.android.activity; 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 Activity02 extends Activity { private Button myButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton = (Button) findViewById(R.id.myButton); myButton.setText("跳转到另一个activity"); myButton.setOnClickListener(new MyButtonListener()); //向myButton添加点击监听器 } class MyButtonListener implements OnClickListener{ public void onClick(View v) { Intent intent = new Intent(); //生成一个Intent对象 intent.setClass(Activity02.this, OtherActivity.class); //第一个参数设置是原Activity,第二个参数是要请求跳转到的activity Activity02.this.startActivity(intent);//执行 } } }
Intent的setClass方法有两个参数,intent相当于一个请求,第二个参数则表示这个请求要到哪里去。
main.xml——Acitivity02.java这个activity的布局文件
<?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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Activity02!</string> <string name="app_name">Activity测试02</string> <string name="othername">Other Activity</string> </resources>
OtherActivity.java——跳转后的Activity
package com.android.activity; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class OtherActivity extends Activity { private TextView myTextView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); myTextView = (TextView)findViewById(R.id.myTextView); myTextView.setText("这是另一个activity"); } }
other.xml——OtherActivity.java的布局文件
<?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>
AndroidManifest.xml——注册OtherActivity
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.activity" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Activity02" 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=".OtherActivity" android:label="@string/othername"> </activity> </application> </manifest>
运行结果:
初始界面:
跳转后界面:
四、使用Intent在Activity之间传递数据的基本方法
Activity02.Java
Intent intent = new Intent(); intent.putExtra("param", "haha"); //添加参数 intent.setClass(Activity02.this, OtherActivity.class); Activity02.this.startActivity(intent);
OtherActivity.java
Intent intent = getIntent(); String param = intent.getStringExtra("param"); //从intent中取得参数 System.out.println("intent传递的参数:"+param);
发表评论
文章已被作者锁定,不允许评论。
-
Android40_Dialog
2011-11-14 00:11 2944Dialog是Android常用的对话框控件。AlertDia ... -
Android39_Clock和TimePicker
2011-11-14 00:08 2317一、AnalogClock和DigitalClock ... -
Android38_ImageView和Gallery
2011-11-14 00:07 3558一、ImageView使用方法 ImageVi ... -
Android37_JSON数据解析
2011-11-08 00:14 2304一、JSON介绍 JSON(JavaSc ... -
Android36_Animations使用(四)
2011-11-08 00:14 3380一、LayoutAnimationsContrlller ... -
Android35_Animations使用(三)
2011-11-08 00:13 2611一、AnimationSet的具体使用方法 ... -
Android34_Animations使用(二)
2011-11-08 00:12 1919在代码中使用Animations可以很方便的调试、运行 ... -
Android33_Animations使用(一)
2011-11-08 00:12 2246一、Animations介绍 Anima ... -
Android31_AppWidget使用(二)
2011-11-05 00:09 2462一、PendingIntent介绍 PendingIn ... -
Android30_AppWidget使用(一)
2011-11-05 00:08 2227一、App Widget定义 App ... -
Android32_Notification用法
2011-11-05 00:09 1842Android系统的状态栏(Status Bar)中有一 ... -
Android29_SeekBar和RatingBar
2011-11-02 23:21 2067一、使用SeekBar步骤: SeekB ... -
Android28_ExpandableListActivity
2011-11-02 23:21 1457ExpandableListActivity就是可扩展的 ... -
Android27_AutoCompleteTextView
2011-11-02 23:21 1029一、创建AutoCompleteTextView ... -
Android26_DatePicker
2011-11-02 23:20 1746一、DatePicker和DatePickerDialo ... -
Android25_Spinner使用方法
2011-11-02 23:20 2772一、创建Spinner的步骤 1.在布局 ... -
Android24_Service初步
2011-10-18 22:27 965一、Service概念 ... -
Android23_Socket编程
2011-10-18 22:19 1467一、什么是Socket Socket是基 ... -
Android22_WIFI网络操作
2011-10-18 22:12 1654一、什么是WIFI WIFI就是一种无线 ... -
Android21_广播机制(二)
2011-10-18 22:00 964一、注册BroadcastReceiver的方法 ...
相关推荐
Android_intent_大全 Android_intent_大全
Checking_Intent-based_Communication_in_Android_With_Intent_Space
Android Intent标签以及进阶
Android_Intent和Intent_Filter详解
Android应用源码之Intent_Intent
Android_Intent详解
ex07_activity_intent 介绍在android中如何使用intent
android_Intent详解, 关于android中intent的使用有关的东东
Android入门程序_Intent的使用
Checking Intent-based Communication in Android With Intent Space Analysis 发表在AisaCCS'2016上。作者提出了IntentScope工具,能够综合系统中所有的security extensions规则,得出一个完整的,所有APP之间能否...
Android应用源码之Intent1_Intent
Android应用源码之Intent_ComponentSample_Intent
android intent和intent action大全
用Action跳转,用类名跳转,Intent的用法 1. 从google搜索内容 2. 浏览网页 3. 显示地图 4. 路径规划 5. 拨打电话 6. 发送短信 等等
些朋友可能对Android系统的手机不太熟悉,相信你见了Android系统的手机后你肯定会喜欢上的,绚丽的界面,好看的字体,彰显华贵和专业。本源码收集的就是android系统中的一些基本单元,比如弹出的提示框、提示层的...
Android编程之Intent_TabHost源码演示,自定义的Tabhost(内含源码),学习制作Android软件的TAB标签界面,在电脑上已经很流行的功能,Android手机上当然不能少,这个TabHost源码相对简单些,是JAVA转学Android比较...
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...
接收系统的时间流逝的广播,屏幕开启关闭的广播,电量改变的广播。这些广播在清单文件里注册时,看不到效果。在这个例子中通过在一个服务里面用代码注册的方式,成功接收了这些广播。
这是一个关于android intent的用法,希望对大家有用,很全的。