在Layout下创建两个xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_equip"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.itcast.select.equip.EquipActivity">
<ImageView
android:id="@+id/TP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:layout_marginTop="30dp"
android:src="@drawable/baby"
/>
<TextView
android:id="@+id/wz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="亲爱的小宝宝!"
android:textSize="20dp"
android:layout_marginTop="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="@color/colorAccent"></TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/shengming"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="生命值"
android:textColorHint="@color/colorPrimary"
android:textSize="20dp"/><TextView />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"/>
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:id="@+id/shengmingzhi"
android:text="0"
android:layout_gravity="center"
android:textColor="@color/colorAccent"></TextView>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/gongjili"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="攻击力"
android:textColorHint="@color/colorPrimary"
android:textSize="20dp"/><TextView />
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"/>
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:id="@+id/gongjizhi"
android:text="0"
android:layout_gravity="center"
android:textColor="@color/colorAccent"></TextView>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/minjiedu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="敏捷度"
android:textColorHint="@color/colorPrimary"
android:textSize="20dp"/><TextView />
<ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"/>
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:id="@+id/minjiezhi"
android:text="0"
android:layout_gravity="center"
android:textColor="@color/colorAccent"></TextView>
</TableRow>
</LinearLayout>
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="3dp"
android:background="#FF1"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:onClick="click"
android:text="快给小宝宝购买装备啊"
android:textSize="20dp"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_equip"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:oriention="vertial"
tools:context="cn.itcast.select.equip.EquipActivity">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:textSize="30dp"
android:textColor="#FFCCOO"
android:text="欢迎进入商店" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:layout_below="@id/tv_name">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/huanying"
android:drawableLeft="@drawable/cx"
android:text="敏捷+60,攻击+30,防御+10"
android:textSize="20dp"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/shangpin"
android:drawableLeft="@drawable/tb"
android:text="敏捷+10,攻击+50,防御+10"
android:textSize="20dp"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购买"
android:textSize="20dp"
android:textColor="#FC1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"/>
</RelativeLayout>
新建一个包,里面新建一个iteminfo的类,同来存储装备信息
package cn.itcast.domain;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class ItemInfo extends AppCompatActivity {
private String name;
private int acctack;
private int life;
private int speed;
public ItemInfo(String name,int acctack,int life,int speed){
this.name=name;
this.acctack=acctack;
this.life=life;
this.speed=speed;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getAcctack(){
return acctack;
}
public void setAcctack(int acctack){
this.acctack=acctack;
}
public int getLife(){
return life;
}
public void setLife(int life){
this.life=life;
}
public int getSpeed(){
return speed;
}
public void setSpeed(int speed){
this.speed=speed;
}
public String toString(){
return " [name="+name+",acctack="+acctack+",life="+life+",speed="+speed+"]";
}
}
创建Activity
package cn.itcast.select.equip;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class EquipActivity extends AppCompatActivity {
private ProgressBar mProgressBar1;
private ProgressBar mProgressBar2;
private ProgressBar mProgressBar3;
private TextView mLifeTV;
private TextView mAttackTV;
private TextView mSpeedTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_equip);
TextView mLifeTV=(TextView)findViewById(R.id.tv_life_progress);
TextView mSpeedTV=(TextView)findViewById(R.id.tv_speed_progress);
TextView mAttackTV=(TextView)findViewById(R.id.tv_attack_progress);
initProgress();
}
private void initProgress(){
mProgressBar1=(mProgressBar)findViewById(R.id.progressBar1);
mProgressBar2=(mProgressBar)findViewById(R.id.progressBar2);
mProgressBar3=(mProgressBar)findViewById(R.id.progressBar3);
mProgressBar1.setMax(1000);
mProgressBar2.setMax(1000);
mProgressBar3.setMax(1000);
}
public void click(View view){
Intent intent=new Intent(this,ShopActivity.class);
startActivityForResult(intent,1);
}
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(data!=null){
if(resultCode==1){
if(requestCode==1)
ItemInfo info(ItemInfo)data.getSerializableExtra("equipment");
updateProgress(info);
}
}
}
}
private void updateProgress(ItemInfo info){
int progress1=mProgressBar1.getProgress();
int progress2=mProgressBar2.getProgress();
int progress3=mProgressBar3.getProgress();
mProgressBar1.setProgress(progress1+info.getLife());
mProgressBar2.setProgress(progress2+info.getAcctack());
mProgressBar3.setProgress(progress3+info.getSpeed());
mLifeTV.setText(mProgressBar1.getProgress()+"");
mAttackTV.setText(mProgressBar1.getProgress()+"");
mSpeedTV.setText(mProgressBar1.getProgress()+"");
}
}
package cn.itcast.select.equip;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ShopActivity extends AppCompatActivity {
private ItemInfo itemInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
itemInfo=new ItemInfo("jinjian",100,20,20);
findViewById(R.id.r1).setOnClickListener(this);
TextView mLifeTV=(TextView)findViewById(R.id.tv_life);
TextView mNameTV=(TextView)findViewById(R.id.tv_name);
TextView mSpeedTV=(TextView)findViewById(R.id.tv_speed);
TextView mAttackTV=(TextView)findViewById(R.id.tv_attack);
mLifeTV.setText("生命值+"itemInfo.getLife());
mNameTV.setText(itemInfo.getName()+"");
mSpeedTV.setText("敏捷度+"itemInfo.getSpeed());
mAttackTV.setText("攻击力+"itemInfo.getAcctack());
}
public void onClick(View view){
switch (view.getId()){
case R.id.r1:
Intent intent=new Intent();
intent.putExtra("equipment",itemInfo);
setResult(1,intent);
finish();
break;
}
}
}
实验结果图