【问题标题】:ImageView not appearing when button clicked单击按钮时没有出现 ImageView
【发布时间】:2020-11-29 22:08:13
【问题描述】:

我创建了一个小型练习应用程序,当单击按钮下一步时,它会更改屏幕上的图像。但是,图像和按钮都不做任何事情。对于 onClickListener 设置,我的 MainActivity 中的所有内容看起来都很正常,我按照各种指南创建了一个有效的布局。但是,我不确定为什么它看起来如此奇怪并且在模拟器中不起作用。

这是我目前编写的 MainActivity 代码:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setListener()
}

private fun changeImage(view: View) {
    when (view.id) {
        R.id.goodMorning -> view.setBackgroundResource(R.drawable.gm1)
        R.id.goodMorning -> view.setBackgroundResource(R.drawable.gm2)

        else -> view.setBackgroundColor(Color.LTGRAY)
    }
}

private fun setListener() {
    val goodMorning = findViewById<ImageView>(R.id.goodMorning)
    val buttonNext = findViewById<TextView>(R.id.buttonNext)

    fun setListener(){

        val clickableViews: List<View> =
            listOf(goodMorning, buttonNext)

        for (item in clickableViews){
            item.setOnClickListener() { changeImage(it) }
        }
    }
}

}

这是布局xml:

<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
    android:id="@+id/goodMorning"
    android:layout_width="404dp"
    android:layout_height="562dp"
    android:contentDescription="@android:string/no"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.428"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.365"
    tools:srcCompat="@drawable/gm1" />

<Button
    android:id="@+id/buttonNext"
    style="@style/twoButtons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:text="@string/buttonNext"
    android:textSize="@dimen/box_text_size"
    app:layout_constraintBaseline_toBaselineOf="@+id/buttonBack"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/buttonBack" />

<Button
    android:id="@+id/buttonDownload"
    style="@style/twoButtons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:text="@string/buttonDownload"
    android:textSize="@dimen/box_text_size"
    app:layout_constraintBaseline_toBaselineOf="@+id/buttonBack"
    app:layout_constraintEnd_toStartOf="@+id/buttonBack"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/buttonBack"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    android:text="@string/buttonBack"
    android:textColor="@android:color/black"
    android:textSize="@dimen/box_text_size"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/goodMorning"
    app:layout_constraintVertical_bias="0.718" />

这是该应用在 Pixel 2 中的外观:

非常感谢任何帮助!

【问题讨论】:

  • 您在外部fun setListener() 中定义了fun setListener()。内部的永远不会被调用,因此它的代码不会被执行。将内部函数内的代码提升到外部代码并删除内部函数。这样做,你就会看到进步。
  • 我明白了。但是,这两张图片中只出现了一张。

标签: android kotlin imageview onclicklistener android-constraintlayout


【解决方案1】:

像这样修改代码

private fun changeImage(view: View) {
        val goodMorning = findViewById<ImageView>(R.id.goodMorning)
        
        when (view.id) {
            R.id.buttonBack -> view.setBackgroundResource(R.drawable.gm1)
            R.id.buttonNext -> view.setBackgroundResource(R.drawable.gm2)

            else -> goodMorning.setBackgroundColor(Color.LTGRAY)
        }
    }

    private fun setListener() {
        val buttonNext = findViewById<TextView>(R.id.buttonNext)
        val buttonBack = findViewById<TextView>(R.id.buttonBack)

        val clickableViews: List<View> =
            listOf(buttonBack, buttonNext)

        for (item in clickableViews){
            item.setOnClickListener() { changeImage(it) }
        }
    }

【讨论】:

  • 我试过了,但它不起作用 - 现在没有图像出现。
【解决方案2】:

我找到了解决问题的方法:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val nextButton: Button = findViewById(R.id.buttonNext)
    nextButton.setOnClickListener() { changeImage() }

}

private fun changeImage() {

    // Shift through all available images
    val imageOptions: ImageView = findViewById(R.id.morningImages)

    val drawableResources = when (imageOptions) {
        buttonNext -> R.drawable.gm1
        else -> R.drawable.gm2
    }

    imageOptions.setImageResource(drawableResources)
        // Repeat function needed
    }
}

您最好在开始时使用 onCreate 创建 clickListener,因为您不必创建另一个私有函数,这会使这个问题比实际情况更令人困惑。

在 changeImage 函数中创建一个新变量以查找 ImageView。之后,调用另一个变量来创建一个 when 块来切换可用的图像,并用 else 语句结束它以避免产生错误。

将 imageResource 设置为 drawableResources,以便图像在请求时显示在屏幕上。

注意:用户浏览完所有图像后,需要重复功能才能再次切换图像。

【讨论】:

    猜你喜欢
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多