Android中Activity跳转后回传返回值

一下内容均可全部复制过去直接使用

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.shopreturn.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="wrap_content"
            android:orientation="horizontal"
            android:layout_height="wrap_content">
                <TextView
                    android:text="生命值"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
                <ProgressBar
                    android:id="@+id/ProgressBar_life"
                    android:progress="0"
                    android:max="100"
                    style="@android:style/Widget.ProgressBar.Horizontal"
                    android:layout_width="300dp"
                    android:layout_height="wrap_content" />
                <TextView
                    android:id="@+id/text_life"
                    android:text="0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
        </LinearLayout>
        <Button
            android:id="@+id/btn1"
            android:text="购买装备"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_height="wrap_content">
            <TextView
                android:text="生命护甲购买成功!生命值加20"
                android:textSize="20sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Button
                android:id="@+id/returns"
                android:text="返回属性页"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.hp.shopreturn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;

public class MainActivity extends Activity {
    Button button;
    ProgressBar progressBar;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.btn1);
        progressBar = findViewById(R.id.ProgressBar_life);
        textView = findViewById(R.id.text_life);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,shop.class);
                startActivityForResult(intent,1);
            }
        });
    }
    private int life = 0;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1 && resultCode==1){
            Bundle bundle = data.getExtras();
            iteminfo iteminfo = (com.example.hp.shopreturn.iteminfo) bundle.getSerializable("iteminfo");
            life+=iteminfo.getLife();
            progressBar.incrementProgressBy(life);
            textView.setText(""+life);
        }
    }
}

shop.java

package com.example.hp.shopreturn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 *
 * Created by hp on 2018/11/7.
 */

public class shop extends Activity {
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
/*        Intent intent = getIntent();
        Bundle bundle = new Bundle();
        bundle.putInt("Image",a[i]);
        intent.putExtras(bundle);
        setResult(1,intent);
        finish();*/
        button = findViewById(R.id.returns);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = getIntent();
                Bundle bundle = new Bundle();
                iteminfo iteminfo = new iteminfo(20);
                bundle.putSerializable("iteminfo",iteminfo);
                intent.putExtras(bundle);
                setResult(1,intent);
                finish();
            }
        });
    }
}

iteminfo.java

package com.example.hp.shopreturn;

import java.io.Serializable;

/**
 *
 * Created by hp on 2018/11/7.
 */

public class iteminfo implements Serializable {
    private int life;
    public int getLife() {
        return life;
    }

    public iteminfo(int life){
        this.life = life;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值