效果图:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ProgressBar manlifeProgressbar;
private ProgressBar manattackProgressbar;
private ProgressBar manspeedProgressbar;
private TextView manlifeTV;
private TextView manattackTV;
private TextView manspeedTV;
private ProgressBar petlifeProgressbar;
private ProgressBar petattackProgressbar;
private ProgressBar petspeedProgressbar;
private TextView petlifeTV;
private TextView petattackTV;
private TextView petspeedTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manlifeTV = findViewById(R.id.tv_manlife_progress);
manattackTV = findViewById(R.id.tv_manattack_progress);
manspeedTV = findViewById(R.id.tv_manspeed_progress);
petlifeTV = findViewById(R.id.tv_petlife_progress);
petattackTV = findViewById(R.id.tv_petattack_progress);
petspeedTV = findViewById(R.id.tv_petspeed_progress);
initProgress();
}
private void initProgress() {
manlifeProgressbar = findViewById(R.id.manlife_progressBar);
manattackProgressbar = findViewById(R.id.manattack_progressBar);
manspeedProgressbar = findViewById(R.id.manspeed_progressBar);
manlifeProgressbar.setMax(1000);
manattackProgressbar.setMax(1000);
manspeedProgressbar.setMax(1000);
petlifeProgressbar = findViewById(R.id.petlife_progressBar);
petattackProgressbar = findViewById(R.id.petattack_progressBar);
petspeedProgressbar = findViewById(R.id.petspeed_progressBar);
petlifeProgressbar.setMax(1000);
petattackProgressbar.setMax(1000);
petspeedProgressbar.setMax(1000);
}
public void click(View view){
Intent intent = new Intent(this,ShopActivity.class);
int id = view.getId();
if (id == R.id.btn_baby){
startActivityForResult(intent,2);
}else if (id == R.id.btn_master){
startActivityForResult(intent,1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null){
if (requestCode == 1){
ItemInfo info = (ItemInfo) data.getSerializableExtra("equipment");
updateMasterProgress(info);
}else if (requestCode == 2){
ItemInfo info = (ItemInfo) data.getSerializableExtra("equipment");
updatePetProgress(info);
}
}
}
private void updateMasterProgress(ItemInfo info) {
int progressLife = manlifeProgressbar.getProgress();
int progressAttack = manattackProgressbar.getProgress();
int progressSpeed = manspeedProgressbar.getProgress();
manlifeProgressbar.setProgress(progressLife+info.getLife());
manattackProgressbar.setProgress(progressAttack+info.getAttack());
manspeedProgressbar.setProgress(progressSpeed+info.getSpeed());
manlifeTV.setText(manattackProgressbar.getProgress()+"");
manattackTV.setText(manattackProgressbar.getProgress()+"");
manspeedTV.setText(manspeedProgressbar.getProgress()+"");
}
private void updatePetProgress(ItemInfo info) {
int progressLife = petlifeProgressbar.getProgress();
int progressAttack = petattackProgressbar.getProgress();
int progressSpeed = petspeedProgressbar.getProgress();
petlifeProgressbar.setProgress(progressLife+info.getLife());
petattackProgressbar.setProgress(progressAttack+info.getAttack());
petspeedProgressbar.setProgress(progressSpeed+info.getSpeed());
petlifeTV.setText(petlifeProgressbar.getProgress()+"");
petattackTV.setText(petattackProgressbar.getProgress()+"");
petspeedTV.setText(petspeedProgressbar.getProgress()+"");
}
}
ShopActivity.java
public class ShopActivity extends AppCompatActivity implements View.OnClickListener {
private ItemInfo itemInfo1;
private ItemInfo itemInfo2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
itemInfo1 = new ItemInfo("金剑",100,20,20);
findViewById(R.id.ly_111).setOnClickListener(this);
TextView mLifeTV1 = findViewById(R.id.tv_life1);
TextView mNameTV1 = findViewById(R.id.tv_name1);
TextView mSpeedTV1 = findViewById(R.id.tv_speed1);
TextView mAttackTV1 = findViewById(R.id.tv_attack1);
mLifeTV1.setText("生命值+"+itemInfo1.getLife());
mNameTV1.setText(itemInfo1.getName()+"");
mSpeedTV1.setText("敏捷值+"+itemInfo1.getSpeed());
mAttackTV1.setText("攻击力+"+itemInfo1.getAttack());
itemInfo2 = new ItemInfo("金盾",30,200,30);
findViewById(R.id.ly_112).setOnClickListener(this);
TextView mLifeTV2 = findViewById(R.id.tv_life2);
TextView mNameTV2 = findViewById(R.id.tv_name2);
TextView mSpeedTV2 = findViewById(R.id.tv_speed2);
TextView mAttackTV2 = findViewById(R.id.tv_attack2);
mLifeTV2.setText("生命值+"+itemInfo2.getLife());
mNameTV2.setText(itemInfo2.getName()+"");
mSpeedTV2.setText("敏捷值+"+itemInfo2.getSpeed());
mAttackTV2.setText("攻击力+"+itemInfo2.getAttack());
}
@Override
public void onClick(View view) {
Intent intent = new Intent();
switch (view.getId()){
case R.id.ly_111:
intent.putExtra("equipment",itemInfo1);
setResult(1,intent);
finish();
break;
case R.id.ly_112:
intent.putExtra("equipment",itemInfo2);
setResult(2,intent);
finish();
break;
}
}
}
ItemInfo.java
public class ItemInfo implements Serializable {
private String name;
private int attack;
private int life;
private int speed;
public ItemInfo(String name,int attack,int life,int speed){
this.name = name;
this.attack = attack;
this.life = life;
this.speed = speed;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAttack(){
return attack;
}
public void setAttack(int attack){
this.attack = attack;
}
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;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:fadingEdge="vertical"
android:scrollbars="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/pet_imgv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/pet" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="生命值:"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ProgressBar
android:id="@+id/petlife_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2" />
<TextView
android:id="@+id/tv_petlife_progress"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="攻击力:"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ProgressBar
android:id="@+id/petattack_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2" />
<TextView
android:id="@+id/tv_petattack_progress"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="敏捷:"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ProgressBar
android:id="@+id/petspeed_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2" />
<TextView
android:id="@+id/tv_petspeed_progress"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="#000000" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/btn_baby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@android:drawable/ic_menu_add"
android:onClick="click"
android:text="给小宝宝购买装备"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/man_imgv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/man" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="生命值:"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ProgressBar
android:id="@+id/manlife_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2" />
<TextView
android:id="@+id/tv_manlife_progress"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="攻击力:"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ProgressBar
android:id="@+id/manattack_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2" />
<TextView
android:id="@+id/tv_manattack_progress"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="敏捷:"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ProgressBar
android:id="@+id/manspeed_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2" />
<TextView
android:id="@+id/tv_manspeed_progress"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="#000000" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/btn_master"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@android:drawable/ic_menu_add"
android:onClick="click"
android:text="主人购买装备"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity_shop.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".ShopActivity">
<LinearLayout
android:id="@+id/ly_111"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<View
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@android:drawable/ic_menu_info_details"/>
<TextView
android:id="@+id/tv_name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:text="商品名称"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_life1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生命值"
android:textSize="18sp"/>
<TextView
android:id="@+id/tv_attack1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="攻击力"
android:textSize="18sp"/>
<TextView
android:id="@+id/tv_speed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="速度"
android:textSize="18sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/ly_112"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<View
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@android:drawable/ic_menu_info_details"/>
<TextView
android:id="@+id/tv_name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:text="商品名称"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_life2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生命值"
android:textSize="18sp"/>
<TextView
android:id="@+id/tv_attack2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="攻击力"
android:textSize="18sp"/>
<TextView
android:id="@+id/tv_speed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="速度"
android:textSize="18sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>