Android Studio - 在按钮回调中设置 ImageView 的可见性不起作用

问题描述 投票:0回答:1

首先 - 我对应用程序编码、kotlin、xml 等非常陌生。(上周开始自学)。 话虽如此:我正在尝试编写一个显示图像的活动,用户需要按下相应的按钮。 (具体来说,该图像是一个 G 谱号音符,按钮只是希伯来语的音符名称列表)。
按下按钮后,我收集“正确与否”的统计数据,并显示“v”或“x”图像,以便用户知道他们是否正确,然后生成一个新图像,直到我循环完所有图像要学习的图像列表中的图像。
我设置它的方式是,当生成新图像时,我重新定义按钮回调,以便正确的按钮显示“v”(并对正确答案统计数据执行+1,然后继续下一张图片),所有其余按钮显示“x”(并对不正确的统计数据执行 +1,并且不要继续移动到下一张图片)。

我面临的问题是一切正常

除了显示 v/x 图像。 我检查过,统计数据收集正确。添加短暂的“睡眠”并没有帮助。 我做错了什么??

这是其价值的代码。 我用注释标记了没有做我认为应该做的事情的两行,并用“THIS IS NOT SHOWING”字样标记。

顺便说一句,我确信这段代码需要进行大量改进,但作为初学者,我希望专注于手头的问题,而不是我的代码、风格等中过多的一般问题:-)

package com.example.test import android.os.Bundle import android.view.View import android.widget.Button import android.widget.HorizontalScrollView import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat class GuessNotesGame : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContentView(R.layout.activity_guess_notes_game) ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) val buttonschoicecontainer = findViewById<HorizontalScrollView>(R.id.scrollviewer) val linearbuttons = findViewById<LinearLayout>(R.id.linearbuttons) val correctim = findViewById<ImageView>(R.id.correctim) val wrongim = findViewById<ImageView>(R.id.wrongim) correctim.visibility = View.INVISIBLE wrongim.visibility = View.INVISIBLE buttonschoicecontainer.visibility = View.INVISIBLE; linearbuttons.visibility = View.VISIBLE; val listofnotes = listOf("c1", "d1", "e1", "f1", "g1", "a1", "b1", "c2") for (note in listofnotes) { val id: Int = resources.getIdentifier("choose" + note, "id", this.packageName) val btn_by_id = findViewById<Button>(id); btn_by_id.visibility = View.INVISIBLE;} val notesPlaceHolder = findViewById<ImageView>(R.id.notesplaceholder) val notesList = listOf( R.drawable.c1, R.drawable.d1, R.drawable.e1, R.drawable.f1, R.drawable.g1, R.drawable.a1, R.drawable.b1, R.drawable.c2) val notesNamesList = listOf("דו", "רה", "מי", "פה", "סול", "לה", "סי", "דו גבוה") // notesnameslist.shuffled() var numCorrect = 0 var curIndex = 0 var numIncorrect = 0 fun runNextNote(ind: Int) { correctim.visibility = View.INVISIBLE wrongim.visibility = View.INVISIBLE val curCorrectNote = notesList[ind] val correctNoteName = notesNamesList[ind] notesPlaceHolder.setImageResource(curCorrectNote); buttonschoicecontainer.visibility = View.VISIBLE; linearbuttons.visibility = View.VISIBLE; for (note in listofnotes) { val id: Int = resources.getIdentifier("choose" + note, "id", this.packageName) val btn_by_id = findViewById<Button>(id); btn_by_id.visibility = View.VISIBLE; btn_by_id.setOnClickListener({ if (btn_by_id.text.toString() == correctNoteName) { correctim.visibility = View.VISIBLE; // THIS IS NOT SHOWING Thread.sleep(1_000) curIndex = curIndex + 1 numCorrect = numCorrect + 1 } else { wrongim.visibility = View.VISIBLE; // THIS IS NOT SHOWING Thread.sleep(1_000) numIncorrect = numIncorrect + 1; } // Thread.sleep(2_000) if (ind < notesNamesList.size - 1) {runNextNote(curIndex)} else { correctim.visibility = View.INVISIBLE wrongim.visibility = View.INVISIBLE val textViewResults = findViewById<TextView>(R.id.textViewResults); textViewResults.visibility = View.VISIBLE; notesPlaceHolder.setImageResource(R.drawable.gcleftreb) textViewResults.text = "Correct: $numCorrect, Wrong: $numIncorrect"; } } );} } runNextNote(0) insets } } }
根据请求,以下是所述活动的 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".GuessNotesGame"> <ImageView android:id="@+id/notesplaceholder" android:layout_width="253dp" android:layout_height="306dp" android:layout_marginStart="128dp" android:layout_marginEnd="128dp" android:layout_marginBottom="165dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:srcCompat="@drawable/introim" /> <HorizontalScrollView android:id="@+id/scrollviewer" android:layout_width="368dp" android:layout_height="101dp" android:layout_marginStart="128dp" android:layout_marginEnd="128dp" android:layout_marginBottom="29dp" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" tools:visibility="invisible"> <LinearLayout android:id="@+id/linearbuttons" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" tools:visibility="invisible"> <Button android:id="@+id/choosec1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:text="@string/c1" android:textSize="26sp" tools:visibility="invisible"/> <Button android:id="@+id/choosed1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:text="@string/d1" android:textSize="26sp" tools:visibility="invisible" /> <Button android:id="@+id/choosee1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:text="@string/e1" android:textSize="26sp" tools:visibility="invisible"/> <Button android:id="@+id/choosef1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:text="@string/f1" android:textSize="26sp" tools:visibility="invisible"/> <Button android:id="@+id/chooseg1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:text="@string/g1" android:textSize="26sp" tools:visibility="invisible"/> <Button android:id="@+id/choosea1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:text="@string/a1" android:textSize="26sp" tools:visibility="invisible"/> <Button android:id="@+id/chooseb1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:text="@string/b1" android:textSize="26sp" tools:visibility="invisible"/> <Button android:id="@+id/choosec2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:text="@string/c2" android:textSize="20sp" tools:visibility="invisible"/> </LinearLayout> </HorizontalScrollView> <ImageView android:id="@+id/wrongim" android:layout_width="85dp" android:layout_height="95dp" android:layout_marginStart="128dp" android:layout_marginTop="20dp" android:layout_marginEnd="128dp" android:layout_marginBottom="69dp" app:layout_constraintBottom_toTopOf="@+id/notesplaceholder" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textViewResults" app:srcCompat="@drawable/wrong" tools:visibility="gone" /> <ImageView android:id="@+id/correctim" android:layout_width="85dp" android:layout_height="95dp" android:layout_marginStart="128dp" android:layout_marginTop="20dp" android:layout_marginEnd="128dp" android:layout_marginBottom="69dp" app:layout_constraintBottom_toTopOf="@+id/notesplaceholder" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textViewResults" app:srcCompat="@drawable/correct" tools:visibility="gone" /> <TextView android:id="@+id/textViewResults" android:layout_width="203dp" android:layout_height="35dp" android:layout_marginStart="106dp" android:layout_marginTop="42dp" android:layout_marginEnd="102dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:visibility="invisible" /> </androidx.constraintlayout.widget.ConstraintLayout>
    
kotlin android-studio
1个回答
0
投票
基于您的代码。有些看起来很糟糕,因为你构建的概念是错误的。因此,我创建了一些注释和修订。 要创建列表,您可以实现recyclerview,而不是一一创建按钮。 Recyclerview 可以让您的组件列表在任何情况下都更加强大和灵活。

您可以查看我的修复来解决您的问题。

https://gist.github.com/sadewa25/1b9cf885bb24bbf7a2f2de9333308c4b

在该代码中,您可以根据该数据获取所有 onclick 项目。你可以改变你的逻辑。 :)

rvMain.adapter = RvAdapter(modelData){ data -> correctim.isVisible = !correctim.isVisible Toast.makeText(this, "${data.id} -- ${data.item}", Toast.LENGTH_SHORT).show() }
这是我的示例屏幕截图,但我使用虚拟图像来完成。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.