Android中小灯泡与信号灯的点亮

本文介绍了如何在Android应用中利用复选框和点击事件控制灯泡和信号灯的亮暗切换。通过点击灯泡图像和选择单选按钮,可以实现灯泡的亮暗控制;勾选复选框则能控制信号灯的开关状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

复选框的应用

近日学习过程中,老师布置的一道实验题

请选择合适的组件,添加点击事件实现如下效果。

点击灯泡图像,灯泡可在亮暗之间切换

选择灯泡右侧单选按钮,也可控制灯泡亮暗

勾选信号灯右侧复选框,可控制相应信号灯的点亮或关闭

最终实验效果如图:

 

我的代码如下:

MainActivity:

package com.example.experienct2to1;  
import androidx.appcompat.app.AppCompatActivity;  
import android.annotation.SuppressLint;  
import android.os.Bundle;  
import android.widget.CheckBox;  
import android.widget.CompoundButton;  
import android.widget.ImageView;  
import android.widget.RadioButton;  
import android.widget.RadioGroup;  
public class MainActivity extends AppCompatActivity  
{  
    private ImageView imageView;  
    private int currentImage = 1;  
    RadioButton Rb1, Rb2;  
    RadioGroup Rg;@  
    SuppressLint("MissingInflatedId")@ Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        CheckBox checkBox1 = findViewById(R.id.checkBox1);  
        ImageView imageView1 = findViewById(R.id.imageView1);  
        CheckBox checkBox2 = findViewById(R.id.checkBox2);  
        ImageView imageView2 = findViewById(R.id.imageView2);  
        CheckBox checkBox3 = findViewById(R.id.checkBox3);  
        ImageView imageView3 = findViewById(R.id.imageView3);  
        CheckBox checkBox4 = findViewById(R.id.checkBox4);  
        ImageView imageView4 = findViewById(R.id.imageView4);  
        imageView = findViewById(R.id.imageView);  
        Rg = findViewById(R.id.Rg);  
        Rb1 = findViewById(R.id.Rb1);  
        Rb2 = findViewById(R.id.Rb2);  
        Rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()  
        {@  
            Override  
            public void onCheckedChanged(RadioGroup radioGroup, int i)  
            {  
                if(Rb1.isChecked())  
                {  
                    imageView.setImageResource(R.drawable.image1);  
                }  
                else  
                {  
                    imageView.setImageResource(R.drawable.image2);  
                }  
            }  
        });  
        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()  
        {@  
            Override  
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)  
            {  
                imageView1.setImageResource(isChecked ? R.drawable.red2 : R.drawable.red1);  
            }  
        });  
        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()  
        {@  
            Override  
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)  
            {  
                imageView2.setImageResource(isChecked ? R.drawable.blue2 : R.drawable.blue1);  
            }  
        });  
        checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()  
        {@  
            Override  
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)  
            {  
                imageView3.setImageResource(isChecked ? R.drawable.green2 : R.drawable.green1);  
            }  
        });  
        checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()  
        {@  
            Override  
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)  
            {  
                imageView4.setImageResource(isChecked ? R.drawable.yellow2 : R.drawable.yellow1);  
            }  
        });  
    }  
} 

Xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="com.example.experienct2to1.MainActivity"  
    android:orientation="vertical">  
  
    <LinearLayout  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:orientation="horizontal">  
        <ImageView  
            android:id="@+id/imageView"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:src="@drawable/image1" />  
  
        <RadioGroup  
            android:id="@+id/Rg"  
            android:layout_marginLeft="50dp"  
            android:layout_marginTop="50dp"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:orientation="vertical">  
  
            <RadioButton  
                android:checked="true"  
                android:id="@+id/Rb1"  
                android:text="开灯"  
                android:textSize="20dp"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content">  
            </RadioButton>  
  
            <RadioButton  
                android:id="@+id/Rb2"  
                android:text="关灯"  
                android:textSize="20dp"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content">  
            </RadioButton>  
  
        </RadioGroup>  
    </LinearLayout>  
    <LinearLayout  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="40dp"  
        android:layout_marginLeft="40dp"  
        android:orientation="horizontal">  
        <ImageView  
            android:id="@+id/imageView1"  
            android:layout_width="100dp"  
            android:layout_height="100dp"  
            android:layout_below="@id/checkBox1"  
            android:src="@drawable/red1" />  
  
        <CheckBox  
            android:id="@+id/checkBox1"  
            android:layout_marginLeft="140dp"  
            android:layout_marginTop="30dp"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="Red" />  
    </LinearLayout>  
  
  
    <LinearLayout  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="30dp"  
        android:layout_marginLeft="40dp"  
        android:orientation="horizontal">  
        <ImageView  
            android:id="@+id/imageView2"  
            android:layout_width="100dp"  
            android:layout_height="100dp"  
            android:layout_below="@id/checkBox2"  
            android:src="@drawable/blue1" />  
  
        <CheckBox  
            android:id="@+id/checkBox2"  
            android:layout_marginLeft="140dp"  
            android:layout_marginTop="30dp"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="Blue" />  
    </LinearLayout>  
  
  
    <LinearLayout  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="30dp"  
        android:layout_marginLeft="40dp"  
        android:orientation="horizontal">  
        <ImageView  
            android:id="@+id/imageView3"  
            android:layout_width="100dp"  
            android:layout_height="100dp"  
            android:layout_below="@id/checkBox3"  
            android:src="@drawable/green1" />  
  
        <CheckBox  
            android:id="@+id/checkBox3"  
            android:layout_marginLeft="140dp"  
            android:layout_marginTop="30dp"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="Green" />  
    </LinearLayout>  
  
  
    <LinearLayout  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="30dp"  
        android:layout_marginLeft="40dp"  
        android:orientation="horizontal">  
        <ImageView  
            android:id="@+id/imageView4"  
            android:layout_width="100dp"  
            android:layout_height="100dp"  
            android:layout_below="@id/checkBox4"  
            android:src="@drawable/yellow1" />  
  
        <CheckBox  
            android:id="@+id/checkBox4"  
            android:layout_marginLeft="140dp"  
            android:layout_marginTop="30dp"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="Yellow" />  
    </LinearLayout>  
  
  
  
</LinearLayout> 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值