一下内容均可全部复制过去直接使用
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;
}
}