使用Intent在Activity间传值 .

来源:互联网 发布:边际效应递减规律知乎 编辑:程序博客网 时间:2024/06/06 10:41

在Android中, 一个Activity可以理解为一个屏幕。  当程序要从一个Activity切换到另外一个Activity时,就需要用到Intent。

Intent专门负责Android程序之间、以及程序内Activity与Service之间交互、通信。


以下是一个简单的demo,使用Intent从一个Activity切换到另一个Activity,并且传递一个数据进行显示。


先看一下效果图——

    第一个Activity:

输入一个姓名“Barry”:

点击“确定”,跳转到另一个Activity,并显示“你好,Barry!”:



代码如下:

Demo_intentActivity.java

[java] view plaincopyprint?
  1. package barry.android.intent;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.   
  11. public class Demo_intentActivity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.   
  18.         Button btn = (Button) findViewById(R.id.button1);  
  19.         btn.setOnClickListener(new OnClickListener() {  
  20.             @Override  
  21.             public void onClick(View v) {  
  22.                 String yourName = ((EditText) findViewById(R.id.name_text))  
  23.                         .getText().toString();  
  24.                 Intent intent = new Intent();  
  25.                 intent.setClass(Demo_intentActivity.this, RecordActivity.class);  
  26.                 intent.putExtra("name", yourName);// 在Intent中设置要传递的数据  
  27.                 startActivity(intent);// 启动新的Activity  
  28.                 Demo_intentActivity.this.finish();// 结束就的Activity  
  29.             }  
  30.         });  
  31.     }  
  32. }  
以及它的布局文件main.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <EditText   
  8.         android:id="@+id/name_text"  
  9.         android:hint="输入你的名字"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         />      
  13.       
  14.     <Button android:text="确定"  
  15.         android:id="@+id/button1"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"/>      
  18.       
  19. </LinearLayout>  


另一个Activity  RecordActivity.java

[java] view plaincopyprint?
  1. package barry.android.intent;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.TextView;  
  7.   
  8. public class RecordActivity extends Activity {  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.record);          
  13.         Intent intent = getIntent();  
  14.         String yourname = intent.getStringExtra("name");//从Intent中获取数据          
  15.         TextView text_show = (TextView) findViewById(R.id.text_show);  
  16.         text_show.setText("你好,"+yourname+"!"); //显示到屏幕上  
  17.     }  
  18. }  
及其布局文件 record.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/text_show"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent" />  
  11.   
  12. </LinearLayout>  


需要注意的是,每个Activity都需要在AndroidManiFest.xml中声明一下,所以在AndroidManiFest.xml中需要加入:

[html] view plaincopyprint?
  1. <activity android:name=".RecordActivity"></activity>  



查看评论
1楼 ofacop2012-03-22 19:57发表 [回复] [引用][举报]
原创粉丝点击