Android之装备选择案例

本文通过创建程序界面展示Android设备的选择,包括生命值、攻击力和敏捷度的进度条,用户可在装备界面购买增强属性的装备。点击按钮启动购买界面,通过Info类传递装备信息,并在主界面更新进度条值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建程序界面

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".Main"  
  6.     android:orientation="vertical"  
  7.     android:gravity="center">  
  8.   
  9.     <ImageView  
  10.         android:id="@+id/pet_imgv"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_gravity="center_horizontal"  
  14.         android:layout_marginBottom="5dp"  
  15.         android:layout_marginTop="30dp"  
  16.         android:src="@drawable/baby"/>  
  17.     <TextView  
  18.         android:id="@+id/pet_dialog_tv"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_gravity="center_horizontal"  
  22.         android:layout_marginBottom="25dp"  
  23.         android:gravity="center"  
  24.         android:text="主人,快给小宝宝购买装备吧" />  
  25.     <TableLayout  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_gravity="center"  
  29.         android:layout_marginBottom="20dp">  
  30.         <TableRow  
  31.             android:layout_width="fill_parent"  
  32.             android:layout_height="wrap_content">  
  33.             <TextView  
  34.                 android:layout_width="0dip"  
  35.                 android:layout_height="wrap_content"  
  36.                 android:layout_weight="1"  
  37.                 android:text="生命值:"  
  38.                 android:textColor="@android:color/black"  
  39.                 android:textSize="14sp"/>  
  40.             <ProgressBar  
  41.                 android:id="@+id/progressBar1"  
  42.                 style="?android:attr/progressBarStyleHorizontal"  
  43.                 android:layout_width="0dip"  
  44.                 android:layout_height="wrap_content"  
  45.                 android:layout_gravity="center"  
  46.                 android:layout_weight="2"/>  
  47.             <TextView  
  48.                 android:id="@+id/tv_life_progress"  
  49.                 android:layout_width="0dip"  
  50.                 android:layout_height="wrap_content"  
  51.                 android:layout_weight="1"  
  52.                 android:text="0"  
  53.                 android:gravity="center"  
  54.                 android:textColor="#000000"/>  
  55.         </TableRow>  
  56.         <TableRow  
  57.             android:layout_width="fill_parent"  
  58.             android:layout_height="wrap_content"  
  59.             android:layout_marginTop="5dp">  
  60.   
  61.             <TextView  
  62.                 android:layout_width="0dip"  
  63.                 android:layout_height="wrap_content"  
  64.                 android:layout_weight="1"  
  65.                 android:text="攻击力:"  
  66.                 android:textColor="@android:color/black"  
  67.                 android:textSize="14sp" />  
  68.   
  69.             <ProgressBar  
  70.                 android:id="@+id/progressBar2"  
  71.                 style="?android:attr/progressBarStyleHorizontal"  
  72.                 android:layout_width="0dip"  
  73.                 android:layout_height="wrap_content"  
  74.                 android:layout_weight="2" />  
  75.   
  76.             <TextView  
  77.                 android:id="@+id/tv_attack_progress"  
  78.                 android:layout_width="0dip"  
  79.                 android:layout_height="wrap_content"  
  80.                 android:layout_weight="1"  
  81.                 android:text="0"  
  82.                 android:gravity="center"  
  83.                 android:textColor="#000000" />  
  84.         </TableRow>  
  85.         <TableRow  
  86.             android:layout_width="fill_parent"  
  87.             android:layout_height="wrap_content"  
  88.             android:layout_marginTop="5dp">  
  89.   
  90.             <TextView  
  91.                 android:layout_width="0dip"  
  92.                 android:layout_height="wrap_content"  
  93.                 android:layout_weight="1"  
  94.                 android:text="敏  捷:"  
  95.                 android:textColor="@android:color/black"  
  96.                 android:textSize="14sp" />  
  97.   
  98.             <ProgressBar  
  99.                 android:id="@+id/progressBar3"  
  100.                 style="?android:attr/progressBarStyleHorizontal"  
  101.                 android:layout_width="0dip"  
  102.                 android:layout_height="wrap_content"  
  103.                 android:layout_weight="2" />  
  104.   
  105.             <TextView  
  106.                 android:id="@+id/tv_speed_progress"  
  107.                 android:layout_width="0dip"  
  108.                 android:layout_height="wrap_content"  
  109.                 android:layout_weight="1"  
  110.                 android:text="0"  
  111.                 android:gravity="center"  
  112.                 android:textColor="#000000" />  
  113.         </TableRow>  
  114.     </TableLayout>  
  115.     <RelativeLayout  
  116.         android:layout_width="match_parent"  
  117.         android:layout_height="wrap_content" >  
  118.       
  119.         <Button  
  120.             android:id="@+id/btn_baby"  
  121.             android:layout_width="wrap_content"  
  122.             android:layout_height="wrap_content"  
  123.             android:layout_alignParentRight="true"  
  124.             android:layout_alignParentTop="true"  
  125.             android:drawablePadding="3dp"  
  126.             android:drawableRight="@android:drawable/ic_menu_add"  
  127.             android:onClick="click2"  
  128.             android:text="小宝宝购买装备"  
  129.             android:textSize="14sp" />  
  130.     </RelativeLayout>  
  131. </LinearLayout>  
  132. 2.创建装备界面
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.   
    4.     android:id="@+id/rl"  
    5.     android:layout_width="match_parent"  
    6.     android:layout_height="wrap_content"  
    7.     android:orientation="vertical">  
    8.     <View  
    9.         android:layout_width="30dp"  
    10.         android:layout_height="30dp"  
    11.         android:background="@android:drawable/ic_menu_info_details"  
    12.         android:layout_centerVertical="true"  
    13.         android:layout_alignParentLeft="true"  
    14.         />  
    15.     <TextView  
    16.         android:id="@+id/tv_name"  
    17.         android:layout_width="wrap_content"  
    18.         android:layout_height="wrap_content"  
    19.         android:layout_centerVertical="true"  
    20.         android:layout_marginLeft="60dp"  
    21.         android:text="商品名称"/>  
    22.     <LinearLayout  
    23.         android:layout_width="wrap_content"  
    24.         android:layout_height="wrap_content"  
    25.         android:layout_centerInParent="true"  
    26.         android:orientation="vertical">  
    27.         <TextView  
    28.             android:id="@+id/tv_life"  
    29.             android:layout_width="wrap_content"  
    30.             android:layout_height="wrap_content"  
    31.             android:textSize="13sp"  
    32.             android:text="生命值" />  
    33.         <TextView  
    34.             android:id="@+id/tv_attack"  
    35.             android:layout_width="wrap_content"  
    36.             android:layout_height="wrap_content"  
    37.             android:textSize="13sp"  
    38.             android:text="攻击力"  
    39.             android:layout_marginTop="5dp"/>  
    40.         <TextView  
    41.             android:id="@+id/tv_speed"  
    42.             android:layout_width="wrap_content"  
    43.             android:layout_height="wrap_content"  
    44.             android:textSize="13sp"  
    45.             android:text="敏    捷"  
    46.             android:layout_marginTop="5dp"/>  
    47.     </LinearLayout>  
    48. </RelativeLayout>  
    49. 3.创建Info类
      1. public class ItemInfo implements Serializable{  
      2.     private String name;  
      3.     private int acctack;  
      4.     private int life;  
      5.     private int speed;  
      6.     public ItemInfo(String name, int acctack, int life, int speed) {  
      7.         this.name = name;  
      8.         this.acctack = acctack;  
      9.         this.life = life;  
      10.         this.speed = speed;  
      11.     }  
      12.     public String getName() {  
      13.         return name;  
      14.     }  
      15.     public void setName(String name) {  
      16.         this.name = name;  
      17.     }  
      18.     public int getAcctack() {  
      19.         return acctack;  
      20.     }  
      21.     public void setAcctack(int acctack) {  
      22.         this.acctack = acctack;  
      23.     }  
      24.     public int getLife() {  
      25.         return life;  
      26.     }  
      27.     public void setLife(int life) {  
      28.         this.life = life;  
      29.     }  
      30.     public int getSpeed() {  
      31.         return speed;  
      32.     }  
      33.     public void setSpeed(int speed) {  
      34.         this.speed = speed;  
      35.     }  
      36.     public String toString() {  
      37.         return " [name=" + name + ", acctack=" + acctack + ", life=" + life + ", speed=" + speed + "]";  
      38.     }  
      39. }  

    4.创建购买装备程序界面
    1. import android.app.Activity;  
    2. import android.content.Intent;  
    3. import android.os.Bundle;  
    4. import android.view.View;  
    5. import android.view.View.OnClickListener;  
    6. import android.widget.TextView;  
    7.   
    8.   
    9. public class ShopActivity extends Activity implements OnClickListener {  
    10.   
    11.     private ItemInfo itemInfo;  
    12.   
    13.     protected void onCreate(Bundle savedInstanceState) {  
    14.         super.onCreate(savedInstanceState);  
    15.         setContentView(R.layout.activity_shop);  
    16.         itemInfo = new ItemInfo("金剑"1002020);  
    17.         findViewById(R.id.rl).setOnClickListener(this);  
    18.         TextView mLifeTV = (TextView) findViewById(R.id.tv_life);  
    19.         TextView mNameTV = (TextView) findViewById(R.id.tv_name);  
    20.         TextView mSpeedTV = (TextView) findViewById(R.id.tv_speed);  
    21.         TextView mAttackTV = (TextView) findViewById(R.id.tv_attack);  
    22.         // TextView显示字符串,这里传入int值编译不会报错,运行会出错  
    23.         mLifeTV.setText("生命值+" + itemInfo.getLife());  
    24.         mNameTV.setText(itemInfo.getName() + "");  
    25.         mSpeedTV.setText("敏捷度+" + itemInfo.getSpeed());  
    26.         mAttackTV.setText("攻击力+" + itemInfo.getAcctack());  
    27.     }  
    28.   
    29.     @Override  
    30.     public void onClick(View v) {  
    31.         // TODO Auto-generated method stub  
    32.         switch (v.getId()) {  
    33.             case R.id.rl:  
    34.                 Intent intent = new Intent();  
    35.                 intent.putExtra("equipment", itemInfo);  
    36.                 setResult(1, intent);  
    37.                 finish();  
    38.                 break;  
    39.         }  
    40.     }  
    41. }  
    42. 5.界面交互程序
      1. import android.app.Activity;  
      2. import android.content.Intent;  
      3. import android.os.Bundle;  
      4. import android.view.Menu;  
      5. import android.view.MenuItem;  
      6. import android.view.View;  
      7. import android.widget.ProgressBar;  
      8. import android.widget.TextView;  
      9.   
      10.   
      11. public class Main extends Activity {  
      12.   
      13.     private ProgressBar mProgressBar1;  
      14.     private ProgressBar mProgressBar2;  
      15.     private ProgressBar mProgressBar3;  
      16.     private TextView mLifeTV;  
      17.     private TextView mAttackTV;  
      18.     private TextView mSpeedTV;  
      19.   
      20.     @Override  
      21.     protected void onCreate(Bundle savedInstanceState) {  
      22.         super.onCreate(savedInstanceState);  
      23.         setContentView(R.layout.activity_main);  
      24.         mLifeTV = (TextView) findViewById(R.id.tv_life_progress);  
      25.         mAttackTV = (TextView) findViewById(R.id.tv_attack_progress);  
      26.         mSpeedTV = (TextView) findViewById(R.id.tv_speed_progress);  
      27.         initProgress();                    //初始化进度条  
      28.     }  
      29.   
      30.     private void initProgress() {  
      31.         mProgressBar1 = (ProgressBar) findViewById(R.id.progressBar1);  
      32.         mProgressBar2 = (ProgressBar) findViewById(R.id.progressBar2);  
      33.         mProgressBar3 = (ProgressBar) findViewById(R.id.progressBar3);  
      34.         mProgressBar1.setMax(1000);        //设置最大值1000  
      35.         mProgressBar2.setMax(1000);  
      36.         mProgressBar3.setMax(1000);  
      37.     }  
      38.   
      39.     // 开启新的activity并且想获取他的返回值  
      40.     public void click(View view) {  
      41.         Intent intent = new Intent(this, ShopActivity.class);  
      42.         startActivityForResult(intent, 1); // 返回请求结果,请求码为1  
      43.     }  
      44.   
      45.     public void click2(View view) {  
      46.         Intent intent = new Intent(this, ShopActivity.class);  
      47.         startActivityForResult(intent, 1);  
      48.     }  
      49.   
      50.     @Override  
      51.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
      52.         super.onActivityResult(requestCode, resultCode, data);  
      53.         if (data != null) {  
      54.             // 判断结果码是否等于1,等于1为主人添加装备,等于2为宝宝添加装备  
      55.             if (resultCode == 1) {  
      56.                 if (requestCode == 1) {  
      57.                     ItemInfo info= (ItemInfo) data.getSerializableExtra("equipment");  
      58.                     //更新ProgressBar的值  
      59.                     updateProgress(info);  
      60.                 }  
      61.             }  
      62.         }  
      63.     }  
      64.     //更新ProgressBar的值  
      65.     private void updateProgress(ItemInfo info) {  
      66.         int progress1 = mProgressBar1.getProgress();  
      67.         int progress2 = mProgressBar2.getProgress();  
      68.         int progress3 = mProgressBar3.getProgress();  
      69.         mProgressBar1.setProgress(progress1+info.getLife());  
      70.         mProgressBar2.setProgress(progress2+info.getAcctack());  
      71.         mProgressBar3.setProgress(progress3+info.getSpeed());  
      72.         mLifeTV.setText(mProgressBar1.getProgress()+"");  
      73.         mAttackTV.setText(mProgressBar2.getProgress() + "");  
      74.         mSpeedTV.setText(mProgressBar3.getProgress() + "");  
      75.     }  
      76.   
      77.     @Override  
      78.     public boolean onCreateOptionsMenu(Menu menu) {  
      79.         // Inflate the menu; this adds items to the action bar if it is present.  
      80.         getMenuInflater().inflate(R.menu.menu_main, menu);  
      81.         return true;  
      82.     }  
      83.   
      84.     @Override  
      85.     public boolean onOptionsItemSelected(MenuItem item) {  
      86.         // Handle action bar item clicks here. The action bar will  
      87.         // automatically handle clicks on the Home/Up button, so long  
      88.         // as you specify a parent activity in AndroidManifest.xml.  
      89.         int id = item.getItemId();  
      90.   
      91.         //noinspection SimplifiableIfStatement  
      92.         if (id == R.id.action_settings) {  
      93.             return true;  
      94.         }  
      95.   
      96.         return super.onOptionsItemSelected(item);  
      97.     }  
      98. }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值