android studio实现按钮控制指示灯点亮熄灭

android studio实现按钮控制指示灯点亮熄灭

采用两个普通按键Button,控制一个由ImageView虚拟的指示灯,按第一个按键灯点亮变成绿色,按第二个按键灯变成灰色。非常简单,直接分享一下。

1、AndroidManifest.xml代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp01">

    <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/Theme.MyApp01">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

2、MainActivity.java代码

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView L1;
    Button bnt1;
    Button bnt2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bnt1 = findViewById(R.id.button1);//  按钮灯开
        bnt2 = findViewById(R.id.button2);//  按钮灯关
        bnt1.setOnClickListener(this);
        bnt2.setOnClickListener(this);
        L1 = (ImageView)this.findViewById(R.id.Lamp);
    }
    
    @Override
    public void onClick(View view) {
        int i=view.getId();
        if (i==bnt1.getId())
            L1.setVisibility(View.VISIBLE);

        if (i==bnt2.getId())
            L1.setVisibility(View.INVISIBLE) ;

    }
}

通过检测两个按钮bnt1及bnt2状态,使指示灯L1在可见与不可见之间切换完成灯的亮灭指示,bnt1点击时L1切换为可见,显示是绿色,bnt2点击时L1切换为不可见,显示灰色,为实现显示灰色,这里是在L1对应的ImageView(Lamp)下放置了一个同样大小灰色的ImageView(ImageView2)。

3、activity_main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="34dp"
        tools:layout_editor_absoluteY="0dp">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="109dp"
            android:layout_marginEnd="50dp"
            android:text="点亮"
            app:layout_constraintEnd_toEndOf="@+id/imageView2"
            app:layout_constraintTop_toBottomOf="@+id/Lamp" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="50dp"
            android:layout_marginTop="109dp"
            android:text="熄灭"
            app:layout_constraintStart_toStartOf="@+id/imageView2"
            app:layout_constraintTop_toBottomOf="@+id/imageView2" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_marginTop="180dp"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:drawable/presence_invisible" />

        <ImageView
            android:id="@+id/Lamp"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_marginTop="180dp"
            android:visibility="invisible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:drawable/presence_online" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

4、最后效果如下

在这里插入图片描述

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
1.物联网的开发难度越来越低当小王被老板要求开发一个物联网项目时,他面临这许多种解决方案,从硬件到软件一应俱全。有的提供了模块化的硬件,有的提供了高度集成的软件开发环境,有的支持python开发,有的支持图形化开发,有的提供硬件SDK,有的提供从云端到移动端成套的解决方案。小王该如何选择?的确,物联网的开发难度越来越低,只要你想,就可以拖拖拽拽做出来一个简单的设备。移动端APP呢?也是如此,你甚至可以登陆某平台的网站在线生成一个安装包直接安装到手机上。但是这些便利化的前提是失去对核心技术的掌握,以及跳转界面、使用习惯等无法把控。2.移动端APP技能是必备当学习物联网技术的小赵到某公司应聘时,公司技术主管对他的知识结构基本满意。然后让他开发一个物联网项目,要软硬件结合,移动端APP是必备的。小赵可以很快做出来一台基于ESP8266的硬件设备,连接到某物联网平台,做出来一个手机端的APP。但是主管对这个APP不太满意,问小赵自己会不会制作手机APP?小赵有些尴尬。为什么移动端APP技能是必备?因为物联网技术涉及到的知识领域有很多个,能够自己做出APP才算是将这些领域的知识完全掌握。就像是一个侠客的武功,最厉害的那个还没有练成,还不能算是真正的高手。3.定制APP价格很贵市场上有不少的物联网软件公司开展物联网应用APP的定制业务,当然价格不菲。如果是公司采购,你将花费较大的价格采购到一个未来并不可控的APP。反过来说,如果是学生应聘,你学到移动端应用知识,将大大提升你的竞争力,你的技能将成为核心竞争力。4.没有类似的教程原因就不多说了。即使有一些片段代码给了大家,大家也做不出来一个能用的APP。5.本课程要讲什么基于阿里云物联网平台(其他物联网平台类似),带您使用Android Studio编写一个安卓APP,实现连接阿里云物联网平台,控制一台智能灯。通过一个软硬件结合项目,结合源代码,一点一点为大家讲解怎么样连接阿里云物联网平台,每一个步骤是怎么回事,源代码怎么实现。为了照顾一些零基础的朋友,课程中还会用一些通俗的语言介绍这些知识,让大家掌握书上和网络上一些不太注意的技术细节。  
实现按钮控制布局的折叠,可以使用 Android 中的 `ConstraintLayout` 和 `ConstraintSet`。 首先,在 XML 布局文件中,将需要折叠的布局放在一个 `ConstraintLayout` 中,并添加一个按钮,用于控制折叠。例如: ```xml <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/collapsedLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@id/someView"> <!-- 折叠的布局 --> <TextView android:id="@+id/collapsedTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:text="Collapsed Text" android:textColor="@android:color/white" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <!-- 控制折叠的按钮 --> <Button android:id="@+id/expandButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Expand" app:layout_constraintTop_toBottomOf="@id/collapsedTextView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout> ``` 接下来,在 Java/Kotlin 代码中,将需要折叠的布局和按钮分别用 `findViewById` 获取,并创建一个 `ConstraintSet` 对象,用于设置折叠和展开后的布局约束。例如: ```kotlin val collapsedLayout = findViewById<ConstraintLayout>(R.id.collapsedLayout) val collapsedTextView = findViewById<TextView>(R.id.collapsedTextView) val expandButton = findViewById<Button>(R.id.expandButton) // 创建 ConstraintSet 对象 val constraintSet = ConstraintSet() // 设置折叠后的布局约束 constraintSet.clone(collapsedLayout) constraintSet.constrainHeight(R.id.collapsedTextView, 0) // 设置展开后的布局约束 val expandedHeight = resources.getDimensionPixelSize(R.dimen.expanded_height) constraintSet.constrainHeight(R.id.collapsedTextView, expandedHeight) // 点击按钮控制折叠和展开 expandButton.setOnClickListener { if (collapsedTextView.height == 0) { // 折叠状态,展开 constraintSet.applyTo(collapsedLayout) expandButton.text = "Collapse" } else { // 展开状态,折叠 constraintSet.applyTo(collapsedLayout) expandButton.text = "Expand" } } ``` 这样,点击按钮时即可控制布局的折叠和展开。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wdzh018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值