草庐IT

移动应用开发技术——Android Studio实验二——Activity的调用——竹园摘竹子

轨迹_669 2023-04-17 原文

掌握线性布局和相对布局的使用方法

掌握基本控件的属性功能及使用方法

掌握Activity的数据回传

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

布局与控件的用法

使用Activity的数据回传实现实验要求

activity_main.xml

<?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"
    xmlns:http="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/bg">
    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#009366">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="首页"
            android:textSize="25sp"
            android:gravity="center"
            android:layout_margin="10dp"
            android:textColor="#FFFFFF"/>
    </LinearLayout>
<RelativeLayout
    android:layout_below="@+id/ll_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/iv_1"
        android:src="@drawable/panda"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"/>
    <ImageButton
        android:id="@+id/imbtn_1"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_alignTop="@+id/iv_1"
        android:layout_toRightOf="@+id/iv_1"
        android:layout_marginLeft="50dp"
        android:background="@drawable/btn_peach"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="去竹园"
        android:layout_alignTop="@+id/imbtn_1"
        android:layout_alignLeft="@+id/imbtn_1"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="15dp"
        android:textSize="15sp"/>


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bamboo"
        android:layout_below="@+id/imbtn_1"
        android:layout_marginTop="20dp"
        android:layout_toRightOf="@+id/iv_1"
        android:layout_marginLeft="20dp"
        android:id="@+id/iv_2"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/iv_2"
        android:layout_alignTop="@+id/iv_2"
        android:layout_marginLeft="10dp"
        android:text="去采摘吧"
        android:textSize="30sp"
        android:textColor="#000"
        android:id="@+id/tv_1"/>



</RelativeLayout>

</RelativeLayout>

MainActivity.java

package com.example.shiyan2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.Attribution;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton imbtn_1=(ImageButton) findViewById(R.id.imbtn_1);
        imbtn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,ZhuyuanActivity2.class);
                startActivityForResult(intent,1);
            }
        });
    }
    @Override
    protected 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)+"个");
            }
        }

    }
}

activity_zhuyuan2.xml

<?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">
    <LinearLayout
        android:id="@+id/ll_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#009366">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="首页"
            android:textSize="25sp"
            android:gravity="center"
            android:layout_margin="10dp"
            android:textColor="#FFFFFF"/>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/ll_2">

        <ImageView
            android:id="@+id/iv_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/tree" />
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bamboo"
            android:id="@+id/im_btn_1"
            android:layout_alignTop="@+id/iv_3"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp"
            />

        <ImageButton
            android: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" />

        <ImageButton
            android:id="@+id/im_btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/im_btn_1"
            android:layout_marginTop="80dp"
            android:layout_marginLeft="220dp"
            android:background="@drawable/bamboo" />
        <ImageButton
            android:id="@+id/im_btn_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/im_btn_1"
            android:layout_marginTop="160dp"
            android:layout_marginLeft="270dp"
            android:background="@drawable/bamboo" />
        <ImageButton
            android:id="@+id/im_btn_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/im_btn_1"
            android:layout_marginTop="160dp"
            android:layout_marginLeft="160dp"
            android:background="@drawable/bamboo" />
        <ImageButton
            android:id="@+id/im_btn_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/im_btn_1"
            android:layout_marginTop="160dp"
            android:layout_marginLeft="60dp"
            android:background="@drawable/bamboo" />
    <ImageButton
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_below="@+id/iv_3"
        android:background="@drawable/btn_peach"
        android:layout_alignRight="@+id/iv_3"
        android:layout_marginTop="20dp"
        android:id="@+id/im_btn_7"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/im_btn_7"
            android:text="退出竹园"
            android:textColor="#000"
            android:layout_alignLeft="@id/im_btn_7"
            android:textSize="15sp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="15dp"/>


    </RelativeLayout>
    



</RelativeLayout>

ZhuyuanActivity2.java

package com.example.shiyan2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class ZhuyuanActivity2 extends AppCompatActivity {
protected int bamboo=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuyuan2);
        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() {
                @Override
                public void onClick(View view) {
                    bamboo++;
                    imageButton.setBackground(null);
                }
            });
            ImageButton back=findViewById(R.id.im_btn_7);
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent();
                    intent.putExtra("bamboo_number",bamboo);
                    setResult(1,intent);
                    finish();
                }
            });
        }
    }
}

小专题讲解

六个竹子一个一个建立对象编写点击事件太麻烦

直接两个循环解决问题

先搞一个list集合类

用于等会循环把按钮对象全装进去

还得弄个这

Resources res =getResources();

等会获取id好用

看循环

一个一个装进集合里面

然后foreach都遍历一遍 都装上监听器

把点击事件弄成点击后bamboo加一 然后把背景background点成null空 就形成了点击后消失还能计数的效果

实验结果

主页面

点击去竹园按钮前往下一个activity

竹子点击后消失 是因为设置了点击后setbackground=null 并计数 待会传回第一个界面

这里点击了三个imagebutton计数3,点击退出竹园 通过finish()返回上个activity 通过intent传输数据

有关移动应用开发技术——Android Studio实验二——Activity的调用——竹园摘竹子的更多相关文章

  1. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  2. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  3. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  4. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  7. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  8. Ruby Sinatra 配置用于生产和开发 - 2

    我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm

  9. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  10. ruby - 是否可以覆盖 gemfile 进行本地开发? - 2

    我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI

随机推荐