1.先打开Android Studio
2.创建工程。
(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">
<ImageView
    android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/xiaoboabao"android:layout_gravity="center_horizontal"/><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"><View
        android:id="@+id/view"android:layout_width="30dp"android:layout_height="30dp"android:background="@android:drawable/ic_menu_info_details"android:layout_marginTop="50dp"/><TextView
        android: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="商品名称" /><LinearLayout        android: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"><TextView
            android:id="@+id/life"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="13sp"android:text="生命值"/><TextView
            android:id="@+id/attrack"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="13sp"android:text="攻击力"/><TextView
            android:id="@+id/speed"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="13sp"android:text="速度"/></LinearLayout>
</RelativeLayout>

(3)创建Info类。在程序中创建一个cn.bzu.domain包,在该包中创建一个Info类,用于封装装备信息。需注意的是:Intent除了传递基本类型之外,也能传递Serializable和Parcelable类型的数据,这里让Info实现Serializable接口。代码如下:

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;}
}

(4)创建ShopActivity.用来展示装备信息的,当点击ShopActivity页面时,会调回MainActivity,并将带数据回传给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.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;}}
}

(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() + "");}
}

(6).运行结果图

点击“主人购买装备”按钮,会跳转到装备显示界面,装备购买成功后,会看到下图:

到这里,案例就完成了。

Activity中的数据传送—案例: 购买装备相关推荐

  1. 从Activity中返回数据

    从Activity中返回数据 一.简介 这里也就是使用intent方式返回数据. 二.具体步骤 在MainActivity通过一个button访问Activity01页面,然后将Activity01页 ...

  2. 把内存中的数据传送到计算机硬盘称为,将内存中的数据传送到计算机硬盘的过程称为什么...

    将内存中的数据传送到计算机硬盘的过程称为写盘:写盘就是将一些想要长期保存的文件,比如影片.音乐.图片或文档刻在光盘上面作永久保存:写盘也叫刻录,一般需要安装刻录软件才能进行. 本文操作环境:Windo ...

  3. Activity的数据回传——案例:装备选择

    1.先打开Android Studio 2.创建工程. (1)设计用户交互页面,如图: 程序对应的布局文件(activity_main.xml)如下所示,在布局代码中用到了ProgressBar(进度 ...

  4. Activity的创建步骤+Activity之间的数据传递+案例(人品测试器)

    Android的四大组件: 1.activity(多层界面运用) 2.广播接收者 3.服务 4.内容提供者 此外,我这里还会说道 5.多媒体编程(图形.声音.视频) 6.Fragment+动画 7.S ...

  5. 在微型计算机中把数据传送到软盘上称为,计算机应用基础知识测试

    A. 不会丢失 B. 全部丢失 C. 大部分丢失 D. 局部丢失 (7) 在微型计算机中,把数据传送到软盘上称为( ). A. 写盘 B. 读盘 C. 输入 D. 以上都不是 (8) 下列说法中,正确 ...

  6. Android 中activity中传递数据的方式

    方式一 通过Intent进行数据传递(日后更新) 方式二 通过剪切板进行数据的传递 首先在第一个Activity中,设置所要进行传递的数据 /*** 通过剪贴板传递字符串数据的操作**/private ...

  7. Http协议中的数据传送之多重表单提交--multipart/form-data

    RFC 2188: Returning Values from Forms: multipart/form-data,这份文件说明了在 HTTP POST 讯息中使用多种格式信息的作法,它可以用在许多 ...

  8. movs 数据传送指令_PLC中简单好用的传送指令功能,你学会了吗?

    戳上方蓝字 "技成电工课堂" 关注我们哦! PLC的各种功能指令中,传送指令的使用非常频繁,且相对简单.一说到简单,我猜已经有很多人蠢蠢欲动,迫不及待地想征服这个传送指令了.那么, ...

  9. 父activity启动子activity并传递数据

    1. Intent component : activity,service,broadcast receiver以及content provider component 与操作系统 通信的一种媒介工 ...

最新文章

  1. qt 拖拽 修改大小(二)
  2. osg中实现HUD(OSG初级篇1)
  3. 欠拟合和过拟合学习笔记
  4. java ftp connect_java实现ftp的几种方式(第3方包)
  5. 【转】测试工程师日常工作需要关注的问题
  6. Leetcode 347.前K个高频元素
  7. oracle 2的次方,Oracle第二次课 - osc_qyg23ccq的个人空间 - OSCHINA - 中文开源技术交流社区...
  8. 100个C语言经典小程序和C语言编写的小游戏
  9. 灵派编码器HTTP API接口说明
  10. 4款idea主题,非常的炫酷
  11. 计算机图书馆管理系统论文模板,基于Java的图书馆管理系统计算机科学与技术毕业设计(论文)...
  12. GIF动态图片分解,多帧动态图分解成多张静态图片
  13. Excel文件转换为XML以及Linux文件编码格式转换
  14. 华三交换机ping不通用户但用户_华三交换机ping不通路由器
  15. lvds输入悬空_LVDS原理及应用
  16. NOI2021 D类打铁记
  17. keil5编写C51程序
  18. ManualResetEvent用法详解
  19. Python读取Word文档段落或者表格
  20. 大数据技术包括哪些内容?

热门文章

  1. 上海交通大学计算机系非全日制,关于上海交通大学非全日制研究生中最便宜的专业...
  2. 【需求】Python利用selenium抓取顺丰的地址数据
  3. 安卓隐藏摄像_一款可以隐藏录像的app
  4. friendly发音_欧路词典|英汉-汉英词典 friendly是什么意思_friendly的中文解释和发音_friendly的翻译_friendly怎么读...
  5. 阿里妈妈展示广告召回之多场景建模算法
  6. 调查问卷动态生成的一点探索
  7. 基于Matlab的磁力计校准(附源码)
  8. 算术运算符:取余(取模)%
  9. 云计算:细分之七大类商业模式
  10. PHP报错:Classes\\PHPExcel\\Cell.php Line(594) Invalid cell coordinate ESIGN1