关闭

回传值

115人阅读 评论(0) 收藏 举报
分类:
回传值
    关键字
    requestCode(请求码)
    resultCode(回执码)
    data(数据 )其实就是intent 作为联系四大组件的枢纽,有它才能有数据
    
    发送端
    实现回传值得第一步
       startActivityForResult(intent,requestCode);//TODO 改换成startActivityForResult(intent,请求码)
    重写onActivityResult(int1,int2,intent)方法
    int1 是requestCode(请求码)
    int2 是resultCode(回执码)
    intent 是data(数据 )
    
      //TODO 重写onActivityResult(请求码,回执码,intent) 当有多个请求时,请求码是用来不同的请求,
    public void onActivityResult(int requestCode,int resultCode,Intent intent){
    
        super.onActivityResult(requestCode,resultCode,intent);
        
        switch(resultCode){//TODO 返回值 resultCode 用来决定使用case xxx
            case 111:
                String msg = intent.getStringExtra("MESSAGE");//TODO MESSAGE是回调的信息
                mEditText.setText("requestCode:"+requestCode+"resultCode"+resultCode);
                mEditText.append(msg);//TODO append添加多余信息
                break;
            case 222:
                String msg2 = intent.getStringExtra("MESSAGE2");//TODO MESSAGE是回调的信息
                mEditText.setText("requestCode:"+requestCode+"resultCode"+resultCode);
                mEditText.append(msg2);//TODO append添加多余信息
                break;
        }
    
    接受端
    设定返回值
      showaddress= (EditText)findViewById(R.id.intent_show_address);
      String str = showaddress.getText().toString()
    设定返回码
     resultCode = 111
     加载信息
     intent.putExta("msg",str);
     设定结果
      setResult(resultCode,intent);
      
       public void onClick(View view) {
        int id = view.getId();
        Intent intent = getIntent();
        String str = showaddress.getText().toString()+showbirth.getText().toString()+showsex.getText().toString();
        String str2 = showaddress.getText().toString()+showbirth.getText().toString();

        switch (id){
            case R.id.parameter_second_btn:
                resultCode = 111;//TODO 回执码,在返回时做判断
                intent.putExtra("MESSAGE",str);
                setResult(resultCode,intent);
                finish();
                break;
            case R.id.parameter_second_btn2:
                resultCode = 222;//TODO 回执码吧 成成
                intent.putExtra("MESSAGE2",str2);
                setResult(resultCode,intent);
                finish();
                break;
        }
在传递对象的时候
    对象应该实现接口Serializable或者接口Parcelable
    实现Serializable接口
    public class Student implements Serializable {
    String name;
    String birth;
    String sex;

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

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

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

        实现接口Parcelable
    public class Teacher implements Parcelable {

    String name;
    String birth;
     String sex;
     Teacher(String name,String birth,String sex){
        this.name = name;
        this.birth = birth;
        this.sex = sex;
    }

    protected Teacher(Parcel in) {
        name = in.readString();
        birth = in.readString();
        sex = in.readString();
    }

    public static final Creator<Teacher> CREATOR = new Creator<Teacher>() {
        @Override
        public Teacher createFromParcel(Parcel in) {
            return new Teacher(in);
        }

        @Override
        public Teacher[] newArray(int size) {
            return new Teacher[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(name);
        parcel.writeString(birth);
        parcel.writeString(sex);
    }
}    
    实现接口Parcelable看着挺多的其实,android studio 很强大,只要把开头public class Teacher implements Parcelable
    写出来,其他的android studio 都可以自动生成
    
   

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8750次
    • 积分:631
    • 等级:
    • 排名:千里之外
    • 原创:57篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章分类