装备选择

1、创建程序

装备选择程序对应的布局文件(activity_main.xml)布局代码使用到了控件ProgressBar(进度条),用来 显示小宝宝的生命值、攻击力和敏捷度的。

<?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"android:orientation="vertical">
<ImageViewandroid:layout_width="245dp"android:layout_height="200dp"app:srcCompat="@drawable/baby"android:id="@+id/pet_imgv"android:layout_gravity="center_horizontal"android:layout_marginBottom="5dp"android:layout_marginTop="30dp"

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="主人,快给小宝宝购买装备吧!" android:textSize="20dp" android:layout_gravity="center_horizontal"/> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginTop="25dp"> <TableRow android:layout_marginTop="10dp" 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:layout_marginRight="20dp"
android:text="生命值:" android:gravity="right"/> <ProgressBar android:id="@+id/pbsm1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_weight="3" />
<TextView android:id="@+id/tv_life" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="20dp" android:text="0" /> </TableRow> <TableRow android:layout_marginTop="10dp" 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:layout_marginRight="20dp" android:text="攻击力:" android:gravity="right"/> <ProgressBar android:id="@+id/pbsm2" style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_weight="3" /> <TextView android:id="@+id/tv_attack" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="20dp"
android:text="0" /> </TableRow> <TableRow android:layout_marginTop="10dp" 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:layout_marginRight="20dp"
android:text="敏捷:" android:gravity="right"/> <ProgressBar android:id="@+id/pbsm3" style="?android:attr/progressBarStyleHorizontal" android:layout_width="0dp" android:layout_marginTop="5dp" android:layout_height="wrap_content" android:layout_weight="3" /> <TextView
android:id="@+id/tv_speed" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="20dp" android:text="0" /> </TableRow> </TableLayout> <LinearLayout android:layout_marginTop="20dp" android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/b_btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableRight="@android:drawable/ic_menu_add"
android:layout_marginRight="40dp" android:drawablePadding="3dp" android:onClick="click1" android:text="主人购买装备"/> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableRight="@android:drawable/ic_menu_add"
android:drawablePadding="3dp" android:text="小宝宝购买装备"/> </LinearLayout></LinearLayout>
2、创建装备界面activity_shop.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/rl"android:layout_width="match_parent"android:layout_height="match_parent"><Viewandroid:id="@+id/view"android:layout_width="30dp"android:layout_height="30dp"android:background="@android:drawable/ic_menu_info_details"android:layout_marginTop="50dp"/><TextViewandroid:id="@+id/tv_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="55dp"android:layout_marginLeft="40dp"android:layout_toRightOf="@id/view"android:text="商品名称" /><LinearLayoutandroid:layout_toRightOf="@id/tv_name"android:layout_marginTop="40dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="40dp"android:orientation="vertical"><TextViewandroid:id="@+id/life"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="13sp"android:text="生命值"/><TextViewandroid:id="@+id/attrack"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="13sp"android:text="攻击力"/><TextViewandroid:id="@+id/speed"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="13sp"android:text="速度"/></LinearLayout>
</RelativeLayout>
3、创建Itemlnfo类
package bzu.edu.cn.hj.cn.bzu.domain;
import java.io.Serializable;
/*** Created by yn on 2017/3/23.*/
public class Info implements Serializable{private String name;private int life;private int attack;private int quick;public Info(String name, int life, int attack, int quick) {this.name = name;this.life = life;this.attack = attack;this.quick = quick;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getLife() {return life;}public void setLife(int life) {this.life = life;}public int getAttack() {return attack;}public void setAttack(int attack) {this.attack = attack;}public int getQuick() {return quick;}public void setQuick(int quick) {this.quick = quick;}
}
Intent除了传递基本类型之外,也能传递Serializable或Parcelable类型的数据。
4、创建ShopActivity
package bzu.edu.cn.hj;import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import bzu.edu.cn.hj.cn.bzu.domain.Info;
import static bzu.edu.cn.hj.R.id.info;public class ShopActivity extends AppCompatActivity implements View.OnClickListener{private TextView name,life,attack,speed;private Info info;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_shop);//(1)初始化显示到界面上的数据info=new Info("金剑",20,100,20);//(2)找需要的控件findViewById(R.id.rl).setOnClickListener(this);name=(TextView)findViewById(R.id.tv_name);life=(TextView)findViewById(R.id.life);attack=(TextView)findViewById(R.id.attrack);speed=(TextView)findViewById(R.id.speed);//(3)初始化数据name.setText(info.getName()+"");life.setText("生命值+"+info.getLife());attack.setText("攻击力+"+info.getAttack());speed.setText("敏捷度+"+info.getQuick());}@Overridepublic void onClick(View view){switch(view.getId()){case R.id.rl:Intent intent=new Intent();intent.putExtra("info",info);setResult(10,intent);finish();break;default:break;}}
}
ShopActivity是用来展示装备信息的,当点击ShopActivity的装备时,会调用MainActivity并将装备信息回传给MainActivity。

setReilt()方法的作用是让当前Activity返回到它的调用者
5、编写界面交互代码(MainActivity)
package bzu.edu.cn.hj;import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import bzu.edu.cn.hj.cn.bzu.domain.Info;public class Main6Activity extends AppCompatActivity {private ProgressBar pb1, pb2, pb3;private TextView life, speed, attack;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main6);//找到我们需要的控件pb1 = (ProgressBar) findViewById(R.id.pbsm1);pb2 = (ProgressBar) findViewById(R.id.pbsm2);pb3 = (ProgressBar) findViewById(R.id.pbsm3);life = (TextView) findViewById(R.id.tv_life);attack = (TextView) findViewById(R.id.tv_attack);speed = (TextView) findViewById(R.id.tv_speed);//初始化一下进度条的最大值pb1.setMax(1000);pb2.setMax(1000);pb3.setMax(1000);}//点击按钮跳转到另一个ShopActivity,进行购买装备public void click1(View v) {Intent intent = new Intent(this, ShopActivity.class);startActivityForResult(intent, 1);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == 10) {Info info = (Info) data.getExtras().get("info");updateProgressBar(info);}super.onActivityResult(requestCode, resultCode, data);}private void updateProgressBar(Info info) {int p1 = pb1.getProgress();int p2 = pb2.getProgress();int p3 = pb3.getProgress();pb1.setProgress(p1 + info.getLife());pb2.setProgress(p2 + info.getAttack());pb3.setProgress(p3 + info.getQuick());life.setText(pb1.getProgress() + "");attack.setText(pb2.getProgress() + "");speed.setText(pb3.getProgress() + "");}
}
MainActivity主要用于响应按钮的点击事件,并将返回的装备信息显示到指定的ListView控件中

Android-回传数据(装备选择)相关推荐

  1. android回传数据实验报告,传热综合实验实验报告.doc

    传热综合实验 一.实验目的: 掌握传热系数K.传热膜系数α1的测定方法,加深对其概念和影响因素的理解: 掌握用最小二乘法确定关联式中常熟A.指数m的值: 通过对普通套管换热器和强化套管换热器的比较,了 ...

  2. java头像选择系统_安卓Java源程序 带选择头像功能的用户注册 使用Intent回传数据...

    本帖最后由 dori 于 2020-12-18 23:12 编辑 完成带选择头像功能的用户注册应用程序开发. 图1 图2 图3 MainActivity.java文件:import androidx. ...

  3. Android中回传数据

    Android中提供了一个startActivityForResult()方法,来实现回传数据 接下来通过一段实例代码来显示如何使用startActivityForResult().Activity0 ...

  4. android数据回传多个页面_Android菜鸟起飞|使用Intent实现Activity跳转的两种方式(无回传数据和有回传数据)...

    使用Intent实现Activity跳转的两种方式 1. 使用startActivity(intnet)方法实现无回传数据的Activity跳转: 使用方法: 为实现跳转动作的控件绑定监听器,在点击事 ...

  5. [Android实例] 两边都能滑动的数据间隔选择SpaceBar

    该篇文章从eoeAndroid搬迁过来的,原文地址:[Android实例] 两边都能滑动的数据间隔选择SpaceBar 前几天看汽车报价APP,突然发现一个很带感的效果,类似于seekbar,不过能够 ...

  6. android数据回传多个页面_Android-Activity之间回传数据

    一个activity中有不同按键向同一个activity跳转 MainActivity中有两个按键,都可以跳转到SecondActivity,但执行的操作不同. MainActivity代码如下,1和 ...

  7. Tencent login函数登录成功后无法成功回传数据(无法返回回调方法) Android qq授权登录

    找了一下午问题在哪里,总是无法成功回传数据,后来才发现自己没有好好看Tencent的开发文档 在某些低端机上调用登录后,由于内存紧张导致APP被系统回收,登录成功后无法成功回传数据. mTencent ...

  8. Android DialogFragment 回传数据

    采用自定义接口的方式回传数据 一.软件截图: 二.代码: 1.定义DatePickerFragment,自定义的接口OnDateInputListener public class DatePicke ...

  9. Android实现数据存储技术

    本文介绍Android中的5种数据存储方式. 数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是: 1 使用SharedPreferences存储数据 2 ...

最新文章

  1. SAXParserFactory之求解
  2. 智能,万亿维空间中的求解
  3. 宣传推广费用_企业如何做好网络推广,提高网络推广的转化率?
  4. 二层交换机、三层交换机与路由器的比较
  5. 【中级软考】RSA、IDEA、RC4、MD5算法分别是什么?
  6. layui 如何去dom_javascript 怎么去引用layui里面的方法
  7. svg 动画_根据AI导出的SVG path制作SVG线条动画
  8. sql server 约束
  9. cnn 回归 坐标 特征图_RCNN, Fast R-CNN 与 Faster RCNN理解及改进方法
  10. java共享内存_Java共享内存
  11. 通过AT指令实现ESP8266模块和TCP服务器的数据传输
  12. [HEOI2014]大工程
  13. 数组的循环及跌送方式
  14. 在python中对文件操作的一般步骤是_文件操作(一) 笔记------python
  15. 深度学习-图像数据标注工具使用(LabelImg和BBox)
  16. 记录一次redmibook pro15声卡驱动相关问题
  17. java 实现macd算法_macd 的java版本实现 包含测试用例
  18. java自行车s码适合身高_公路自行车尺寸与身高的选择
  19. 种类并查集(POJ1703)
  20. 数据库主流容灾方案对比分析

热门文章

  1. 云系统服务器收费,云服务器收费标准
  2. colorkey口红怎么样_colorkey小众品牌口红唇釉效果好吗 – 爱分享
  3. 机器学习最易懂之EM算法详解与python实现
  4. TiDB上百T数据拆分实践
  5. 机器学习读书笔记之6 - 贝叶斯系列
  6. Fiddler / Charles - 夜神模拟器证书安装App抓包
  7. Visual Studio安装与使用教程
  8. h5 android 字体设置,解决因为手机设置字体大小导致h5页面在webview中变形的BUG
  9. Python采集全国各地百度地图上店铺POI数据(母婴、美食等)
  10. Java判断日期格式是否正确