Android:实现装备购买

这篇博客展示了如何在Android应用中实现装备购买的功能。通过建立多个Activity,包括一个主界面和商店界面,博主详细介绍了布局设计和代码实现。在主界面中,包括角色信息和进度条展示,用户可以通过点击按钮进入商店。商店界面则显示装备信息,用户选择装备后,可以通过点击事件返回主界面并更新角色属性。

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

大概步骤建立几个Activity并将图片分别关联到其中,并进行布局操作,一个是主要的界面,另一个是进入Shop后的界面,操作的具体代码和结果如下:

代码:

activity_char.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.     <TextView  
  6.         android:id="@+id/name"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_marginLeft="10dp"  
  10.         />  
  11. <ImageView  
  12.     android:id="@+id/iv"  
  13.     android:layout_width="120dp"  
  14.     android:layout_height="150dp"  
  15.     android:layout_gravity="center"  
  16.     android:layout_marginTop="50dp"  
  17.     android:background="@drawable/soldier76"/>  
  18.     <TextView  
  19.         android:id="@+id/CN"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_gravity="center"  
  23.         android:layout_marginTop="5dp"  
  24.         android:textSize="23sp"  
  25.         android:text="Soldier76"  
  26.         />  
  27.     <TextView  
  28.         android:id="@+id/status"  
  29.         android:layout_width="wrap_content"  
  30.         android:layout_height="wrap_content"  
  31.         android:layout_gravity="center"  
  32.         android:layout_marginTop="5dp"  
  33.         android:layout_marginBottom="3dp"  
  34.         android:text="Wonderful"  
  35.         android:textColor="#7CFC00"/>  
  36.     <TableLayout  
  37.         android:layout_width="match_parent"  
  38.         android:layout_height="wrap_content"  
  39.         android:layout_gravity="center">  
  40.         <TableRow  
  41.             android:layout_width="match_parent"  
  42.             android:layout_height="wrap_content">  
  43.             <TextView  
  44.                 android:layout_width="wrap_content"  
  45.                 android:layout_height="wrap_content"  
  46.                 android:text="Health"  
  47.                 android:textSize="20sp"  
  48.                 android:layout_weight="1"  
  49.                 />  
  50.             <ProgressBar  
  51.                 android:id="@+id/pro_health"  
  52.                 style="@style/Widget.AppCompat.ProgressBar.Horizontal"  
  53.                 android:layout_weight="2"  
  54.                 android:indeterminateTint="#7CFC00"  
  55.                 android:indeterminateTintMode="src_atop"/>  
  56.   
  57.             <TextView  
  58.                 android:id="@+id/status_hea"  
  59.                 android:layout_width="wrap_content"  
  60.                 android:layout_height="wrap_content"  
  61.                 android:layout_weight="3"  
  62.                 />  
  63.   
  64.   
  65.         </TableRow>  
  66.         <TableRow  
  67.             android:layout_width="match_parent"  
  68.             android:layout_height="wrap_content">  
  69.             <TextView  
  70.                 android:layout_width="wrap_content"  
  71.                 android:layout_height="wrap_content"  
  72.                 android:text="Power"  
  73.                 android:textSize="20sp"  
  74.                 android:layout_weight="1"  
  75.                 />  
  76.             <ProgressBar  
  77.                 android:id="@+id/pro_power"  
  78.                 style="@style/Widget.AppCompat.ProgressBar.Horizontal"  
  79.                 android:layout_weight="2"  
  80.                 android:indeterminateTint="#6495ED"  
  81.                 android:indeterminateTintMode="src_atop"/>  
  82.             <TextView  
  83.                 android:id="@+id/status_pow"  
  84.                 android:layout_width="wrap_content"  
  85.                 android:layout_height="wrap_content"  
  86.                 android:layout_weight="3"  
  87.                 />  
  88.   
  89.   
  90.         </TableRow>  
  91.         <TableRow  
  92.             android:layout_width="match_parent"  
  93.             android:layout_height="wrap_content">  
  94.             <TextView  
  95.                 android:layout_width="wrap_content"  
  96.                 android:layout_height="wrap_content"  
  97.                 android:text="Sensitivity"  
  98.                 android:textSize="20sp"  
  99.                 android:layout_weight="1"  
  100.                 />  
  101.             <ProgressBar  
  102.                 android:id="@+id/pro_sen"  
  103.                 style="@style/Widget.AppCompat.ProgressBar.Horizontal"  
  104.                 android:layout_weight="2"  
  105.                 android:indeterminateTint="#EEEE00"  
  106.                 android:indeterminateTintMode="src_atop"/>  
  107.             <TextView  
  108.                 android:id="@+id/status_sen"  
  109.                 android:layout_width="wrap_content"  
  110.                 android:layout_height="wrap_content"  
  111.                 android:layout_weight="3"  
  112.                 />  
  113.   
  114.   
  115.         </TableRow>  
  116.   
  117.     </TableLayout>  
  118. <Button  
  119.     android:id="@+id/button"  
  120.     android:layout_width="wrap_content"  
  121.     android:layout_height="wrap_content"  
  122.     android:layout_gravity="center"  
  123.     android:layout_marginTop="20dp"  
  124.     android:text="Go to Shop"/>  
  125. </LinearLayout>  
activity_shop.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     android:id="@+id/rr"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:layout_width="match_parent" android:layout_height="match_parent">  
  6.     <TextView  
  7.         android:id="@+id/item"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Item"  
  11.         android:layout_marginLeft="60dp"  
  12.         android:layout_marginTop="20dp"  
  13.         android:textSize="20sp"  
  14.         android:layout_centerVertical="true"  
  15.         />  
  16.     <LinearLayout  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_toRightOf="@+id/item"  
  20.         android:layout_marginLeft="10dp"  
  21.         android:layout_centerInParent="true"  
  22.         android:orientation="vertical">  
  23.         <TextView  
  24.             android:id="@+id/health"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="Health"  
  28.             android:textSize="23sp"/>  
  29.         <TextView  
  30.             android:id="@+id/power"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="Power"  
  34.             android:textSize="23sp"/>  
  35.         <TextView  
  36.             android:id="@+id/sen"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:text="Sen"  
  40.             android:textSize="23sp"/>  
  41.   
  42.     </LinearLayout>  
  43.   
  44.   
  45. </RelativeLayout>  
MainActivity.java:

  1. package com.eee.eeeee;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.inputmethodservice.Keyboard;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.KeyEvent;  
  9. import android.view.MotionEvent;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.RadioButton;  
  14. import android.widget.Toast;  
  15. import java.util.Timer;  
  16. import java.util.TimerTask;  
  17. import static android.R.attr.data;  
  18. import static android.R.attr.logo;  
  19. import static com.eee.eeeee.R.styleable.View;  
  20.   
  21. public class MainActivity extends AppCompatActivity{  
  22.     public EditText et1,et2;  
  23.     public RadioButton rb1,rb2;  
  24.     public static final int RC=1;  
  25.     @Override  
  26.    public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_main);  
  29.         Button b1 =(Button)findViewById(R.id.BB1);  
  30.         Button b2 =(Button)findViewById(R.id.BB2);  
  31.         et1 =(EditText)findViewById(R.id.et_UN);  
  32.         et2 =(EditText)findViewById(R.id.et_PW);  
  33.         rb1 =(RadioButton)findViewById(R.id.man);  
  34.         rb2 =(RadioButton)findViewById(R.id.woman);  
  35.         b1.setOnClickListener(new View.OnClickListener() {  
  36.             @Override  
  37.             public void onClick(View view) {  
  38.                 Intent intent =new Intent(MainActivity.this,SecACT.class);  
  39.                 intent.putExtra("un",et1.getText().toString().trim());  
  40.                 startActivity(intent);  
  41.   
  42.   
  43.   
  44.             }  
  45.         });  
  46.         b1.setOnClickListener(new View.OnClickListener() {  
  47.             @Override  
  48.             public void onClick(View view) {  
  49.                // Intent intent =new Intent(MainActivity.this,SecACT.class);  
  50.                 passData();  
  51.                 Toast.makeText(MainActivity.this,"Regist Successful!",Toast.LENGTH_SHORT).show();  
  52.                 //startActivity(intent);  
  53.   
  54.             }  
  55.         });  
  56. //        b2.setOnClickListener(new View.OnClickListener() {  
  57. //            @Override  
  58. //            public void onClick(View view) {  
  59. //                startActivity(new Intent(MainActivity.this,SecACT.class));  
  60. //                finish();  
  61. //            }  
  62. //        });  
  63.    }  
  64. //      @Override  
  65. //      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  66. //          if(requestCode==RC){  
  67. //              switch (resultCode){  
  68. //                  case RESULT_OK:  
  69. //                      String dataString=data.getStringExtra("ED");  
  70. //                      Log.i("----------->", dataString);  
  71. //              }  
  72. //          }  
  73. //      }  
  74.   
  75.   
  76.   
  77.       public void passData() {  
  78.                 /**Intent**/  
  79.                 Intent intent = new Intent(MainActivity.this, SecACT.class);  
  80.                 intent.putExtra("un", et1.getText().toString().trim());  
  81.                 intent.putExtra("pd", et2.getText().toString().trim());  
  82.                 /**Bundle**/  
  83. //            Bundle bundle =new Bundle();  
  84. //            bundle.putString("un",et1.getText().toString().trim());  
  85. //            bundle.putString("pd",et2.getText().toString().trim());  
  86.                 String sex = "";  
  87.                 if (rb1.isChecked()) {  
  88.                     sex = "Man";  
  89.                 } else if (rb2.isChecked()) {  
  90.                     sex = "Woman";  
  91.                 }  
  92.                 /**Intent**/  
  93.                 intent.putExtra("sex", sex);  
  94.                 startActivity(intent);  
  95.             }  
  96. //        /**Bundle**/  
  97.         bundle.putString("sex",sex);  
  98.         intent.putExtras(bundle);  
  99. // }  
  100.     /**Double Click BackButton to Exit**/  
  101.     private long firstTime=0;  
  102.     @Override  
  103.     public boolean onKeyUp(int keyCode, KeyEvent event) {  
  104.         switch (keyCode){  
  105.             case KeyEvent.KEYCODE_BACK:  
  106.                 long secondTime=System.currentTimeMillis();  
  107.                 if(secondTime-firstTime>2000){  
  108.                     Toast.makeText(MainActivity.this,"Press Again to Exit!",Toast.LENGTH_SHORT).show();  
  109.                     firstTime=secondTime;  
  110.                     return true;  
  111.                 }else{  
  112.                     finish();  
  113.                 }  
  114.                 break;  
  115.         }  
  116.         return super.onKeyUp(keyCode, event);  
  117.     }  
  118. //one way to  
  119.   
  120.   
  121.   
  122. }  
ShopAct.java:

  1. package com.eee.eeeee;  
  2.   
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.view.View;  
  7. import android.widget.TextView;  
  8.   
  9. import cn.itcast.domain.ItemInfo;  
  10.   
  11. /** 
  12.  * Created by li124 on 2017/2/25. 
  13.  */  
  14.   
  15. public class ShopAct extends AppCompatActivity implements View.OnClickListener{  
  16.     private ItemInfo itemInfo;  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_shop);  
  20.         itemInfo =new ItemInfo("Tactical eyepiece",80,100,60);  
  21.         findViewById(R.id.rr).setOnClickListener(this);  
  22.         TextView h=(TextView)findViewById(R.id.health);  
  23.         TextView p=(TextView)findViewById(R.id.power);  
  24.         TextView s=(TextView)findViewById(R.id.sen);  
  25.         TextView n=(TextView)findViewById(R.id.item);  
  26.         n.setText(itemInfo.getName()+"");  
  27.         h.setText("Health++"+itemInfo.getHealth());  
  28.         p.setText("Power++"+itemInfo.getPower());  
  29.         s.setText("Sen++"+itemInfo.getSen());  
  30.   
  31.     }  
  32.     @Override  
  33.     public void onClick(View v){  
  34.         switch (v.getId()){  
  35.             case R.id.rr:  
  36.                 Intent intent =new Intent();  
  37.                 intent.putExtra("equip",itemInfo);  
  38.                 setResult(1,intent);  
  39.                 finish();  
  40.                 break;  
  41.         }  
  42.     }  
  43. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值