• Bundle对象实现不同Activity之间数据传递


    res/layout/ex03_10.xml

    <?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <RadioGroup android:layout_height="wrap_content" 
        			android:layout_width="wrap_content" 
        			android:id="@+id/rgSex" 
        			android:orientation="horizontal" 
        			android:layout_x="100dip" 
        			android:layout_y="60dip">
            <RadioButton android:text="@string/sex_man" 
            		     android:layout_height="wrap_content" 
            		     android:layout_width="wrap_content" 
            		     android:checked="true" 
            		     android:id="@+id/radio_man"></RadioButton>
            <RadioButton android:text="@string/sex_woman" 
            			 android:layout_height="wrap_content" 
            			 android:layout_width="wrap_content" 
            			 android:id="@+id/radio_woman"></RadioButton>
        </RadioGroup>
        <EditText android:layout_height="wrap_content" 
        		  android:layout_width="wrap_content" 
        		  android:text="EditText" 
        		  android:id="@+id/etWeight" 
        		  android:layout_x="97dip" 
        		  android:layout_y="133dip"></EditText>
        <Button android:text="Button" 
        		android:layout_height="wrap_content" 
        		android:layout_width="wrap_content" 
        		android:id="@+id/btn" 
        		android:layout_x="120dip" 
        		android:layout_y="212dip"></Button>
    </AbsoluteLayout>

    res/layout/ex03_10_01.xml

    <?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
        <TextView android:layout_height="wrap_content" 
        	      android:layout_width="wrap_content" 
        	      android:text="TextView" 
        	      android:layout_x="126dip" 
        	      android:layout_y="144dip" 
        	      android:id="@+id/tvResult"></TextView>
    </AbsoluteLayout>

    src/EX03_10.java

    package gphone.ex03_10;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioGroup;
    public class EX03_10 extends Activity {
    	Button btn=null;
    	EditText etWeight=null;
    	RadioGroup rgSex=null;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ex03_10);
            btn=(Button)findViewById(R.id.btn);
            etWeight=(EditText)findViewById(R.id.etWeight);
            rgSex=(RadioGroup)findViewById(R.id.rgSex);
            btn.setOnClickListener(new Button.OnClickListener(){
    
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				String strWeight=etWeight.getText().toString();
    				
    				String strSex="女";
    				if(rgSex.getCheckedRadioButtonId()==R.id.radio_man)
    				{
    					strSex="男";
    				}
    				else
    				{
    					strSex="女 ";
    				}
    				Bundle b=new Bundle();
    				b.putString("Weight", strWeight);
    				b.putString("Sex", strSex);
    				Intent intent=new Intent();
    				intent.putExtras(b);
    				intent.setClass(EX03_10.this,EX03_10_01.class);
    				startActivity(intent);
    				
    			}
            	
            });
        }
    }

    src/EX03_10_01.java

    package gphone.ex03_10;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class EX03_10_01 extends Activity{
    	TextView tvResult=null;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.ex03_10_01);
    		tvResult=(TextView)findViewById(R.id.tvResult);
    		
    		Bundle b=this.getIntent().getExtras();
    		String strWeight=b.getString("Weight");
    		String strSex=b.getString("Sex");
    		tvResult.setText("结果为:"+strWeight+","+strSex);
    	}
    
    }

    运行结果

    魔乐截屏 魔乐截屏(1)

  • 相关阅读:
    快手记录的面试题2
    快手Java实习一二面经(记录的面试题1)
    219. 存在重复元素 II(面试题也考过)
    117. 填充每个节点的下一个右侧节点指针 II(没想到,但是其实蛮简单的)
    116. 填充每个节点的下一个右侧节点指针
    最后来几个快手的面试题吧,先记录下来大概看看
    快手Java实习一二面面经(转载)
    双亲委派模型
    聚集索引与非聚集索引总结(转载)
    136. 只出现一次的数字
  • 原文地址:https://www.cnblogs.com/AlexCheng/p/2120047.html
Copyright © 2020-2023  润新知