慢慢学习Kotlin。只是从滚动中生成一个随机数。如果roll = 9,我想使按钮和搜寻栏不可见。

我正在使用toggleVisibility函数来完成此操作,但Kotlin编译器将isVisible视为未解决的引用

package com.example.randomizer

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.SeekBar
import android.widget.TextView
import android.widget.VideoView
import java.util.*

class MainActivity : AppCompatActivity() {

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

        val rollButton = findViewById<Button>(R.id.rollButton)
        val resultsTextView = findViewById<TextView>(R.id.resultsTextView)
        val seekBar = findViewById<SeekBar>(R.id.seekBar)
        val winText = "9 You Win !"




        rollButton.setOnClickListener {
            val rand = Random().nextInt(seekBar.progress)
            resultsTextView.text = rand.toString()
            if (rand == 9) {
                resultsTextView.text = winText
                seekBar.toggleVisibility()
                rollButton.toggleVisibility()
            }

        }



    }

    fun View.toggleVisibility() {
        if (this.isVisible()) {
            this.visibility = View.INVISIBLE
        } else {
            this.visibility = View.VISIBLE
        }
    }
}

编译器错误:
unresolved reference isVisible

最佳答案

您是否自行定义了View的isVisible
View类没有名为isVisible()的方法。

正如@ user2340612所说,它可以定义为:

fun View.isVisible(): Boolean {
    return this.visibility == View.VISIBLE
}

09-26 03:26