android中activity之间使用intent通信
16lz
2022-07-14
基本概念
-
activity:activity是android几大组件之一,呈现给用户的每一个界面都是一个activity,可以在activity中定制各式各样的布局,那么在不同的activity之间是如何通信的呢?这就要引入intent
-
intent:intent在android中被形象地比喻为搬运工,主要是在不同的activity之间实现数据的传输和界面的跳转,从面向对象的角度可以这么分析:
-
activity相当于面向对象类中的private属性成员,默认情况下只能被当前界面中的其他控件调用,在实际编程中也是这样的,一个activity使用一个类来封装,这样可以降低不同类之间的耦合性,即松耦合
-
intent相当于面向对象类中的public属性成员,activity可以通过它和其他activity或者程序进行交互和通信
-
实例展示
-
定义第一个activity:MainActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package com.wjink.activity; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button mButton = null ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton = (Button)findViewById(R.id.btn01); //设置按键监听 mButton.setOnClickListener( new mOnClickListener()); } //通过实现OnClickListener接口,完成mOnClickListener类的建立 class mOnClickListener implements OnClickListener { @Override public void onClick(View v) { //建立intent对象 Intent mIntent = new Intent(); //setClass方法,第一个参数为本activity对象 //第二参数为intent要传递到的activity类 mIntent.setClass(MainActivity. this , Activity02. class ); //启动activity中intent对象 MainActivity. this .startActivity(mIntent); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true ; } } |
-
第一个activity对应的layout文件,定义一个文本显示空间和一个button控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | < 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/txt01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/hello_world" /> < Button android:id = "@+id/btn01" android:layout_below = "@id/txt01" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/button01" /> RelativeLayout > |
-
定义第二个activity:Activity02
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.wjink.activity; 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 Activity02 extends Activity { Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_activity02); mButton=(Button)findViewById(R.id.btn02); mButton.setOnClickListener( new monClickListener()); } class monClickListener implements OnClickListener{ @Override public void onClick(View v) { //和第一个activity不同的地方,就是intent传输的方向 Intent mIntent= new Intent(); mIntent.setClass(Activity02. this , MainActivity. class ); Activity02. this .startActivity(mIntent); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity02, menu); return true ; } } |
-
第二个activity的layout布局文件:基本上和第一个activity的布局是一样的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | < 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 = ".Activity02" > < TextView android:id = "@+id/txt02" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/hello_world" /> < Button android:id = "@+id/btn02" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:layout_below = "@id/txt02" android:text = "@string/button02" /> RelativeLayout > |
-
源代码下载
点击打开下载链接
总结
- intent不仅能够实现不同activity之间的跳转,还能够在不同activity之间传输数据,调用系统自带的程序,例如发短信,打电话等等
- 除了一些action和data属性之外,intent还有category,type,component,extras等属性
- 更多内容参考api文档中intent部分
更多相关文章
- Android开发——Android搜索框架(二)
- [Android] ACTION_GET_CONTENT与ACTION_PICK的区别
- Android(安卓)UI系列 - 布局 - 目录
- android Manifest.xml选项-android:ConfigChanges
- linearLayout 和 relativeLayout的属性区别
- Android(安卓)属性动画(Property Animation) 完全解析 (下)
- Android修改字体样式
- android EditText使用指南
- ImageView的scaletype属性