zoukankan      html  css  js  c++  java
  • android 开发之activity之间传递数据

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

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

    行数据传递的例子.

    1 2

    这个例子里面使用了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> 
  • 相关阅读:
    第十四周课程总结&实验报告(简单记事本的实现)
    第十三周课程总结
    第十二周课程总结
    第十一周课程总结
    第十周课程总结
    第九周课程总结&实验报告(七)
    第八周课程总结&实验报告(六)
    第七周课程总结&试验报告(五)
    基于C的
    RMQ 区间最值问题
  • 原文地址:https://www.cnblogs.com/sun_catboy/p/1709869.html
Copyright © 2011-2022 走看看