装备选择

来源:互联网 发布:java tag 编辑:程序博客网 时间:2024/04/28 06:37

编写一个装备选择的程序:


一.创建程序:

  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.         <Button  
  119.             android:id="@+id/btn_master"  
  120.             android:layout_width="wrap_content"  
  121.             android:layout_height="wrap_content"  
  122.             android:layout_alignParentLeft="true"  
  123.             android:layout_alignParentTop="true"  
  124.             android:drawableRight="@android:drawable/ic_menu_add"  
  125.             android:onClick="click"  
  126.             android:drawablePadding="3dp"  
  127.             android:text="主人购买装备"  
  128.             android:textSize="14sp" />  
  129.         <Button  
  130.             android:id="@+id/btn_baby"  
  131.             android:layout_width="wrap_content"  
  132.             android:layout_height="wrap_content"  
  133.             android:layout_alignParentRight="true"  
  134.             android:layout_alignParentTop="true"  
  135.             android:drawablePadding="3dp"  
  136.             android:drawableRight="@android:drawable/ic_menu_add"  
  137.             android:onClick="click2"  
  138.             android:text="小宝宝购买装备"  
  139.             android:textSize="14sp" />  
  140.     </RelativeLayout>  
  141. </LinearLayout>  
进度条是用控件ProgressBar来控制的,它用来显示小宝宝的生命力、攻击力、敏捷进度条。

二.创建装备界面:

  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>  

三.创建类:

  1. package select.itcast.cn.huichuan;  
  2.   
  3. /** 
  4.  * Created by wanglaoda on 15-8-2. 
  5.  */  
  6. import java.io.Serializable;  
  7.   
  8. //实现序列化方便传递数据  
  9. public class ItemInfo implements Serializable{  
  10.     private String name;  
  11.     private int acctack;  
  12.     private int life;  
  13.     private int speed;  
  14.     public ItemInfo(String name, int acctack, int life, int speed) {  
  15.         this.name = name;  
  16.         this.acctack = acctack;  
  17.         this.life = life;  
  18.         this.speed = speed;  
  19.     }  
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.     public void setName(String name) {  
  24.         this.name = name;  
  25.     }  
  26.     public int getAcctack() {  
  27.         return acctack;  
  28.     }  
  29.     public void setAcctack(int acctack) {  
  30.         this.acctack = acctack;  
  31.     }  
  32.     public int getLife() {  
  33.         return life;  
  34.     }  
  35.     public void setLife(int life) {  
  36.         this.life = life;  
  37.     }  
  38.     public int getSpeed() {  
  39.         return speed;  
  40.     }  
  41.     public void setSpeed(int speed) {  
  42.         this.speed = speed;  
  43.     }  
  44.     public String toString() {  
  45.         return " [name=" + name + ", acctack=" + acctack + ", life=" + life + ", speed=" + speed + "]";  
  46.     }  
  47. }  
需要注意的是,Intent除了传递基本类型之外,只能传递Serializable或Parcelable类型的数据,为了方便数据传递,在这里让ItemInfo类实现Serializable接口。
四.创建ShopActivity:
  1. package select.itcast.cn.huichuan;  
  2.   
  3. /** 
  4.  * Created by wanglaoda on 15-8-2. 
  5.  */  
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.TextView;  
  12.   
  13.   
  14. public class ShopActivity extends Activity implements OnClickListener {  
  15.   
  16.     private ItemInfo itemInfo;  
  17.   
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_shop);  
  21.         itemInfo = new ItemInfo("金剑"1002020);  
  22.         findViewById(R.id.rl).setOnClickListener(this);  
  23.         TextView mLifeTV = (TextView) findViewById(R.id.tv_life);  
  24.         TextView mNameTV = (TextView) findViewById(R.id.tv_name);  
  25.         TextView mSpeedTV = (TextView) findViewById(R.id.tv_speed);  
  26.         TextView mAttackTV = (TextView) findViewById(R.id.tv_attack);  
  27.         // TextView显示字符串,这里传入int值编译不会报错,运行会出错  
  28.         mLifeTV.setText("生命值+" + itemInfo.getLife());  
  29.         mNameTV.setText(itemInfo.getName() + "");  
  30.         mSpeedTV.setText("敏捷度+" + itemInfo.getSpeed());  
  31.         mAttackTV.setText("攻击力+" + itemInfo.getAcctack());  
  32.     }  
  33.   
  34.     @Override  
  35.     public void onClick(View v) {  
  36.         // TODO Auto-generated method stub  
  37.         switch (v.getId()) {  
  38.             case R.id.rl:  
  39.                 Intent intent = new Intent();  
  40.                 intent.putExtra("equipment", itemInfo);  
  41.                 setResult(1, intent);  
  42.                 finish();  
  43.                 break;  
  44.         }  
  45.     }  
  46. }  
上述代码中的重点代码是后几行,从这段代码中可以看出,使用setResult()方法跳转Activity不需要指定要跳转的Activity。setReult()方法的作用是让当前Activity返回到它的调用者,在这里可以理解为让ShopActivity返回到MainActivity。
五.编写界面交互:
    1. package select.itcast.cn.huichuan;  
    2.   
    3. import android.app.Activity;  
    4. import android.content.Intent;  
    5. import android.os.Bundle;  
    6. import android.view.Menu;  
    7. import android.view.MenuItem;  
    8. import android.view.View;  
    9. import android.widget.ProgressBar;  
    10. import android.widget.TextView;  
    11.   
    12.   
    13. public class Main extends Activity {  
    14.   
    15.     private ProgressBar mProgressBar1;  
    16.     private ProgressBar mProgressBar2;  
    17.     private ProgressBar mProgressBar3;  
    18.     private TextView mLifeTV;  
    19.     private TextView mAttackTV;  
    20.     private TextView mSpeedTV;  
    21.   
    22.     @Override  
    23.     protected void onCreate(Bundle savedInstanceState) {  
    24.         super.onCreate(savedInstanceState);  
    25.         setContentView(R.layout.activity_main);  
    26.         mLifeTV = (TextView) findViewById(R.id.tv_life_progress);  
    27.         mAttackTV = (TextView) findViewById(R.id.tv_attack_progress);  
    28.         mSpeedTV = (TextView) findViewById(R.id.tv_speed_progress);  
    29.         initProgress();                    //初始化进度条  
    30.     }  
    31.   
    32.     private void initProgress() {  
    33.         mProgressBar1 = (ProgressBar) findViewById(R.id.progressBar1);  
    34.         mProgressBar2 = (ProgressBar) findViewById(R.id.progressBar2);  
    35.         mProgressBar3 = (ProgressBar) findViewById(R.id.progressBar3);  
    36.         mProgressBar1.setMax(1000);        //设置最大值1000  
    37.         mProgressBar2.setMax(1000);  
    38.         mProgressBar3.setMax(1000);  
    39.     }  
    40.   
    41.     // 开启新的activity并且想获取他的返回值  
    42.     public void click(View view) {  
    43.         Intent intent = new Intent(this, ShopActivity.class);  
    44.         startActivityForResult(intent, 1); // 返回请求结果,请求码为1  
    45.     }  
    46.   
    47.     public void click2(View view) {  
    48.         Intent intent = new Intent(this, ShopActivity.class);  
    49.         startActivityForResult(intent, 1);  
    50.     }  
    51.   
    52.     @Override  
    53.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    54.         super.onActivityResult(requestCode, resultCode, data);  
    55.         if (data != null) {  
    56.             // 判断结果码是否等于1,等于1为主人添加装备,等于2为宝宝添加装备  
    57.             if (resultCode == 1) {  
    58.                 if (requestCode == 1) {  
    59.                     ItemInfo info= (ItemInfo) data.getSerializableExtra("equipment");  
    60.                     //更新ProgressBar的值  
    61.                     updateProgress(info);  
    62.                 }  
    63.             }  
    64.         }  
    65.     }  
    66.     //更新ProgressBar的值  
    67.     private void updateProgress(ItemInfo info) {  
    68.         int progress1 = mProgressBar1.getProgress();  
    69.         int progress2 = mProgressBar2.getProgress();  
    70.         int progress3 = mProgressBar3.getProgress();  
    71.         mProgressBar1.setProgress(progress1+info.getLife());  
    72.         mProgressBar2.setProgress(progress2+info.getAcctack());  
    73.         mProgressBar3.setProgress(progress3+info.getSpeed());  
    74.         mLifeTV.setText(mProgressBar1.getProgress()+"");  
    75.         mAttackTV.setText(mProgressBar2.getProgress() + "");  
    76.         mSpeedTV.setText(mProgressBar3.getProgress() + "");  
    77.     }  
    78.   
    79.     @Override  
    80.     public boolean onCreateOptionsMenu(Menu menu) {  
    81.         // Inflate the menu; this adds items to the action bar if it is present.  
    82.         getMenuInflater().inflate(R.menu.menu_main, menu);  
    83.         return true;  
    84.     }  
    85.   
    86.     @Override  
    87.     public boolean onOptionsItemSelected(MenuItem item) {  
    88.         // Handle action bar item clicks here. The action bar will  
    89.         // automatically handle clicks on the Home/Up button, so long  
    90.         // as you specify a parent activity in AndroidManifest.xml.  
    91.         int id = item.getItemId();  
    92.   
    93.         //noinspection SimplifiableIfStatement  
    94.         if (id == R.id.action_settings) {  
    95.             return true;  
    96.         }  
    97.   
    98.         return super.onOptionsItemSelected(item);  
    99.     }  
    100. }  
    六.清单文件的配置:
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     package="select.itcast.cn.huichuan" >  
    4.   
    5.     <application  
    6.         android:allowBackup="true"  
    7.         android:icon="@mipmap/ic_launcher"  
    8.         android:label="@string/app_name"  
    9.         android:theme="@style/AppTheme" >  
    10.         <activity  
    11.             android:name=".Main"  
    12.             android:label="@string/app_name" >  
    13.             <intent-filter>  
    14.                 <action android:name="android.intent.action.MAIN" />  
    15.   
    16.                 <category android:name="android.intent.category.LAUNCHER" />  
    17.             </intent-filter>  
    18.         </activity>  
    19.         <activity android:name=".ShopActivity"/>  
    20.     </application>  
    21.   
    22. </manifest>  



0 0