【Android开发】不同Activity之间的数据回传实例(一)摘桃子游戏

这篇具有很好参考价值的文章主要介绍了【Android开发】不同Activity之间的数据回传实例(一)摘桃子游戏。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、功能介绍

该项目实现的功能主要有:

  1. 在首页显示一个按钮点击该按钮跳转到桃园页面
  2. 在桃园页面,点击桃子会弹窗显示摘到几个桃子,同时被点击桃子消失,总桃子数+1
  3. 点击退出桃园会返回首页,首页桃子数会根据点击的桃子数动态增加

二、代码实现

1. 资源准备

将项目所需要的图片bg.png、monkey.png、btn_peach.png、peach_pic.png 导入程序的drawablehdpi文件夹中(默认情况下程序中没有drawable-hdpi 文件夹,需手动在res 文件夹中创建一个)。

2. 布局文件设计

2.1 主Activity

在layout文件夹中编辑activity_main.xml

<?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">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#008577"
        android:gravity="center"
        android:text="首页"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/iv_monkey"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/monkey" />

        <Button
            android:id="@+id/btn_peach"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="250dp"
            android:layout_toRightOf="@id/iv_monkey"
            android:background="@drawable/btn_peach"
            android:gravity="center"
            android:text="去桃园"
            android:textColor="@android:color/black"
            android:textSize="18sp" />
        <ImageView
            android:id="@+id/iv_peach"
            android:layout_width="45dp"
            android:layout_height="35dp"
            android:layout_below="@+id/btn_peach"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:src="@drawable/peach_pic" />
        <TextView
            android:id="@+id/tv_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn_peach"
            android:layout_marginTop="25dp"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/iv_peach"
            android:text="摘到0 个"
            android:textColor="@android:color/black"
            android:textSize="20sp" />
    </RelativeLayout>
</LinearLayout>
2.2 辅Activity

在layout文件夹中新增activity_peach.xml

<?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">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#008577"
        android:gravity="center"
        android:text="桃园"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/tree_bg">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginTop="70dp"
            android:background="@drawable/tree">
            <Button
                android:id="@+id/btn_one"
                android:layout_width="55dp"
                android:layout_height="40dp"
                android:layout_marginLeft="140dp"
                android:layout_marginTop="35dp"
                android:background="@drawable/peach_pic" />

            <Button
                android:id="@+id/btn_two"
                android:layout_width="55dp"
                android:layout_height="40dp"
                android:layout_below="@id/btn_one"
                android:layout_marginLeft="80dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/peach_pic" />
            <Button
                android:id="@+id/btn_three"
                android:layout_width="55dp"
                android:layout_height="40dp"
                android:layout_below="@id/btn_one"
                android:layout_marginLeft="70dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/btn_two"
                android:background="@drawable/peach_pic" />
            <Button
                android:id="@+id/btn_four"
                android:layout_width="55dp"
                android:layout_height="40dp"
                android:layout_below="@id/btn_two"
                android:layout_marginLeft="25dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/peach_pic" />
            <Button
                android:id="@+id/btn_five"
                android:layout_width="55dp"
                android:layout_height="40dp"
                android:layout_below="@id/btn_two"
                android:layout_marginLeft="70dp"
                android:layout_marginTop="15dp"
                android:layout_toRightOf="@id/btn_four"
                android:background="@drawable/peach_pic" />

            <Button
                android:id="@+id/btn_six"
                android:layout_width="55dp"
                android:layout_height="40dp"
                android:layout_below="@id/btn_two"
                android:layout_marginLeft="45dp"
                android:layout_marginTop="15dp"
                android:layout_toRightOf="@id/btn_five"
                android:background="@drawable/peach_pic" />
        </RelativeLayout>
        <Button
            android:id="@+id/btn_exit"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_margin="50dp"
            android:background="@drawable/btn_peach"
            android:text="退出桃园"
            android:gravity="center"
            android:textColor="@android:color/black"
            android:textSize="18sp"/>
    </RelativeLayout>
</LinearLayout>
2.3 其他资源文件

主题文件themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.PickPeach" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
    <style name="MyCustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

3. 实现逻辑功能

3.0 全局配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hzj.pickpeach">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/MyCustomTheme">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".PeachActivity"
            android:exported="true">
        </activity>
    </application>

</manifest>
3.1 首页功能

在MainActivity 中实现“去桃园”按钮的点击事件,当点击按钮时程序跳转到桃园摘桃的界面。同时,还需要接收桃园界面回传过来的桃子个数。

public class MainActivity extends AppCompatActivity {
    private Button btn_peach;
    private TextView tv_count;
    private int totalCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        btn_peach = findViewById(R.id.btn_peach);
        tv_count = findViewById(R.id.tv_count);
        //为按钮设置点击事件监听器
        btn_peach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置意图对象,实现界面跳转
                Intent intent = new Intent(MainActivity.this, PeachActivity.class);
                startActivityForResult(intent, 1);
//                startActivity(intent);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //判断请求码和返回码是否正确
        if (requestCode==1 && requestCode==1) {
            //获取回传的数据
            int count = data.getIntExtra("count", 0);;
            totalCount = totalCount + count;
            tv_count.setText("摘到" + totalCount + "个");
        }
    }
}
3.2 桃园界面的摘桃效果

点击桃子实现摘桃效果,同时全局桃子数动态变化,点击退出桃园,返回首页,同时更新首页桃子数。

public class PeachActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_exit;
    private int count = 0;//桃子个数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_peach);
        init();
    }

    private void init() {
        btn_one = findViewById(R.id.btn_one);
        btn_two = findViewById(R.id.btn_two);
        btn_three = findViewById(R.id.btn_three);
        btn_four = findViewById(R.id.btn_four);
        btn_five = findViewById(R.id.btn_five);
        btn_six = findViewById(R.id.btn_six);
        btn_exit = findViewById(R.id.btn_exit);
        btn_one.setOnClickListener(this);
        btn_two.setOnClickListener(this);
        btn_three.setOnClickListener(this);
        btn_four.setOnClickListener(this);
        btn_five.setOnClickListener(this);
        btn_six.setOnClickListener(this);
        btn_exit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_one: //第一个桃子的点击事件
                info(btn_one);
                break;
            case R.id.btn_two: //第二个桃子的点击事件
                info(btn_two);
                break;
            case R.id.btn_three: //第三个桃子的点击事件
                info(btn_three);
                break;
            case R.id.btn_four: //第四个桃子的点击事件
                info(btn_four);
                break;
            case R.id.btn_five: //第五个桃子的点击事件
                info(btn_five);
                break;
            case R.id.btn_six: //第六个桃子的点击事件
                info(btn_six);
                break;
            case R.id.btn_exit: //“退出桃园”按钮的点击事件
                returnData();
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + view.getId());
        }
    }

    /**
     * 按钮的点击事件处理
     */
    private void info(Button btn) {
        //桃子个数加1
        count++;
        //被摘掉的桃子设置为隐藏不可见
        btn.setVisibility(View.INVISIBLE);
        Toast.makeText(PeachActivity.this, "摘到" + count + "个桃子",
                Toast.LENGTH_LONG).show();
    }

    /**
     * 将数据回传到上个界面
     */
    private void returnData() {
        //设置意图对象,将摘桃子的个数回传给首页界面
        Intent intent = new Intent();
        intent.putExtra("count", count);
        //返回码标识
        setResult(1, intent);
        //销毁本界面
        PeachActivity.this.finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //当点击设备返回键时,调用数据回传方法,返回首页界面
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            returnData();
        }
        return false;
    }
}

三、效果展示

【Android开发】不同Activity之间的数据回传实例(一)摘桃子游戏,期末复习,android,游戏,java
【Android开发】不同Activity之间的数据回传实例(一)摘桃子游戏,期末复习,android,游戏,java
【Android开发】不同Activity之间的数据回传实例(一)摘桃子游戏,期末复习,android,游戏,java文章来源地址https://www.toymoban.com/news/detail-816569.html

到了这里,关于【Android开发】不同Activity之间的数据回传实例(一)摘桃子游戏的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android kotlin系列讲解(入门篇)使用Intent在Activity之间穿梭

    返回总目录 上一篇: Android kotlin系列讲解(入门篇)Activity的理解与基本用法        你应该已经对创建 Activity 的流程比较熟悉了,那我现在在 ActivityTest 项目再快速地创建一个 Activity 。        还是右击 com.example.activitytest 包→ New → Activity → Empty Views Activity ,会弹出

    2024年02月12日
    浏览(27)
  • 【开发问题】flink-cdc不用数据库之间的,不同类型的转化

    我一开始是flink-cdc,oracle2Mysql,sql 我一开始直接用的oracle【date】类型,mysql【date】类型,sql的校验通过了,但是真正操作数据的时候报错,告诉我oracle的数据格式的日期数据,不可以直接插入到mysql格式的日期数据,说白了就是数据格式不一致导致的 我想的是既然格式不对

    2024年02月12日
    浏览(35)
  • Android开发笔记(三)—Activity篇

    (1)从当前页面跳到新页面: startActivity(new Intent(源页面.this,目标页面.class)) (2)从当前页面回到上一个页面,相当于关闭当前页面: finish() onCreate:创建活动。把页面布局加载到内存,进入了初始状态。 onStart:开始活动。把活动页面显示在屏幕上,进入了就绪状态。 on

    2024年02月06日
    浏览(25)
  • Android开发-Activity中“android:exported“属性的作用,以及“Permission Denial: starting Intent“错误解决

    如何在一个应用程序中,启动另外一个应用程序?最近正有这样的需求,也踩了一个小坑。本节介绍使用Activity中\\\"android:exported\\\"属性来实现这种访问。 Activity中\\\"android:exported\\\"属性说明: 在程序清单AndroidMenifest.xml文件中,可以设置这个属性。 Android中的Activity中\\\"android:exported\\\"属

    2024年02月06日
    浏览(39)
  • Qt/C++音视频开发50-不同ffmpeg版本之间的差异处理

    ffmpeg的版本众多,从2010年开始计算的项目的话,基本上还在使用的有ffmpeg2/3/4/5/6,最近几年版本彪的比较厉害,直接4/5/6,大版本之间接口有一些变化,特别是一些废弃接口被彻底删除了,而网络上的各种文章几乎都是ffmpeg3左右为主的,所以本人在写这个全功能播放组件的时

    2024年02月14日
    浏览(41)
  • Android开发页面跳转异常且应用停止:android.content.ActivityNotFoundException: Unable to find explicit activity

    E/AndroidRuntime: FATAL EXCEPTION: main     Process: com.example.app01, PID: 13901     android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.app01/com.example.app01.RegisterActivity}; have you declared this activity in your AndroidManifest.xml? 根据日志信息不难发现问题的所在,就是我们需要将存

    2024年02月16日
    浏览(33)
  • Mysql不同数据库之间表结构同步

    开发环境的Mysql表结构做了修改,要同步到其他环境数据库中使用数据库管理工具JookDB的表结构同步功能就很方便。虽然Navicat也有这个功能但是有免费的当然是用免费的。 用JookDB添加数据库后在数据库节点上右键选择“同步结构”即可开始表结构同步。 1.选择结构同步的源库

    2024年02月05日
    浏览(38)
  • 不同activity项目创建时的区别

    在 Android Studio 中创建项目时,可以选择创建不同类型的 Activity 作为应用程序的入口点。其中,包括 Empty Activity、Basic Activity、Empty Compose Activity 和 Basic Compose Activity 四种类型。 Empty Activity:这是最简单的 Activity 类型,它只包含一个空白的屏幕和一个默认的菜单项,可以在其中

    2024年01月21日
    浏览(47)
  • vue3+ts+element-plus实际开发之导出表格和不同类型之间相互赋值

    1. 安装依赖 npm run xlsx 2. 引入,import * as XLSX from “xlsx”; 3. 报错找不到模块“xlsx”或其相应的类型声明 修改成大写就好了 import * as XLSX from \\\'XLSX\\\' ,如果没有报提示就直接用 4. 使用导出文件 //---- 导出表 1. 直接用a标签下载 鼠标移入样式,点击自动下载 2. 有特殊数据需要解析

    2024年02月15日
    浏览(36)
  • SQL server 实现不同服务器之间的数据同步(作业实现)

            公司目前有两台服务器,一台计划用于存储历史数据(History),一台计划用于日常(Daily),现在的需求是将日常的SQL server产生的历史数据表,通过定期同步的方式存储到存放历史数据的服务器上(Daily——History)这个顺序一定要记清楚! 目录 SQL server实现数据

    2024年02月16日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包