从一个Activity向另一个Activity传递数据

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/top_text" />
    <RadioGroup
        android:id="@+id/gadiogroup" 
        android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@+id/textView1"
	    android:layout_marginLeft="80dp"
	    android:layout_marginTop="30dp"
	    android:orientation="horizontal">
	    <RadioButton
	        android:id="@+id/radioButton1"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/radioButton1_text" />
	
	    <RadioButton
	        android:id="@+id/radioButton2"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/radioButton2_text" />
   </RadioGroup>

   <TextView
       android:id="@+id/textView2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBottom="@+id/gadiogroup"
       android:layout_marginBottom="14dp"
       android:layout_marginRight="27dp"
       android:layout_toLeftOf="@+id/textView1"
       android:text="@string/sex_text" />
  <TextView
       android:id="@+id/textView3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBottom="@+id/gadiogroup"
       android:layout_marginBottom="-60dp"
       android:layout_marginRight="27dp"
       android:layout_toLeftOf="@+id/textView1"
       android:text="@string/shenggao_text" />

  <EditText
      android:id="@+id/edittext"
      android:layout_width="200dp"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView1"
      android:layout_alignTop="@+id/textView3"
      android:ems="10"
      android:inputType="number" />

  <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/edittext"
      android:layout_marginTop="32dp"
      android:layout_toRightOf="@+id/textView3"
      android:text="@string/button_text" />

</RelativeLayout>


activity_activity02.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Activity02" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="16dp"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="14dp"/>
    
</RelativeLayout>


MainActivity.java

我们在设计中添加了RadioButton控件,并将它放在了RadioGroup组中,这样来做选择男、女的性别比较简单、方便。但是我们知道RadioButton只能选中一个,所以,在MainActivity.java中就需要添加RadioButton的监听。

 

private RadioGroup rg;
private Button button;
static String sex;

rg = (RadioGroup)this.findViewById(R.id.gadiogroup);
        button = (Button)this.findViewById(R.id.button);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
		   switch(checkedId){
			case R.id.radioButton1:
				sex = "F";break;
			case R.id.radioButton2:
				sex = "M";break;
				}
			}
		});

添加完RadioButton的监听后,就需要添加按钮Button的click监听事件,在其中要把RadioButton的监听事件包含进去,并且还要向Activity02中传递数据。传递数据的时候就要用到bundle.putString(String key, String value)方法。

首先我来介绍一下传递数据的过程:

Bundle bundle = new Bundle();
bundle.putString("sex", "男性");
Intent intent = new Intent();
intent.putExtras(bundle);
intent.setClass(MainActivity.this, Activity02.class);
startActivity(intent);


在另一个Activity02中接受数据:

Intent intent = getIntent();
Bundle bundle=intent.getExtras();
String sex=bundle.getString("sex");
Toast.makeText(this, "您选择的是:"+sex, Toast.LENGTH_LONG);

这只是简单的数据传递过程,那么现在我们就把上边这个“计算标准体重”的案例的Button的click事件补充完整:

public class MainActivity extends Activity {
     private RadioGroup rg;
     private Button button;
     private EditText et;
     static String sex="F" ;
           @Override
     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        
           rg = (RadioGroup)this.findViewById(R.id.gadiogroup);
           button = (Button)this.findViewById(R.id.button);
           et = (EditText)this.findViewById(R.id.edittext);
       
        
           button.setOnClickListener(new OnClickListener() {
			
		@Override
		public void onClick(View v) {
			Bundle bundle = new Bundle();
			rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					
				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					switch(checkedId){
					case R.id.radioButton1:
						sex = "F";break;
					case R.id.radioButton2:
						sex = "M";break;
					}
						
				}
			});
				
			int a =Integer.parseInt(et.getText().toString()) ;
			if(sex == "F"){
			        String b = (a-80)*0.70+"";
			        bundle.putString("sex", "男性");
			        bundle.putString("result", b);
			}else if(sex == "M"){
			         String c = (a-70)*0.60+"";
				bundle.putString("sex", "女性");
				bundle.putString("result", c);
			}
				
			 bundle.putString("shengao", et.getText().toString());
				
			Intent intent = new Intent();
			intent.putExtras(bundle);
			intent.setClass(MainActivity.this, Activity02.class);
			startActivity(intent);
		}
	});
        
   }
}

Activity02.java

public class Activity02 extends Activity {

	private TextView text1,text2,text3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity02);
		
		text1 = (TextView)this.findViewById(R.id.textView1);
		text2 = (TextView)this.findViewById(R.id.textView2);
		text3 = (TextView)this.findViewById(R.id.textView3);
		Intent intent = getIntent();
	    Bundle bundle=intent.getExtras();
	    
		String sex=bundle.getString("sex");
		text1.setText("您选择的是"+sex);
		
		String shengao=bundle.getString("shengao");
		text2.setText("您的身高是"+shengao+"厘米");
		
		String result=bundle.getString("result");
		text3.setText("您的标准体重是:"+result+"公斤");
	}
}



这样,你就可以做一个Android应用小程序-----《计算你的标准体重》,还等什么,赶快行动吧。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值