Bundle的使用


     
     
注册界面布局:content_main.xml
<? xml version= "1.0" encoding= "utf-8" ?>

<TableLayout xmlns: android ="http://schemas.android.com/apk/res/android"

    android :layout_width= "match_parent"

    android :layout_height= "match_parent"

    android :orientation= "vertical">




    <TextView

        android :layout_width= "match_parent"

        android :layout_height= "wrap_content"

        android :text= "请输入您的注册信息"

        android :textSize= "20sp" />




    <TableRow>

        <TextView

            android :layout_width= "match_parent"

            android :layout_height= "wrap_content"

            android :text= "用户名:"

            android :textSize= "16sp" />

        <EditText

            android :id= "@+id/name"

            android :layout_width= "match_parent"

            android :layout_height= "wrap_content"

            android :hint= "请填写注册的账号"

            android :selectAllOnFocus= "true"/>

    </TableRow>




    <TableRow>

        <TextView

            android :layout_width= "match_parent"

            android :layout_height= "wrap_content"

            android :text= "密码:"

            android :textSize= "16sp" />

        <EditText

            android :id= "@+id/password"

            android :password= "true"

            android :layout_width= "match_parent"

            android :layout_height= "wrap_content"

            android :selectAllOnFocus= "true"/>

    </TableRow>

    <TableRow>

        <TextView

            android :layout_width= "match_parent"

            android :layout_height= "wrap_content"

            android :text= "性别:"

            android :textSize= "16sp" />

        <RadioGroup

            android :layout_width= "match_parent"

            android :layout_height= "wrap_content"

            android :orientation= "horizontal">

            <RadioButton

                android:id= "@+id/male"

                android:text= "男"

                android:textSize= "16sp"

                android:layout_width= "wrap_content"

                android:layout_height= "wrap_content" />

             <RadioButton

                 android:id= "@+id/female"

                 android:text= "女"

                 android:textSize= "16sp"

                 android:layout_width= "wrap_content"

                 android:layout_height= "wrap_content" />

        </RadioGroup>

    </TableRow>

        <Button

            android :id= "@+id/bn"

            android :text= "注册"

            android :textSize= "16sp"

            android :layout_width= "wrap_content"

            android :layout_height= "wrap_content" />




</TableLayout>



Person.java
package com.eson.activity ;

import java.io.Serializable ;

/**
* Created by Eson on 2016/5/21.
*/
public class Person implements Serializable {
    private String name;
    private String password ;
    private String gender ;

    public Person(String name , String password, String gender) {
        this . name = name ;
        this. password = password;
        this. gender = gender;
    }

    public String getName() {
        return name ;
    }

    public void setName (String name) {
        this . name = name ;
    }

    public String getPassword() {
        return password ;
    }

    public void setPassword (String password) {
        this . password = password ;
    }

    public String getGender() {
        return gender ;
    }

    public void setGender (String gender) {
        this . gender = gender ;
    }
}

主程序BundleTest.java,获取注册界面输入的内容并放进 Person.java中,在将数据放进Bundle里。
package com.eson.activity ;

import android.content.Intent ;
import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
import android.view.View ;
import android.widget.Button ;
import android.widget.EditText ;
import android.widget.RadioButton ;

public class BundleTest extends AppCompatActivity {

    private Button bn;
    private EditText name ;
    private EditText password ;
    private RadioButton male ;
    private String gender ;

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

        bn = (Button) findViewById(R.id. bn );
        name = (EditText) findViewById(R.id. name );
        password = (EditText) findViewById(R.id. password );
        male = (RadioButton) findViewById(R.id. male );
        bn .setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick (View v) {
                gender = male .isChecked()?"男" : "女";
                Person p=new Person( name.getText().toString() , password.getText().toString() , gender) ;
                //创建Bundle对象
                Bundle data=new Bundle() ;
                data.putSerializable("person" ,  p);
                //创建一个Intent
                Intent intent=new Intent(BundleTest. this,ResultActivity. class );
                intent.putExtras(data);
                //启动intent对应的Acticity
                startActivity(intent);
            }
        });
    }
}

用ResultActivity将获取到的数据显示在result.xml上
package com.eson.activity ;

import android.app.Activity ;
import android.content.Intent ;
import android.os.Bundle ;
import android.widget.TextView ;

/**
* Created by Eson on 2016/5/21.
*/
public class ResultActivity extends Activity {

    private TextView name;
    private TextView password ;
    private TextView gender ;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super .onCreate(savedInstanceState) ;
        setContentView(R.layout. result );
        name = (TextView) findViewById(R.id. name );
        password = (TextView) findViewById(R.id. password );
        gender = (TextView) findViewById(R.id. gender );
        //获取启动该Result的Intent
        Intent intent=getIntent() ;
        //直接通过Intent取出它所携带的Bundle数据包中的数据
        Person p= (Person) intent.getSerializableExtra( "person" );
        name .setText( "您的用户名为:" +p.getName()) ;
        password .setText( "您的密码为:" +p.getPassword());
        gender .setText( "你的性别为:" +p.getGender());
    }
}

result.xml 显示获取到的数据

<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns: android ="http://schemas.android.com/apk/res/android"
    android :layout_width= "match_parent"
    android :layout_height= "match_parent"
    android :orientation= "vertical">

    <TextView
        android :id= "@+id/name"
        android :textSize= "18sp"
        android :layout_width= "match_parent"
        android :layout_height= "wrap_content" />
    <TextView
        android :id= "@+id/password"
        android :textSize= "18sp"
        android :layout_width= "match_parent"
        android :layout_height= "wrap_content" />
    <TextView
        android :id= "@+id/gender"
        android :textSize= "18sp"
        android :layout_width= "match_parent"
        android :layout_height= "wrap_content" />
</LinearLayout>

最后在配置文件AndroidManifest.xml加入下面代码

<activity android :name=".ResultActivity" />

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值