zoukankan      html  css  js  c++  java
  • 关于Bundle传递消息

    定义引用三个组件
    EditText name = (EditText)findViewById(R.id.name);
    EditText passwd = (EditText)findViewById(R.id.passwd);
    RadioButton male = (RadioButton) findViewById(R.id.male);
    String gender = male.isChecked() ? "男 " : "女";
    Person p = new Person(name.getText().toString(), passwd
    .getText().toString(), gender);

    /***********id.getText()此方法得到相应输入框的内容

    data.putSerializable("person", p);
    "person"为可序列化数据包的key只是作为区分传送数据包的标志。
    p为传送的对象。

    利用Intent发送相关数据包,需要建立一个新的Intent的连接。
    利用Intent的putExtras(data);发送数据包。

    *******************/

    // 创建一个Bundle对象
    Bundle data = new Bundle();
    data.putSerializable("person", p);
    // 创建一个Intent
    Intent intent = new Intent(MainActivity.this,
    ResultActivity.class);

    intent.putExtras(data);
    // 启动intent对应的Activity
    startActivity(intent);


    /**************************************************************

    以上为发送数据包相关代码。下面写接受数据包以及显示数据
    ***************************************************************/
    定义三个显示文本组件
    TextView name = (TextView) findViewById(R.id.name);
    TextView passwd = (TextView) findViewById(R.id.passwd);
    TextView gender = (TextView) findViewById(R.id.gender);

    // 建立该Activity的Intent,作用是为了连接与上一个Activity的数据接收
    Intent intent = getIntent();

    // 利用Intent接收到的数据包,指定给新建对象
    Person p = (Person) intent.getSerializableExtra("person");

    /******id.setText方法设置显示发送过来的数据内容*********************/
    name.setText("您的用户名为:" + p.getName());
    passwd.setText("您的密码为:" + p.getPasswd());
    gender.setText("您的性别为:" + p.getGender());


    /************下面是该对象类方法的声明********************************/
    import java.io.Serializable;

    public class Person implements Serializable{
    private Integer id;
    private String name;
    private String passwd;
    private String gender;

    public Person(String name, String passwd, String gender) {
    this.name = name;
    this.passwd = passwd;
    this.gender = gender;
    }
    /***************下面省略大部分相关类方法,getName();setName();getGender();setGender();等**********************************8/




































  • 相关阅读:
    linux execl()函数 关于execl()函数族的用法不在赘述,
    mysql日期格式化
    JavaScript超越了Java,c,python等等成为Stack Overflow上最热门的
    GO语言web框架Gin之完全指南
    计算机专业四年本科的课程表是什么样的?
    文化常识大全201901 普通老百姓交的朋友谓“布衣之交”
    JavaScript超越了Java,c,python等等成为Stack Overflow上最热门的
    linux bash吧,还有啥Bourne Again Shell
    TCP、UDP服务器模型 在网络程序里面,通常都是一
    SpringMVC常见面试题总结(超详细回答)
  • 原文地址:https://www.cnblogs.com/yhc04161120/p/4841602.html
Copyright © 2011-2022 走看看