一、实验目的

1) 掌握组件ProgressBar的使用

2) 掌握startActivityForResult方法的使用

二、实验仪器

Android Studio

三、实验任务

装备选择

1.运行效果图

点击主人购买装备后跳转到装备页面

点击金剑装备后小宝宝的各个属性值增加。

2.设计思路

(1)将准备好的图标复制到res/drawable文件夹下

(2)创建一个垂直的线性布局,并在线性布局中创建1个表格布局和一个相对布局

(3)在线性布局中添加一个ImageButton和一个TextView,在表格布局的每一行添加两个TextView和一个ProgressBar,在相对布局中添加两个Button按钮。

(4)创建装备界面activity_shop,使用相对布局,并在相对布局中创建一个线性布局。在相对布局中添加一个View和一个TextView,在线性布局中添加三个TextView。

(5)在程序中创建一个cn.itcast.domain包,在该包中创建一个ItemInfo类,用于封装装备信息。

(6)创建ShopActivity用来展示装备信息,当单击ShopActivity的装备时,会调回MainActivity并将装备信息回传给MainActivity。

3.案例实现代码

1)mainActivity

package cn.edu.bzu.a34lab5;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.itcast.domain.ItemInfo;
import static cn.edu.bzu.a34lab5.R.layout.activity_shop;
public class MainActivity extends AppCompatActivity {
     ProgressBar pb1;
    ProgressBar pb2;
    ProgressBar pb3;
    TextView tv_life;
    TextView tv_atk;
    TextView tv_quick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //(1)找到我们关心的控件
         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);
         tv_atk=(TextView)findViewById(R.id.tv_atk);
         tv_quick=(TextView)findViewById(R.id.tv_quick);
        //(2)初始化进度条的最大值
        pb1.setMax(1000);
        pb2.setMax(1000);
        pb3.setMax(1000);
    }
    //点击按钮跳转到 shopping页面进行购买装备
    public void Click1(View v){
        Intent intent=new Intent(this,ShopActivity.class);
        //开启一个页面并且要开启这个页面的返回数据
        startActivityForResult(intent,1);
    }
    public void Click2(View v){
        Intent intent=new Intent(this,ShopActivity.class);
        //开启一个页面并且要开启这个页面的返回数据
        startActivityForResult(intent,1);
    }
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        if(resultCode==10){
            //(1)代表数据来源于ShopActivity 取数据
           ItemInfo itemInfo=(ItemInfo) data.getExtras().get("itemInfo");
            //(2)更新一下ui
            updateProgressBar(itemInfo);
        }
        super.onActivityResult(requestCode,resultCode,data);
    }
    //更新当前控件的ui
    private void updateProgressBar(ItemInfo itemInfo){
       //(1)获取当前progressbar的进度
        int progress1=pb1.getProgress();
        int progress2=pb2.getProgress();
        int progress3=pb3.getProgress();
        //(2)更新一下progressbar的进度
        pb1.setProgress(progress1+itemInfo.getLife());
        pb2.setProgress(progress2+itemInfo.getAttack());
        pb3.setProgress(progress3+itemInfo.getQuick());
        //(3)更新一下Textview的值
        tv_life.setText(pb1.getProgress()+"");
        tv_atk.setText(pb2.getProgress()+"");
        tv_quick.setText(pb3.getProgress()+"");
    }
}

2)ShopActivity

package cn.edu.bzu.a34lab5;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.itcast.domain.ItemInfo;
import static cn.edu.bzu.a34lab5.R.id.tv_atk;
import static cn.edu.bzu.a34lab5.R.id.tv_life;
import static cn.edu.bzu.a34lab5.R.id.tv_speed;
public class ShopActivity extends AppCompatActivity implements View.OnClickListener {
    ItemInfo itemInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.activity_shop);
        findViewById(R.id.rl).setOnClickListener(this);
        //(1)初始化显示到界面上的数据
        itemInfo=new ItemInfo("金剑",20,100,20);
        //(2)找到控件显示数据
        TextView tv_name=(TextView)findViewById(R.id.tv_name);
        TextView tv_life=(TextView) findViewById(R.id.tv_life);
        TextView tv_attack=(TextView) findViewById(R.id.tv_attack);
        TextView tv_speed= (TextView) findViewById(R.id.tv_speed);
        //(3)初始化一下数据展示到控件上
        tv_name.setText(itemInfo.getName());
        tv_attack.setText("攻击力:"+itemInfo.getAttack());
        tv_life.setText("生命力:"+itemInfo.getLife());
        tv_speed.setText("敏捷度:"+itemInfo.getQuick());
    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.rl: //证明我们点击的就是这个布局
                //(1)获取当前的数据把itemInfo数据返回
               Intent intent= new Intent();
                intent.putExtra("itemInfo",itemInfo);
                //把结果返回给调用者(mainActivity)  通过onActivityResult方法返回
                setResult(10,intent);
                //(2)关闭当前页面,通过onActivityResult方法把数据返回
                finish();
                break;
            default:
                break;
        }
    }
}

3)ItemInfo

package cn.itcast.domain;
import android.content.Intent;
import java.io.Serializable;
/**
 * Created by 17862 on 2017/3/26.
 */
public class ItemInfo implements Serializable{
    private String name;
    private int  life;
    private int  attack;
    private int  quick;
    public ItemInfo(String name,int life,int attack,int quick){
        super();
        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;
    }
}

4)创建装备选择页面,程序界面对应布局文件activity_mian.xml如下所示(用的LinearLayout布局中嵌套了RelativeLayout和TableLayout布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="cn.edu.bzu.a34lab5.MainActivity">
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/baby"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="45dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="主人,快来给小宝宝购买装备吧!"
        android:gravity="center_horizontal"
        android:textSize="18sp"/>
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="25dp"
        >
        <!-- 第1行-->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="生命值:" />
            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:id="@+id/progressBar1" />
            <TextView
                android:id="@+id/tv_life"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="20dp"
                android:text="80"/>
        </TableRow>
        <!-- 第2行-->
        <TableRow
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="wrap_content"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="攻击力:" />
            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:id="@+id/progressBar2" />
            <TextView
                android:id="@+id/tv_atk"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:text="80"/>
        </TableRow>
        <!-- 第3行-->
        <TableRow
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="wrap_content"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="敏捷:" />
            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:id="@+id/progressBar3" />
            <TextView
                android:id="@+id/tv_quick"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="80"
                android:layout_marginTop="5dp"/>
        </TableRow>
    </TableLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
      >
        <Button
            android:id="@+id/btn_master"
            android:onClick="Click1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:drawableRight="@android:drawable/ic_menu_add"
            android:drawablePadding="3dp"
            android:text="主人购买装备"
            android:textSize="14sp"
            />
        <Button
            android:id="@+id/btn_baby"
            android:onClick="Click2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:drawableRight="@android:drawable/ic_menu_add"
            android:drawablePadding="3dp"
            android:text="小宝宝购买装备"
            android:textSize="14sp"/>
    </RelativeLayout>
</LinearLayout>

5)创建装备页面,程序界面对应布局文件activity_shop.xml如下所示(用的RelativeLayout布局中嵌套了和LinearLayout布局):

<?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="wrap_content"
    android:orientation="vertical"
    >
    <View
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@android:drawable/ic_menu_info_details"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        />
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="60dp"
        android:text="商品名称"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_life"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="生命值"
            android:textSize="13sp"/>
        <TextView
            android:id="@+id/tv_attack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="攻击力"
            android:textSize="13sp"/>
        <TextView
            android:id="@+id/tv_speed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="速度"
            android:textSize="13sp"/>
    </LinearLayout>
</RelativeLayout>

Android装备选择实验相关推荐

  1. Android 装备选择

    这次做的是一个购买装备并显示属性加成的案例,一个是购买界面,一个是装备属性界面. 属性加成由progressbar显示,还有就是数值变化. 1.装备购买界面如下: 代码如下 <?xml vers ...

  2. android装备选择,云顶之弈决斗大师阵容装备搭配_云顶之弈决斗大师阵容装备搭配2021_资讯...

    云顶之弈决斗大师阵容装备搭配,云顶之弈是由英雄联盟云顶之弈模式独立出来的游戏,原汁原味还原电脑上的英雄联盟云顶之弈模式,让玩家在手机上上也能感受到云顶之弈下棋的乐趣,玩家需要在云顶之弈里进行八人乱战, ...

  3. android装备选择,巫师三前期必拿装备武器_巫师三前期必拿最强装备推荐选择_攻略...

    巫师三狂猎是巫师系列游戏的第三部,游戏是根据小说改编而来,既有小说的剧情又有精美逼真的游戏画面,曾获奖无数!第三部的游戏剧情是讲主人公杰洛特(猎魔人)的最终冒险.游戏以战斗为主,并结合主线任务和支线任 ...

  4. android装备选择,原神迪卢克武器怎么选_原神迪卢克武器选择推荐

    原神迪卢克武器怎么选.原神中的迪卢克是一个操纵搭大剑的角色,还是十分强力的,那么这个角色应该怎么样选择武器呢?本期小编就为大家带来了相关内容,感兴趣的小伙伴们,快一起来看看吧. 西风大剑: 武器技能: ...

  5. android对象识别实验报告,Android 3相册实验报告.doc

    Android 3相册实验报告 中原工学院计算机学院 软件平台开发技术大作业 班 级: 软件121 作品名称: 3D相册 姓 名: 辛俊闪 学 号: 201200834101 指导教师: 刘凤华 20 ...

  6. 移动开发技术(Android)——综合实验

    移动开发技术(Android)--综合实验 一.MainActivity的设计 1.activity_main布局文件显示效果如下图所示: 2.MainActivity实现的功能,要求如下: 二.De ...

  7. 惠勒延迟选择实验_肯·惠勒(Ken Wheeler)与开源软件的兴衰

    惠勒延迟选择实验 In this episode of the Versioning Show, Tim and David are joined by Ken Wheeler, a Formidab ...

  8. Android-回传数据(装备选择)

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

  9. Android技术应用实验指导书,Android应用开发实验指导书

    第 1 页手机应用开发实验指导书西南科技大学计算机科学与技术学院2015.11第 2 页目录手机应用开发 .1实验指导书 .1实验一:搭建 Android开发平台和创建 HelloWorld程序 .- ...

  10. android double比较大小吗,Android双向选择控件DoubleSeekBar使用详解

    本文实例为大家分享了Android双向选择控件DoubleSeekBar的使用方法,供大家参考,具体内容如下 先看效果图 1.DoubleSlideSeekBar public class Doubl ...

最新文章

  1. 《软件工程方法与实践》—— 导读
  2. 洛谷 P1205 [USACO1.2]方块转换 Transformations
  3. SpringBoot2.1.9 多Redis Lettuce配置
  4. redis常规命令记录
  5. c++可视化_数据可视化——如何让你的信息图被记住
  6. eclipse32位python版下载_python之(3)Python Eclipse+PyDec下载和安装教程(超级详细)...
  7. 标准h5的定位_H5地理定位
  8. NOIP2017错题
  9. zbbz加载成功用不了_cad加载应用程序不能加载
  10. 电脑计算机配置应用程序兼容性,软件和系统不兼容怎么办 电脑禁用程序兼容助手服务的操作方法...
  11. Linux系统下安装ssh服务
  12. Openssl学习——x509证书函数
  13. openbsd运行Linux应用程序,为什么默认的Linux安装运行的进程多于默认的OpenBSD安装?...
  14. Windows错误系统配置提权之系统服务权限配置错误 (二)
  15. 第十八章 Radix树路由表
  16. pyqt 控件焦点_PyQt5 控件学习(一个一个学习之QTextEdit)
  17. jenkins添加从节点
  18. 一个女孩的工作经历告诉我们
  19. LINUX自学第一课
  20. 基于FPGA的数字等精度频率计

热门文章

  1. 电商会员等级制度总结
  2. oracle中execute函数,oracle中execute immediate的使用(select/insert/update/delete)详解
  3. a55 matlab排列组合_matlab 排列组合函数的用法
  4. banner图片通用设置
  5. 中级软件设计师刷题笔记
  6. 斐波那契数列几个公式
  7. Demand Side Platform (需求方平台)名词一览
  8. 前端小白找工作日记(1)
  9. Linux——clamAV查杀病毒与防护
  10. 解析智能推荐系统开发中十大关键要素