复选框的应用
近日学习过程中,老师布置的一道实验题
请选择合适的组件,添加点击事件实现如下效果。
点击灯泡图像,灯泡可在亮暗之间切换
选择灯泡右侧单选按钮,也可控制灯泡亮暗
勾选信号灯右侧复选框,可控制相应信号灯的点亮或关闭
最终实验效果如图:
我的代码如下:
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 &