使用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
- package barry.android.intent;
- 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;
- import android.widget.EditText;
- public class Demo_intentActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btn = (Button) findViewById(R.id.button1);
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String yourName = ((EditText) findViewById(R.id.name_text))
- .getText().toString();
- Intent intent = new Intent();
- intent.setClass(Demo_intentActivity.this, RecordActivity.class);
- intent.putExtra("name", yourName);// 在Intent中设置要传递的数据
- startActivity(intent);// 启动新的Activity
- Demo_intentActivity.this.finish();// 结束就的Activity
- }
- });
- }
- }
package barry.android.intent;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;import android.widget.EditText;public class Demo_intentActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button btn = (Button) findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String yourName = ((EditText) findViewById(R.id.name_text)).getText().toString();Intent intent = new Intent();intent.setClass(Demo_intentActivity.this, RecordActivity.class);intent.putExtra("name", yourName);// 在Intent中设置要传递的数据startActivity(intent);// 启动新的ActivityDemo_intentActivity.this.finish();// 结束就的Activity}});}}以及它的布局文件main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <EditText
- android:id="@+id/name_text"
- android:hint="输入你的名字"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button android:text="确定"
- android:id="@+id/button1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/name_text" android:hint="输入你的名字" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:text="确定" android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
- package barry.android.intent;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
- public class RecordActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.record);
- Intent intent = getIntent();
- String yourname = intent.getStringExtra("name");//从Intent中获取数据
- TextView text_show = (TextView) findViewById(R.id.text_show);
- text_show.setText("你好,"+yourname+"!"); //显示到屏幕上
- }
- }
package barry.android.intent;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class RecordActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.record); Intent intent = getIntent(); String yourname = intent.getStringExtra("name");//从Intent中获取数据 TextView text_show = (TextView) findViewById(R.id.text_show); text_show.setText("你好,"+yourname+"!"); //显示到屏幕上 }}及其布局文件 record.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/text_show"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text_show" android:layout_width="fill_parent" android:layout_height="fill_parent" /></LinearLayout>
需要注意的是,每个Activity都需要在AndroidManiFest.xml中声明一下,所以在AndroidManiFest.xml中需要加入:
- <activity android:name=".RecordActivity"></activity>
<activity android:name=".RecordActivity"></activity>
查看评论
- 1楼 ofacop2012-03-22 19:57发表 [回复] [引用][举报]
- 使用Intent在Activity间传值 .
- android Intent 在Activity间传值
- Android 使用Intent 在Activity 间跳转
- 使用Intent在activity之间传递数据
- 使用 Intent 在 Activity 之间穿梭
- 使用 Intent 在 Activity 之间穿梭
- activity intent使用
- 使用Intent启动Activity
- 使用Intent启动Activity
- 使用Intent启动Activity
- 使用Intent启动Activity
- 使用Intent启动Activity
- 我的Android笔记(三)——使用Intent在Activity间传值
- 使用Intent Bundle在Activity间传递数据
- Android开发之使用Intent在Activity之间传递数据
- Android笔记——在activity中使用intent
- android中使用Intent在activity之间传递数据
- Intent在Activity中的应用
- MFC原创教程:从不会开始(对话框2)
- [转]SUSE Linux Enterprise 10 安装 Oracle 10g (图形界面)
- string中的c_str()、data()、copy(p,n)函数的用法
- 读 Beginning Android Games 2nd Edition (十五) 游戏主菜单
- javascript中的event属性
- 使用Intent在Activity间传值 .
- diff与patch
- JRTPLIB存在问题吗?
- <script type="text/javascript">中后者是什么意思?
- pci cpu
- 企业各职位英文缩写
- C#实现Base64编码与解码
- 网络基础系列:网络协议
- 【利用OpenDataSource、OPENROWSET进行分布式查询和数据的导入导出】