• 关于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/




































  • 相关阅读:
    更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found
    解决Xcode 5下使用SVN出现 The operation couldn’t be completed. (NSURLErrorDomain error -1012.) 问题
    Android模拟器启动不了解决办法
    在AndroidManifest.xml文件中设置Android程序的启动界面方法
    Windows2008+MyEclipse10+Android开发环境搭配
    ADT下载地址整理
    android:inputType常用取值
    VS2010中使用Jquery调用Wcf服务读取数据库记录
    Linux手动安装netcore3.0
    StdOS之运行指示灯
  • 原文地址:https://www.cnblogs.com/yhc04161120/p/4841602.html
Copyright © 2020-2023  润新知