案例文件列表如下:
2.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:orientation =”vertical“
android :gravity =“center”
tools:context =“cn.edu.bzu.myapplication2.MainActivity”>
<ImageView
android:layout_width =“wrap_content”
android:layout_height =“wrap_content”
app:srcCompat =“@ drawable / baby”
android: layout_marginTop = “30DP”
机器人:ID = “@ + ID / ImageView的”
机器人:layout_gravity = “CENTER_HORIZONTAL”
机器人:layout_marginBottom = “5DP”/>
<TextView的
机器人:layout_width = “WRAP_CONTENT”
机器人:layout_height = “WRAP_CONTENT”
机器人:文字=“主人,快给小宝宝购买装备吧”
android:layout_marginBottom =“25dp”
android:layout_gravity =“center_horizontal”
android:gravity =“center”
android:id =“@ + id / pet_dialog_tv”/>
<TableLayout
android :layout_width =“match_parent”
android:layout_height =“wra p_content“
android:gravity =”center“
android:layout_marginBottom =”20dp“>
<TableRow
android:layout_width =”match_parent“
android:layout_height =”wrap_content“
android:layout_centerVertical =”true“
android:layout_alignParentStart =”true“>
<TextView
android:layout_width =“0dp”
android:layout_height =“wrap_content”
android:layout_weight =“1”
android:text =“生命值:”
android:textColor =“@ android:color / black”
android:textSize =“14sp” >
<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:id =”@ + id / tv_life_progress“
android:layout_width =”0dp“
android:layout_height =”wrap_content“
android:layout_weight =”1“
android:text =”0“
android:gravity =中心“
android:textColor =”#000000“
/>
</ TableRow>
<TableRow
android:layout_width =”match_parent“
android:layout_height =”wrap_content“>
<TextView
android:layout_width =”0dp“
android:layout _height =“wrap_content”
android:layout_weight =“1”
android:text =“攻击力:”
android:textColor =“@ android:color / black”
android:textSize =“14sp”/>
<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:id =“@ + id / tv_attack_progress”
android:layout_width =“0dp”
android:layout_height =“wrap_content”
android:layout_weight =“1”
android:text =“0”
android:gravity =“center”
android:textColor =“#000000 “
/>
</的TableRow>
<的TableRow
机器人:layout_width =” match_parent”
机器人:layout_height = “WRAP_CONTENT”>
<TextView的
机器人:layout_width = “0dp”
机器人:layout_height = “WRAP_CONTENT”
机器人:layout_weight = “1”
机器人:文本=“
敏捷:” android:textColor =“@ android:color / black”
android:textSize =“14sp”/>
<ProgressBar
android:id =“@ + id / progressBar3”
style =“?android:attr / progressBarStyleHorizontal”
android:layout_width =“0dp”
android:layout_heig HT = “WRAP_CONTENT”
机器人:layout_gravity = “中心”
机器人:layout_weight = “2”
/>
<TextView的
机器人:ID = “@ + ID / tv_speed_progress”
机器人:layout_width = “0dp”
机器人:layout_height = “WRAP_CONTENT”
机器人: layout_weight =“1”
android:text =“0”
android:gravity =“center”
android:textColor =“#000000”
/>
</ TableRow>
</ TableLayout>
<Button
android:layout_width =“wrap_content”
android:layout_height = “wrap_content”
android:text =“
购买装备” android:drawableLeft =“@ android:drawable / ic_menu_add”
android:textSize =“14sp”
android:id =“@ + id / host”
android:onClick =“click1” />
</ LinearLayout>
3.activity_shop.xml代码如下:
<?xml version =“1.0”encoding =“utf-8”?>
<LinearLayout xmlns:android =“http://schemas.android.com/apk/res/android”
android:layout_width =“match_parent”
android:layout_height = “match_parent”
机器人:取向= “垂直”>
<TextView的
机器人:layout_width = “match_parent”
机器人:layout_height = “WRAP_CONTENT”
机器人:文本= “请选择您要选择的装备”
机器人:TEXTSIZE = “30sp”
机器人: textColor =“#33CCFF”/>
<RelativeLayout
android:id =“@ + id /
baojian ” android:layout_width =“match_parent”
android:layout_height =“80dp”
android:orientation =“vertical”>
<View
android:layout_width = 40dp“
android:layout_height =”40dp“
android:layout_centerVertical =”true“
android:layout_alignParentLeft =”true“
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:text =“商品名称”
android:textSize =“20sp”
android:layout_centerVertical =“true”
android:layout_marginLeft =“60dp”/>
<LinearLayout
android:t_width =“50dp”
android:layout_height =“50dp”
android:layout_alignParentRight =“true”
android:layout_marginTop =“10dp”
android:layout_marginRight =“70dp”
android:orientation =“vertical”>
<TextView
android:id =“@ ID / tv_life1"
机器人:layout_width = “WRAP_CONTENT”
机器人:layout_height = “WRAP_CONTENT”
机器人:文本= “生命值”
机器人:TEXTSIZE = “15sp”/>
<TextView的
机器人:ID = “@ + ID / tv_attack1”
机器人: layout_width =“wrap_content”
android:layout_height =“wrap_content”
android:text =“攻击力”
android:textSize =“15sp”/>
<TextView
android:id =“@ + id / tv_speed1”
android:layout_width =“wrap_content”
android :layout_height =“wrap_content”
android:text =“敏捷度”
android:textSize =“15sp”/>
</ LinearLayout>
</ RelativeLayout>
<RelativeLayout
android:id =“@ + id / wuyingdao”
android:layout_width =“match_parent “
android:layout_height =”80dp“
android:orientation =”vertical“>
<查看
android:layout_width =”40dp“
android:layout_height =”40dp“
android:layout_centerVertical =”true“
android:layout_alignParentLeft =”true“
android: “ @android:drawable / ic_menu_info_details“/>
<TextView
android:id =”@ + id / tv_name2“
android:layout_width =”wrap_content“
android:layout_height =”wrap_content“
android:text =”商品名称“
android:textSize =”20sp “
android:layout_centerVertical =”true“
android:layout_marginLeft =”60dp“/>
<LinearLayout
android:layout_width =”50dp“
android:layout_height =”50dp“
android:layout_alignParentRight =”true“
android:layout_marginRight =”70dp“
android:layout_marginTop =“10dp”
android:orientation =“vertical”>
<TextView
android:id =“@ + id / tv_life2”
android:layout_width =“wrap_content”
android:layout_height =“wrap_content”
android:text =“生命值”
android:textSize =“15sp”/>
<TextView
android:id =“@ + id / tv_attack2”
android:layout_width =“wrap_content”
android:layout_height =“wrap_content”
android:text =“攻击力”
android:textSize =“15sp”/>
<TextView的
机器人:ID = “@ + ID / tv_speed2”
机器人:layout_width = “WRAP_CONTENT”
机器人:layout_height = “WRAP_CONTENT”
机器人:文本= “敏捷度”
机器人:TEXTSIZE = “15sp”/>
</的LinearLayout>
</相对的 UT>
<RelativeLayout的
机器人:ID = “@ + ID /旋风”
机器人:layout_width = “match_parent”
机器人:layout_height = “80dp”
机器人:取向= “垂直”>
<查看
机器人:layout_width = “40dp”
机器人:layout_height = “40dp”
android:layout_centerVertical =“true”
android:layout_alignParentLeft =“true”
android:background =“@ android:drawable / ic_menu_info_details”/>
<TextView
android:id =“@ + id / tv_name3”
android:layout_width =“wrap_content “
机器人:layout_height =” WRAP_CONTENT”
机器人:文本= “商品名称”
机器人:TEXTSIZE = “20SP”
机器人:layout_centerVertical = “真”
机器人:layout_marginLeft = “60dp”/>
<LinearLayout中
机器人:layout_width = “50dp”
机器人: layout_height =“50dp”
android:layout_alignParentRight =“true”
android:layout_marginTop =“10dp”
android:layout_marginRight =“70dp”
android:orientation =“vertical”>
<TextView
android:id =“@ + id / tv_life3”
android:layout_width =“wrap_content”
android:layout_height =“wrap_content”
android:text =“生命值”
android:textSize =“15sp”/>
<TextView
android:id =“@ + id / tv_attack3”
android:layout _width =“wrap_content”
android:layout_height =“wrap_content”
android:text =“攻击力”
android:textSize =“15sp”/>
<TextView
android:id =“@ + id / tv_speed3”
android:layout_width =“wrap_content”
android :layout_height =“wrap_content”
android:text =“敏捷度”
android:textSize =“15sp”/>
</ LinearLayout>
</ RelativeLayout>
</ LinearLayout>
4.MainActivity.java代码如下:
包cn.edu.bzu.myapplication2;
import android.content.Intent;
导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.view.View;
导入android.widget.ProgressBar;
导入android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ProgressBar pb1;
ProgressBar pb2;
ProgressBar pb3;
TextView tv_life;
TextView tv_attack;
TextView tv_speed;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb1 =(ProgressBar)findViewById(R.id.progressBar1);
pb2 =(ProgressBar)findViewById(R.id.progressBar2);
pb3 =(ProgressBar)findViewById(R.id.progressBar3);
tv_life =(TextView)findViewById(R.id.tv_life_progress);
tv_attack =(TextView)findViewById(R.id.tv_attack_progress);
tv_speed =(TextView)findViewById(R.id.tv_speed_progress);
pb1.setMax(1000); //初始化进度条的最大值
pb2.setMax(1000);
pb3.setMax(1000);
}
public void click1(View view){
Intent intent = new Intent(this,ShoppingActivity.class);
startActivityForResult(intent,1);
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(resultCode == 1){
ItemInfo itemInfo1 =(ItemInfo)data.getExtras()。get(“itemInfo1”);
updateProgressBar1(itemInfo1);
}
if(resultCode == 2){
ItemInfo itemInfo2 =(ItemInfo)data.getExtras()。get(“itemInfo2”);
updateProgressBar1(itemInfo2);
}
if(resultCode == 3){
ItemInfo itemInfo3 =(ItemInfo)data.getExtras()。get(“itemInfo3”);
updateProgressBar1(itemInfo3);
}
super.onActivityResult(requestCode,resultCode,data);
}
private void updateProgressBar1(ItemInfo itemInfo1){
int progress1 = pb1.getProgress();
int progress2 = pb2.getProgress();
int progress3 = pb3.getProgress();
pb1.setProgress(progress1 + itemInfo1.getLife());
pb2.setProgress(progress2 + itemInfo1.getAcctack());
pb3.setProgress(progress3 + itemInfo1.getSpeed());
tv_life.setText(pb1.getProgress()+“”);
tv_attack.setText(pb2.getProgress()+“”);
tv_speed.setText(pb3.getProgress()+“”);
}
}
5.ShoppingActivity.java代码如下:
包cn.edu.bzu.myapplication2;
导入android.app.Activity;
import android.content.Intent;
导入android.os.Bundle;
导入android.view.View;
导入android.widget.TextView;
public class ShoppingActivity extends Activity implements View.OnClickListener {
private ItemInfo itemInfo1;
private ItemInfo itemInfo2;
private ItemInfo itemInfo3;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
findViewById(R.id.baojian).setOnClickListener(this);
findViewById(R.id.wuyingdao).setOnClickListener(this);
findViewById(R.id.xuanfeng).setOnClickListener(this);
itemInfo1 = new ItemInfo(“宝剑”,10,20,20);
TextView tv_name1 =(TextView)findViewById(R.id.tv_name1);
TextView tv_life1 =(TextView)findViewById(R.id.tv_life1);
TextView tv_attack1 =(TextView)findViewById(R.id.tv_attack1);
TextView tv_speed1 =(TextView)findViewById(R.id.tv_speed1);
tv_name1.setText(itemInfo1.getName());
tv_life1.setText(itemInfo1.getLife());
tv_attack1.setText(itemInfo1.getAcctack());
tv_speed1.setText(itemInfo1.getSpeed());
itemInfo2 = new ItemInfo(“无影刀”,8,10,10);
TextView tv_name2 =(TextView)findViewById(R.id.tv_name2);
TextView tv_life2 =(TextView)findViewById(R.id.tv_life2);
TextView tv_attack2 =(TextView)findViewById(R.id.tv_attack2);
TextView tv_speed2 =(TextView)findViewById(R.id.tv_speed2);
tv_name2.setText(itemInfo2.getName());
tv_life2.setText(itemInfo2.getLife());
tv_attack2.setText(itemInfo2.getAcctack());
tv_speed2.setText(itemInfo2.getSpeed());
itemInfo3 = new ItemInfo(“旋风”,20,5,5);
TextView tv_name3 =(TextView)findViewById(R.id.tv_name3);
TextView tv_life3 =(TextView)findViewById(R.id.tv_life3);
TextView tv_attack3 =(TextView)findViewById(R.id.tv_attack3);
TextView tv_speed3 =(TextView)findViewById(R.id.tv_speed3);
tv_name3.setText(itemInfo3.getName());
tv_life3.setText(itemInfo3.getLife());
tv_attack3.setText(itemInfo3.getAcctack());
tv_speed3.setText(itemInfo3.getSpeed());
}
@Override
public void onBackPressed(){
super.onBackPressed();
}
public void onClick(View view){
switch(view.getId()){
case R.id.baojian:
Intent intent = new Intent();
intent.putExtra(“itemInfo1”,itemInfo1);
setResult(1,intent);
完();
打破;
案件意图:
意图intent1 =新意图();
intent1.putExtra(“itemInfo2”,itemInfo2);
setResult(2,intent1);
完();
打破;
case R.id.xuanfeng:
Intent intent2 = new Intent();
intent2.putExtra(“itemInfo3”,itemInfo3);
setResult(3,intent2);
完();
打破;
默认:
break;
}
}
}
6.ItemInfo.java代码如下:
包cn.edu.bzu.myapplication2;
import java.io.Serializable;
/ **
*由管理员于2017/3/23创建。
* /
public class ItemInfo实现Serializable {
private String name;
private int acctack;
私人生活
私有速度;
public ItemInfo(String name,int acctack,int life,int speed){
this.name = name;
this.acctack = acctack;
this.life = life;
这个速度=速度
}
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 +“,00life =”+ life +“,speed =”+ speed +“]
}
}
7.AndroidManifest.xml代码如下:
<?xml version =“1.0”encoding =“utf-8”?>
<manifest xmlns:android =“http://schemas.android.com/apk/res/android”
package =“cn.edu.bzu.myapplication2 “>
<应用
机器人:allowBackup =”真”
机器人:图标= “@ mip映射/ ic_launcher”
机器人:标签= “DeviceChoose”
机器人:supportsRtl = “真”
机器人:主题= “@风格/ AppTheme”>
<活动机器人: name =“。MainActivity”>
<intent-filter>
<action android:name =“android.intent.action.MAIN”/>
<category android:name =“android.intent.category.LAUNCHER”/>
</ intent-过滤器>
</ activity>
<activity android:name =“。ShoppingActivity”> </ activity>
</ application>
</ manifest>
8.运行效果如下:
<img src="<font><font class=" "="">