[Android开发练习3] 四季图的切换

news2024/10/10 14:25:04

 前言

        本题主要涉及到使用帧式布局,以及如何为组件设置点击的事件响应,包括获取事件源,设置事件监听器,在Activity文件中书写一般的业务逻辑代码。

文章目录

布局代码

text3_season.xml(layout)

style.xml(values)

buttonstyle.xml(drawable)

功能代码

SeasonActivity.java

 运行效果


布局代码

text3_season.xml(layout)

        使用帧式布局(FrameLayout)来存放图片控件,使用线性布局(LinearLayout)来存放四个按钮标签,全局采用相对布局(RelativeLayout),形成最终的展示页面。四季图可以自行从网上选择下载,并将其复制粘贴到drawable文件夹下进行调用。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <!--    帧式布局放图片控件-->
    <FrameLayout
        android:id="@+id/frameSeason"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:tag="春天"
            android:src="@drawable/spring"
            android:scaleType="fitXY"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </ImageView>
        <ImageView
            android:tag="夏天"
            android:visibility="gone"
            android:src="@drawable/summer"
            android:scaleType="fitXY"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </ImageView>
        <ImageView
            android:tag="秋天"
            android:visibility="gone"
            android:src="@drawable/autom"
            android:scaleType="fitXY"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </ImageView>
        <ImageView
            android:tag="冬天"
            android:visibility="gone"
            android:src="@drawable/winter"
            android:scaleType="fitXY"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </ImageView>
    </FrameLayout>
    <!--    线性布局放标签按钮-->
    <LinearLayout
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/spring"
            android:text="春天"
            style="@style/season_name"
            >
        </TextView>
        <TextView
            android:id="@+id/summer"
            android:text="夏天"
            style="@style/season_name"
            >
        </TextView>
        <TextView
            android:id="@+id/autuom"
            android:text="秋天"
            style="@style/season_name"
            >
        </TextView>
        <TextView
            android:id="@+id/winter"
            android:text="冬天"
            style="@style/season_name"
            >
        </TextView>

    </LinearLayout>

</RelativeLayout>

style.xml(values)

由于底部的按钮样式一致,故将其提取出来写成样式文件进行复用。

<resources>
    <style name="season_name">
        <item name="android:layout_width">50dp</item>
        <item name="android:layout_height">50dp</item>
        <item name="android:background">@drawable/buttonstyle</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">15sp</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_margin">10dp</item>
    </style>
</resources>

buttonstyle.xml(drawable)

底部按钮蓝色圆形背景图的设置

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="#2664aa"></solid>
</shape>

功能代码

SeasonActivity.java

        使用Java写页面背后的业务逻辑,重点在于事件响应机制,理解事件、事件源、事件监听器三者区别以及如何书写,原理与Java 中AWT或者Swing图形界面类似。

package com.example.app01;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class SeasonActivity extends AppCompatActivity {
    //定义组件类型的变量来接收事件源组件
    private TextView Spring;
    private TextView Summer;
    private TextView Autuom;
    private TextView Winter;
    private FrameLayout flSeason;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //指定当前的页面
        setContentView(R.layout.text3_season);
        initView();
    }
    //初始化获取组件
    private void initView(){
        SeasonClick sea = new SeasonClick();
        //通过id找到指定组件
        Spring=findViewById(R.id.spring);
        Summer=findViewById(R.id.summer);
        Autuom=findViewById(R.id.autuom);
        Winter=findViewById(R.id.winter);
        flSeason=findViewById(R.id.frameSeason);
        //设置事件监听器
        Spring.setOnClickListener(sea);
        Summer.setOnClickListener(sea);
        Autuom.setOnClickListener(sea);
        Winter.setOnClickListener(sea);
    }
    //内部类,实现事件监听
    class SeasonClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            //类型强转换
            TextView txt = (TextView) v;
            //获取图片的tag
            String tag = txt.getText().toString();
            //获取帧式布局中子元素的个数
            int count = flSeason.getChildCount();
            for (int i = 0; i < count; i++) {
                //通过下标获取到组件
                View vi = flSeason.getChildAt(i);
                //如果图片的tag与所点击的标签按钮一致,则图片显示,否则设置为隐藏
                if(vi.getTag().equals(tag)){
                    vi.setVisibility(View.VISIBLE);
                }else{
                    vi.setVisibility(View.GONE);
                }

            }
        }
    }

}

注意:由于安卓默认开机启动项是MainActivity.java文件,因此我们要在配置文件中修改为SeasonActivity才能显示本文件,如下图所示

其实这里就是在将每一个Activity进行配置,后续文章会重点讲解安卓中的Activity。


 运行效果

点击下方按钮,切换到对应季节的图片

春天
夏天

 

秋天

 

冬天

 

END.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/190659.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

私有云OpenStack学习笔记1

私有云&#xff1a;自己组建私有云系统。 私有云系统有哪些呢&#xff1f; OpenStack、CloudStack、Eucalyptus、vCloud Director四大主流云平台。 云计算有极大的潜力提高效率&#xff0c;显著地节约成本&#xff0c;实现可升级的基础设施和高性能以及安全的数据存储。 Ope…

秋招备战——Java基础知识

垃圾回收&#xff0c;JVM常用参数 将内存中不再被使用的对象进行回收&#xff0c;GC中用于回收的方法称为收集器&#xff0c;由于GC需要消耗一些资源和时间&#xff0c;Java在对对象的生命周期特征进行分析后&#xff0c;按照新生代、老年代的方式来对对象进行收集&#xff0c…

2023情人节送另一半什么好?四款适合送女友的数码好物分享

2023年情人节快到了&#xff0c;一个关于浪漫、惊喜并且充满爱意的节日&#xff0c;不少人都在烦恼送另一半什么好。而数码产品在日常生活中也是必不可少的存在&#xff0c;在此&#xff0c;我给大家整理了四款适合送女友的数码好物&#xff0c;一起来看看吧。 一、蓝牙耳机&a…

操作系统权限提升(四)之系统错误配置-Tusted Service Paths提权

系列文章 操作系统权限提升(一)之操作系统权限介绍 操作系统权限提升(二)之常见提权的环境介绍 操作系统权限提升(三)之Windows系统内核溢出漏洞提权 注&#xff1a;阅读本编文章前&#xff0c;请先阅读系列文章&#xff0c;以免造成看不懂的情况&#xff01;&#xff01;&am…

这18张 Python 数据科学速查表,让你的代码能力飞起来

数据科学是利用科学方法、流程、算法和系统从数据中提取价值的跨学科领域。数据科学家综合利用一系列技能&#xff08;包括统计学、计算机科学和业务知识&#xff09;来分析从网络、智能手机、客户、传感器和其他来源收集的数据。 目前在主流的数据科学领域一般有三大生态&…

深入详解 二次移动平均法python

什么是 二次移动平均法 二次移动平均法&#xff0c;也称为指数加权移动平均法&#xff0c;是一种用于平滑时间序列数据的算法。 文章目录二次移动平均法逻辑Python代码实现第二种实现二次移动平均法的方式第三种卷积实现二次移动平均法二次移动平均法的应用场景二次移动平均法逻…

Linux监控之prometheus学习

目录 需要学习以及掌握的知识 第一步&#xff1a;安装准备 1、安装nginx 2、安装prometheus 2_1、解压&#xff1a;prometheus-2.0.0.linux-amd64.tar.gz 2_2、移动到安装目录 2_3、将Prometheus配置为系统服务 2_4、重新加载系统文件&#xff0c;使prometheus.service文…

Linux字符设备、块设备的区别

一、字符设备 字符设备就是在对某设备进行操作时&#xff0c;该设备的读取以字节为单位进行。字符设备的操作是通过linux系统直接调用驱动程序完成的&#xff0c;在驱动程序的上一层并没用文件系统。因此字符设备的特点如下&#xff1a; 1、以字节流的方式进行读写、一个字节一…

亚马逊上线优惠券推荐功能,如何选品成为重中之重?

随着全球数字经济的进一步发展以及中国数字化基础设施的完善&#xff0c;众多中国品牌选择出海掘金道路。虽然全球经济格局的不确定因素在增多&#xff0c;但是总体上各国消费潜力在逐步释放&#xff0c;我国外贸行业也在不断迭代优化&#xff0c;尤其是出口跨境电商行业&#…

Python 基础语法介绍(二)

文章目录一、概述二、函数1&#xff09;函数定义2&#xff09;函数调用3&#xff09;函数传参1、形式参数和实际参数2、关键字参数3、参数默认值4、可变参数4&#xff09;函数返回值1、语法结构2、多值返回5&#xff09;变量作用域1、局部变量2、全局变量【1】在主程序中定义全…

金融风控12

社交网络分析与金融反欺诈 设备指纹 一般是基于某些设备信息&#xff0c;通过一些设备指纹算法将这些信息组合&#xff0c;通过特定hsah算法得到一个ID值&#xff0c;作为该设备唯一标识符 常见元素有&#xff1a; - sim卡信息 - wifi信息 - 硬盘信息 - 内存信息 - 屏幕…

【Qt】6.QTableWidget控件、其他控件、自定义控件封装、鼠标事件

目录 QTableWidget控件 代码 widget.cpp 结果 其他控件 stackWidget栈控件 下拉框 代码 widget.cpp 结果 自定义控件封装 代码 smallwidget.h smallwidget.cpp widget.cpp 结果 鼠标事件 代码 mylabel.h mylabel.cpp 结果 QTableWidget控件 设置列数setC…

oAuth2的入门

目录一、OAuth2流程演示示例第一步第二步二、流程2.1 资源所有者2.2 客户2.3 客户2.4 认证服务器2.5 客户2.6 资源服务器三、测试一、OAuth2流程演示示例 第一步 先到gitee下载oAuth2官方提供的代码&#xff0c;然后导入项目 https://gitee.com/lisenaq/oauth2-example.git第…

SpringMVC(十四):SpringMVC异常处理

文章目录 SpringMVC异常处理 一、异常简介 二、异常处理具体实现

Charles 的简单使用

1.下载并安装charles官方下载地址&#xff1a;https://www.charlesproxy.com/download/当前最新版本是4.6.3 选择合适的安装包进行下载&#xff08;windows.msi、macos.dmg、linux.tar.gz&#xff09;例windows.msi&#xff1a;执行msi文件&#xff0c;选择合适的下载地址&…

【机器学习】聚类算法(理论)

聚类算法&#xff08;理论&#xff09; 目录一、概论1、聚类算法的分类2、欧氏空间的引入二、K-Means算法1、算法思路2、算法总结三、DBSCAN算法1、相关概念2、算法思路3、算法总结四、实战部分...一、概论 聚类分析&#xff0c;即聚类&#xff08;Clustering&#xff09;&…

js继承的6种方式

// 1 原型链继承function Per() {this.name "key";}Per.prototype new Person(); // 主要var per1 new Per();console.log(per1.age);// instanceof 判断元素是否在其他元素的原型链上// per1继承了Person的属性&#xff0c;返回trueconsole.log(Per1 instanceof …

java常用类:BigInteger类和BigDecimal类

java常用类型: Ineteger等包装类 String类&#xff0c;StringBuffer类和StringBuilder类 Math类及常用方法 System类及常用方法 Arrays类及常用方法 BigInteger类和BigDecimal类及常用方法 日期类Date类,Calender类和LocalDateTime类 文章目录引言BigInteger (大整数)常用方法B…

二分算法学习

&#x1f33c; 扎着马尾的姑娘&#xff0c;笑容温柔很善良自在的少年 - 要不要买菜 - 单曲 - 网易云音乐 前言 本来打算做蓝桥杯2022&#xff23;&#xff0b;&#xff0b;A组省赛F题青蛙过河的,看到标签显示"二分",第一时间竟然想不到二分是什么,所以来学习下 目录…

传闻将与马云合作,涨了7倍的正大企业国际,还能跟风吗?

1月30日周一&#xff0c;港股正大企业国际逆势大涨68.35%&#xff0c;1月31日正大企业国际继续飙升&#xff0c;最高点涨超275%&#xff0c;收盘时涨幅达251.88%&#xff0c;成为2023年第一只翻倍的股票。今日早盘继续近40个点&#xff0c;还在持续发酵中。 消息面上&#xff…