河南大学移动应用开发实验2

server/2025/3/19 23:47:56/

通过线性布局和相对布局来搭建两个Activity界面,界面效果如下图所示。当点击“去竹园按钮后”,跳转到第二个界面。在第二个界面中,点击界面中间竹子,可统计摘取竹子数并使对应竹子图片消失。点击退出竹园按钮后,返回第一个界面,并将摘取竹子数显示到竹子图片后方

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:http="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg"><LinearLayoutandroid:id="@+id/ll_1"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#009366"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:gravity="center"android:text="首页"android:textColor="#FFFFFF"android:textSize="25sp" /></LinearLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@+id/ll_1"><ImageViewandroid:id="@+id/iv_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:src="@drawable/panda" /><ImageButtonandroid:id="@+id/imbtn_1"android:layout_width="125dp"android:layout_height="67dp"android:layout_alignTop="@+id/iv_1"android:layout_marginLeft="50dp"android:layout_toRightOf="@+id/iv_1"android:background="@drawable/btn_peach" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/imbtn_1"android:layout_alignTop="@+id/imbtn_1"android:layout_marginLeft="25dp"android:layout_marginTop="15dp"android:text="去竹园"android:textSize="25sp" /><ImageViewandroid:id="@+id/iv_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/imbtn_1"android:layout_marginLeft="20dp"android:layout_marginTop="20dp"android:layout_toRightOf="@+id/iv_1"android:src="@drawable/bamboo" /><TextViewandroid:id="@+id/tv_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/iv_2"android:layout_marginLeft="10dp"android:layout_toRightOf="@+id/iv_2"android:text="摘到7个"android:textColor="#000"android:textSize="30sp" /></RelativeLayout>
</RelativeLayout>package com.example.myexp2_bamboo;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ImageButton imbtn_1 = (ImageButton) findViewById(R.id.imbtn_1);//Intent intent = new Intent(this, activity2_main.class);imbtn_1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(MainActivity.this, activity2_main.class);startActivityForResult(intent,1);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent intent) {super.onActivityResult(requestCode, resultCode, intent);if (requestCode == 1) {if (resultCode == 1) {TextView tv = findViewById(R.id.tv_1);tv.setText("摘到" + intent.getIntExtra("bamboo_number", 0) + "个");}}}
}<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/tree_bg"><LinearLayoutandroid:id="@+id/ll_2"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#009366"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:gravity="center"android:text="竹园"android:textColor="#FFFFFF"android:textSize="25sp" /></LinearLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@+id/ll_2"><ImageViewandroid:id="@+id/iv_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@drawable/tree" /><ImageButtonandroid:id="@+id/im_btn_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/iv_3"android:layout_centerHorizontal="true"android:layout_marginTop="25dp"android:background="@drawable/bamboo" /><ImageButtonandroid:id="@+id/im_btn_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/im_btn_1"android:layout_marginLeft="100dp"android:layout_marginTop="80dp"android:background="@drawable/bamboo" /><ImageButtonandroid:id="@+id/im_btn_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/im_btn_1"android:layout_marginLeft="220dp"android:layout_marginTop="80dp"android:background="@drawable/bamboo" /><ImageButtonandroid:id="@+id/im_btn_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/im_btn_1"android:layout_marginLeft="270dp"android:layout_marginTop="160dp"android:background="@drawable/bamboo" /><ImageButtonandroid:id="@+id/im_btn_5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/im_btn_1"android:layout_marginLeft="160dp"android:layout_marginTop="160dp"android:background="@drawable/bamboo" /><ImageButtonandroid:id="@+id/im_btn_6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/im_btn_1"android:layout_marginLeft="60dp"android:layout_marginTop="160dp"android:background="@drawable/bamboo" /><ImageButtonandroid:id="@+id/im_btn_7"android:layout_width="100dp"android:layout_height="50dp"android:layout_below="@+id/iv_3"android:layout_alignRight="@+id/iv_3"android:layout_marginTop="20dp"android:background="@drawable/btn_peach" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@id/im_btn_7"android:layout_alignTop="@+id/im_btn_7"android:layout_marginLeft="20dp"android:layout_marginTop="15dp"android:text="退出竹园"android:textColor="#000"android:textSize="15sp" /></RelativeLayout></RelativeLayout>package com.example.myexp2_bamboo;import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;
import java.util.List;public class activity2_main extends AppCompatActivity {protected int bamboo = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity2_main);List<ImageButton> imts = new ArrayList<ImageButton>();Resources res = getResources();for (int i = 1; i <= 6; i++) {int id = res.getIdentifier("im_btn_" + i, "id", getPackageName());ImageButton zhuzi = findViewById(id);imts.add(zhuzi);}for (ImageButton imageButton : imts) {imageButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {bamboo++;imageButton.setBackground(null);}});ImageButton back = findViewById(R.id.im_btn_7);back.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent();intent.putExtra("bamboo_number", bamboo);setResult(1, intent);finish();}});}}
}<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><applicationandroid:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.MyExp2_bamboo"tools:targetApi="31"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".activity2_main"android:exported="true" /></application></manifest>

 

通过本次实验,我掌握了线性布局和相对布局的使用方法,掌握基本控件的属性功能及使用方法,基本掌握了Activity的数据回传。在实践过程中,我学会了如何利用Intent组件优雅地实现Activity之间的跳转

Java代码部分的编写不仅复习和巩固了我之前所学的知识,我在编写中间代码时遇到了功能无法实现的问题,因为没有在AndroidManifest.xml文件中正确登记组件。


http://www.ppmy.cn/server/176369.html

相关文章

Java数据类型 Arrays VS ArraysList VS LikedList 解析

在学习Java过程中&#xff0c;在刷题时总是搞不清楚这三种数据结构的区别&#xff0c;打算写篇文章记录一下 Arrays VS ArrayList ArrayList VS LinkedList 总结 Arrays VS ArrayList ArraysArrayList类型Java的基本数据类型Java集合框架中的一个类&#xff0c;实现了List接…

《解锁华为黑科技:MindSpore+鸿蒙深度集成奥秘》

在数字化浪潮汹涌澎湃的当下&#xff0c;人工智能与操作系统的融合已成为推动科技发展的核心驱动力。华为作为科技领域的先锋&#xff0c;其AI开发框架MindSpore与鸿蒙系统的深度集成备受瞩目&#xff0c;开启了智能生态的新篇章。 华为MindSpore&#xff1a;AI框架的创新先锋…

2018年全国职业院校技能大赛高职组-计算机网络应用竞赛竞赛样题I卷

目录 总体规划 模块二:设备基础信息配置 模块三:云计算融合网络部署 模块四:移动互联网络组建与优化 模块五:网络空间安全部署 总体规划 CII网络公司总部设有研发、市场、供应链、售后等4个部门,统一进行IP地址及业务资源的规划和分配。集团总部及亚太地区的网络拓扑结构如…

【WEB APIs】正则表达式

目录 1. 正则表达式 1.1 介绍 1.2 语法​编辑 1.3 元字符 1.3.1 边界符 1.3.2 量词 1.3.3 字符类 1.4 修饰符 2. 小兔鲜页面注册 3. 小兔鲜登陆页面 login index 4. 小兔鲜图片切换放大镜 1. 正则表达式 1.1 介绍 1.2 语法 const str 前端啦啦啦啦啦啦lllll// 1.…

MySQL连接较慢原因分析及解决措施

文章目录 整体说明一、问题现象二、问题分析2.1、DNS反向解析问题2.2、网络问题2.3、SSL/TLS协商问题2.4、自动补全的延迟 三、问题解决 摘要&#xff1a; MySQL连接较慢原因分析及解决措施 关键词&#xff1a; MySQL、连接缓慢、客户端、参数设置 整体说明 在使用MySQL的时候…

【论文精读】DifFace: Blind Face Restoration with Diffused Error Contraction

文章目录 0.前言1.当前问题2.怎么解决问题3.具体做法(Method)3.1 受什么的启发&#xff1f;(Motivation)3.2具体的模型设计(Design)3.3 整体算法 4.实验效果4.1 Synthetic(CelebA-Test)4.2 Real World &#xff08;LFW, WebPhoto, and WIDER&#xff09; 0.前言 这篇文章是被 …

高频SQL50题 第一天 | 1757. 可回收且低脂的产品、584. 寻找用户推荐人、595. 大的国家、1683. 无效的推文、1148. 文章浏览 I

1757. 可回收且低脂的产品 题目链接&#xff1a;https://leetcode.cn/problems/recyclable-and-low-fat-products/description/?envTypestudy-plan-v2&envIdsql-free-50 状态&#xff1a;已完成 考点&#xff1a;无 select product_id from Products where low_fats Y a…

漏洞挖掘—EDU SRC证书站漏洞挖掘记录

免责声明&#xff1a;请勿利用文章内的相关技术从事非法测试&#xff0c;由于传播、利用此文所提供的信息或者工具而造成的任何直接或者间接的后果及损失&#xff0c;均由使用者本人负责&#xff0c;所产生的一切不良后果与文章作者无关。该文章仅供学习用途使用。 前言 好久没…