我在 LinearLayout 中有多个按钮,它们都有不同的点击选项。我想当我选择其中一个时,它会被选中并更改其背景,然后我选择另一个第一个选中的消失,然后选择新的一个回合,背景会改变。我有 8-9 个按钮或更多。我怎样才能做到这一点?
这是 action_main.xml
<HorizontalScrollView
android:id="@+id/horScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<ImageButton
android:id="@+id/spaceship"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/spaceship" />
<ImageButton
android:id="@+id/mask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
app:srcCompat="@drawable/mask" />
</LinearLayout>
</HorizontalScrollView>
这是 Main.java
ImageButton spaces = findViewById(R.id.spaceship);
spaces.setBackgroundColor(Color.TRANSPARENT);
spaces.setOnClickListener(view -> {
if(getChecked())
spaces.setBackgroundColor(Color.TRANSPARENT);
else
spaces.setBackgroundColor(Color.DKGRAY);
setChecked(!check);
modelLoader.loadModel(this, R.raw.sample_ship);
setRenderable(spaceShipRendeble);
首先,我设置检查变量“false”。
例如,一个按钮被选中,它变成灰色,而不是我选择另一个按钮变成灰色,第一次选择将变成透明。但我做不到。
牛魔王的故事
撒科打诨
哈士奇WWW
相关分类