打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
android 开发之activity之间传递数据

在android中,我们经常会从一个activity将数据传递到另外的一个activity中,而且还要从另外的activity中获取返回的数据,

其实在android中有很多方法可以做到,比如使用Application或者是Bundle来进行数据传递.今天就看看使用Bundle来进

行数据传递的例子.

这个例子里面使用了Bundle从第一个activity传递了数据到第二个里面,而第二个处理完之后,又将数据返回给了第一个.

具体实现请看代码

---------------------ActivityBundle.java------------------------

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;import android.widget.RadioButton;public class ActivityBundle extends Activity{	private RadioButton rbMan;	private RadioButton rbWoman;	private EditText editHight;	private Button btnOK;	private double body_height;		@Override	protected void onActivityResult(int requestCode, int resultCode, Intent data)	{		switch (resultCode)		{			//结果返回		case RESULT_OK:			//获取Bundle的数据			Bundle bl= data.getExtras();			String sex=bl.getString("sex");			if (sex.equals("M"))			{				rbMan.setChecked(true);			}			else			{				rbWoman.setChecked(true);			}			body_height=bl.getDouble("height");			editHight.setText(""+body_height);			break;		default:			break;		}	}		@Override	public void onCreate(Bundle savedInstanceState)	{		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		btnOK = (Button) findViewById(R.id.btn_calc);		rbMan = (RadioButton) findViewById(R.id.radio_man);		rbWoman = (RadioButton) findViewById(R.id.radio_woman);		editHight = (EditText) findViewById(R.id.text_edit);		btnOK.setOnClickListener(new OnClickListener()		{			@Override			public void onClick(View v)			{				try				{					body_height = Double.parseDouble(editHight.getText()							.toString());				}				catch (Exception e)				{					body_height = 0;				}				String sex = "";				if (rbMan.isChecked())				{					sex = "M";				}				else				{					sex = "F";				}				//通过intent跳转到下一个页面.				Intent intent = new Intent();				intent.setClass(ActivityBundle.this, ResultActivity.class);				//通过Bundle来获取数据,通过key-Value的方式放入数据				Bundle bl = new Bundle();				bl.putDouble("height", body_height);				bl.putString("sex", sex);				//将Bundle放入Intent传入下一个Activity				intent.putExtras(bl);				//跳到下一个Activity,并且等待其返回结果				startActivityForResult(intent, 0);				//不能够在这个Activity调用了startActivityForResult之后调用finsh()				//否则无法接收到返回			}		});	}}

-----------------------ResultActivity.java----------------------

import java.text.DecimalFormat;import java.text.NumberFormat;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.TextView;public class ResultActivity extends Activity{	Bundle bl;	Intent intent;	Button btn;	@Override	protected void onCreate(Bundle savedInstanceState)	{		super.onCreate(savedInstanceState);		setContentView(R.layout.result);		//获取到上一个页面传过来的Intent		intent=this.getIntent();		//获取Intent中的Bundle数据		bl=intent.getExtras();		String sex=bl.getString("sex");		double height=bl.getDouble("height");		String sexText="";		if (sex.equals("M"))		{			sexText="男性";		}		else		{			sexText="女性";		}				String weight=getWeight(sex,height);		TextView tv1=(TextView)findViewById(R.id.TextView01);		tv1.setText("你是一位"+sexText);		TextView tv2=(TextView)findViewById(R.id.TextView02);		tv2.setText("你的身高是"+height+"厘米");		TextView tv3=(TextView)findViewById(R.id.TextView03);		tv3.setText("你的标准体重是"+weight+"公斤");		btn=(Button)findViewById(R.id.btn_back);		btn.setOnClickListener(new OnClickListener()		{			@Override			public void onClick(View arg0)			{				//将intent传会上一个Activity				ResultActivity.this.setResult(RESULT_OK, intent);				ResultActivity.this.finish();			}					});	}		private String format(double num)	{		NumberFormat nf =new DecimalFormat("0.00");		String s=nf.format(num);		return s;	}		private String getWeight(String sex,double height)	{		String weight="";		if (sex.equals("M"))		{			weight=format((height-80)*0.7);		}		else		{			weight=format((height-70)*0.6);		}		return weight;	}}---------------mann.xml-----------------
<?xml version="1.0" encoding="utf-8"?><RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:paddingBottom="4dip"       android:paddingLeft="12dip"    android:paddingTop="4dip"    android:paddingRight="12dip"    ><TextView  	android:id="@+id/title_view"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /><TextView	android:id="@+id/text_title"	android:layout_width="fill_parent"	android:layout_height="wrap_content"	android:textSize="20sp"	android:layout_below="@+id/title_view"	android:text="@string/str_text"	/>	<TextView	android:id="@+id/text_weight"	android:layout_width="wrap_content"	android:layout_height="wrap_content"	android:textSize="20sp"	android:layout_below="@+id/text_title"	android:text="@string/str_weight"	/>		<RadioGroup 	android:id="@+id/RadioGroup01" 	android:layout_width="wrap_content" 	android:layout_below="@+id/text_title"	android:layout_toRightOf="@+id/text_weight" 	android:orientation="horizontal"	android:checkedButton="@+id/radio_man"	android:layout_height="wrap_content">		<RadioButton		android:id="@+id/radio_man"		android:text="@string/str_man"	/>		<RadioButton		android:id="@+id/radio_woman"		android:text="@string/str_woman"	/>	</RadioGroup><TextView	android:id="@+id/text_height"	android:layout_width="wrap_content"	android:layout_height="wrap_content"	android:textSize="20sp"	android:layout_below="@+id/RadioGroup01"	android:text="@string/str_height"	/>		<EditText	android:id="@+id/text_edit"	android:layout_width="200dip"	android:layout_height="wrap_content"	android:layout_below="@+id/RadioGroup01"	android:layout_toRightOf="@+id/text_height"		android:numeric="decimal"/><Button 	android:layout_width="wrap_content" 	android:text="@string/str_calc" 	android:layout_below="@+id/text_edit" 	android:id="@+id/btn_calc" 	android:layout_toRightOf="@+id/text_height" 	android:layout_height="wrap_content">	</Button><TextView	android:id="@+id/text_cm"	android:layout_width="wrap_content"	android:layout_height="wrap_content"	android:layout_below="@+id/RadioGroup01"	android:textSize="20sp"	android:layout_toRightOf="@+id/text_edit"	android:text="@string/str_cm"/>		</RelativeLayout>

----------------result.xml-----------------

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"	android:layout_width="wrap_content" android:layout_height="wrap_content">	<TableLayout android:id="@+id/TableLayout01"		android:layout_width="wrap_content" android:layout_height="wrap_content">		<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"			android:layout_height="wrap_content">			<RelativeLayout android:id="@+id/RelativeLayout01"				android:paddingLeft="12dip" android:paddingRight="12dip"				android:paddingTop="10dip" android:paddingBottom="10dip"				android:layout_width="wrap_content" android:layout_height="wrap_content">				<TextView android:id="@+id/TextView01"					android:layout_width="fill_parent" android:layout_height="wrap_content"					android:textSize="20sp">				</TextView>			</RelativeLayout>		</TableRow>		<TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content"			android:layout_height="wrap_content">			<RelativeLayout android:id="@+id/RelativeLayout02"				android:paddingLeft="12dip" android:paddingRight="12dip"				android:paddingTop="10dip" android:paddingBottom="10dip"				android:layout_width="wrap_content" android:layout_height="wrap_content">				<TextView android:id="@+id/TextView02"					android:layout_width="wrap_content" android:layout_height="wrap_content"					android:textSize="20sp">				</TextView>			</RelativeLayout>		</TableRow>		<TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content"			android:layout_height="wrap_content">			<RelativeLayout android:id="@+id/RelativeLayout03"				android:paddingLeft="12dip" android:paddingRight="12dip"				android:paddingTop="10dip" android:paddingBottom="10dip"				android:layout_width="wrap_content" android:layout_height="wrap_content">				<TextView android:id="@+id/TextView03"					android:layout_width="wrap_content" android:layout_height="wrap_content"					android:textSize="20sp"></TextView>			</RelativeLayout>		</TableRow>		<TableRow android:id="@+id/TableRow04" android:layout_width="wrap_content"			android:layout_height="wrap_content">			<RelativeLayout android:id="@+id/RelativeLayout04"				android:paddingLeft="12dip" android:paddingRight="12dip"				android:paddingTop="10dip" android:paddingBottom="10dip"				android:layout_width="wrap_content" android:layout_height="wrap_content">				<TextView android:id="@+id/TextView04"					android:layout_width="wrap_content" android:layout_height="wrap_content"					android:textSize="20sp">				</TextView>			</RelativeLayout>		</TableRow>		<TableRow android:id="@+id/TableRow05" android:layout_width="wrap_content"			android:layout_height="wrap_content">			<RelativeLayout android:id="@+id/RelativeLayout05"				android:paddingLeft="40dip" android:paddingRight="40dip"				android:paddingTop="10dip" android:paddingBottom="10dip"				android:layout_width="wrap_content" android:layout_height="wrap_content">				<Button android:text="@string/back_view" android:id="@+id/btn_back"					android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>			</RelativeLayout>		</TableRow>	</TableLayout></LinearLayout>

---------------AndroidManifest.xml----------------

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="activity.bundle"      android:versionCode="1"      android:versionName="1.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".ActivityBundle"                  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="ResultActivity"/>    </application></manifest> 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
怎么给android 设置边框(tableLayout、表格)
Android 设置页面UI设计
AndroidUI优化4
新浪微博布局学习——活用RelativeLayout
TabActivity的美化
Android 设置主题实现点击波纹效果
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服