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