我们使用的手机不光是只用到一个应用程序,比如在淘宝界面要付款的时候可能会启动微信付款等,这就相当于在淘宝的Activity中启动了微信的Activity。还比如说当我们注册一个网站是,可能会给自己发送一条短信作为验证,这就是在当前的Activity中启动了短信的Activity。之前对比的MVC设计模式中Controller可以调用另一个Controller中的数据或者跳转等,那么在Android平台中也是可以实现Activity之间的调用的。

 

程序:

              首先做这样一个程序,从一个Activity跳转到另一个Activity中:

              关联的布局文件main.xml:

[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.       
  7.     <Button  
  8.         android:id="@+id/myButton"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         />  
  12.   
  13. LinearLayout>  

              第一个Activity:

[csharp]  view plain  copy
  1. public class Activity extends ActionBarActivity {  
  2.     /** 
  3.      * Called when the activity is first created 
  4.      */  
  5.     //首先获得点击跳转的按钮  
  6.     private Button myButton =null;  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main); //选择关联的布局文件  
  11.         myButton=(Button)findViewById(R.id.myButton); //通过id获得按钮  
  12.         //通过监听器把对象捆绑到按钮上  
  13.         myButton.setOnClickListener((android.view.View.OnClickListener) new MyButtonListener());  
  14.     }  
  15.       
  16.     //监听器类  
  17.     class MyButtonListener implements OnClickListener{    
  18.         public void onClick(View v){  
  19.             // 生成一个Intent对象  
  20.             Intent intent=new Intent();  
  21.             intent.putExtra("testIntent""123");  
  22.             intent.setClass(Activity.this, OtherActivity.class); //设置跳转的Activity  
  23.             Activity.this.startActivity(intent);  
  24.         }  
  25.     }  
  26. }  

              第一个Activity到第二个Activity是通过intent来传送数据的,那么在第二个Activity是如何接收数据的呢?第二个Activity对应的布局文件other.xml中只用一个TextView来盛放数据。

[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.       
  7.     <TextView  
  8.         android:id="@+id/myTextView"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         />  
  12. LinearLayout>  

              第二个Activity中接收数据

[csharp]  view plain  copy
  1. public class OtherActivity extends Activity {  
  2.     private TextView myTextView=null;  
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.other);  
  8.         Intent intent=getIntent();  
  9.         String value=intent.getStringExtra("testIntent");  
  10.         myTextView=(TextView)findViewById(R.id.myTextView);  
  11.         myTextView.setText(value);  
  12.     }  
  13. }  

             当然不要忘记在配置文件中注册第二个Activity:

[html]  view plain  copy
  1. <activity android:name=".OtherActivity" android:label="@string/other" />  

              在一个Activity跳转到另外一个Activity中是通过intent对象来传递的,而一个Intent对象到底能包含多少东西还需要我们自己去实践。

更多相关文章

  1. “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
  2. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  3. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  4. Android应用程序资源——menu菜单资源
  5. Android常见面试题&字节跳动、阿里、腾讯2019实习生Android岗部
  6. android学习——使用TableLayout动态生成表格,并为tablerow中的列
  7. [置顶] Android(安卓)Binder跨进程与非跨进程的传输异同源码分析
  8. android 获取本地缓存文件大小,删除功能
  9. 重温String和StringBuffer

随机推荐

  1. Android的IPC机制Binder的详解(转发)
  2. 页面未随软键盘上升及android隐藏软键盘
  3. 简单研究Android(安卓)View绘制一 测量过
  4. TextView属性android:ellipsize实现跑马
  5. android:imeOptions属性
  6. Android+Eclipse环境
  7. Android的源代码结构
  8. Android(安卓)编译时注解
  9. android 纯c/c++开发
  10. android布局学习利器-Hierarchy Viewer