不同Activity之间的数据传递

本例子使用import android.os.Bundle对象进行数据封装,点击按钮时将数据传递到另一个Activity进行显示。

1、主界面xml代码:

<LinearLayout 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:orientation="vertical"
   
android:padding="10dp">
    
   
 
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:" />
        <EditText
            android:id="@+id/name" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="none"/>
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学号:" />
        <EditText
            android:id="@+id/number" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别:" />
        <RadioGroup 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:checkedButton="@+id/male">
            <RadioButton
                android:id="@+id/male" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男"/>
            <RadioButton
                android:id="@+id/female"  
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"/>
        </RadioGroup>
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="兴趣:" />
        <CheckBox
            android:id="@+id/sport" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="运动"/>
        <CheckBox
            android:id="@+id/music" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="音乐"/>
        <CheckBox
            android:id="@+id/reading" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="读书"/>
    </LinearLayout>

    <Button 
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交"/>

</LinearLayout>


2、java代码:

public class MainActivity extends Activity {

    private EditText et_name ;
    private EditText et_number;
    private RadioButton rb_male;
    //private RadioButton rb_female;
    private CheckBox cb_sport;
    private CheckBox cb_music;
    private CheckBox cb_read;
    private Button btn_submit;

    public void init()
    {
        et_name = (EditText)findViewById(R.id.name);
        et_number = (EditText)findViewById(R.id.number);
        rb_male = (RadioButton)findViewById(R.id.male);
        //rb_female = (RadioButton)findViewById(R.id.female);
        cb_sport = (CheckBox)findViewById(R.id.sport);
        cb_read = (CheckBox)findViewById(R.id.reading);
        cb_music = (CheckBox)findViewById(R.id.music);
        btn_submit = (Button)findViewById(R.id.submit);
        btn_submit.setOnClickListener(new ButtonListener());
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

public class ButtonListener implements OnClickListener
{
    @Override
    public void onClick(View v) {
       StringBuilder sb = new StringBuilder();
       if(et_name.getText().toString()==null || et_name.getText().toString().length()<1)
       {
           Toast.makeText(MainActivity.this, "姓名不能为空", Toast.LENGTH_LONG).show();
           return;
       }
       if(et_number.getText().toString()==null || et_number.getText().toString().length()<1)
       {
           Toast.makeText(MainActivity.this, "学号不能为空", Toast.LENGTH_LONG).show();
           return;
       }

       sb.append("您的姓名:").append(et_name.getText().toString()).append("\n")
       .append("学号:").append(et_number.getText().toString()).append("\n")
       .append("性别:").append(rb_male.isChecked()?"男":"女").append("\n")
       .append("爱好:").append(cb_sport.isChecked()?"运动 ":"").append(cb_music.isChecked()?"音乐":"")
.append (cb_read.isChecked()?"阅读 ":"");

       Intent intent = new Intent();
       intent.setClass(MainActivity.this, AnotherActivity.class);
       Bundle bundle = new Bundle();
       bundle.putString("data", sb.toString());
       intent.putExtras(bundle);

       startActivity(intent);
       MainActivity.this.finish();
    }
  }
}


3、另一Activity的界面只包含一个TextView控件来显示传递过来的数据。(略)


4、另一个Activity接收数据的java代码:

public class AnotherActivity extends Activity {

private TextView tv_data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_another);

    Bundle b = getIntent().getExtras();
    String str = b.getString("data");
    tv_data = (TextView)findViewById(R.id.data);
    tv_data.setText(str);
  }
}

 

5、配置AndroidManifest.xml

<activity
    android:name="com.czh.bundledemo.AnotherActivity"
    android:label="@string/app_name" >
</activity>


6、运行结果:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值