【问题标题】:setOnClickListener not working android kotlinsetOnClickListener 不工作 android kotlin
【发布时间】:2020-04-20 00:21:55
【问题描述】:

我正在学习 android 和 kotlin,并且正在构建一个练习应用程序。当我运行我的代码时,按钮不起作用,我不知道为什么或我做错了什么/忘记添加。任何帮助表示赞赏!

我的xml代码:

 <Button
    android:id="@+id/solveBtn"
    android:layout_width="113dp"
    android:layout_height="65dp"
    android:layout_marginBottom="184dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Solve" 
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/NumInput" />

和我的 kt 代码: 类 MainActivity : AppCompatActivity() {

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

    var num = NumInput.inputType.toInt()
    var answer = ansText

    fun solver (num1 : Int) {
        var div = 1
        var count = 0
        if ((num1 > 0) && (num1 < 2)){
            println("$num1 is a prime number!")
        } else{
            while (div < num1){
                if ((num1 % div) == 0){
                    div++
                    count++
                }else {
                    div++
                }
            }
        }
        if (count == 0) {
            answer.text = "$num1 is not a prime number"
        } else {
            answer.text = "$num1 is a prime number"
        }

    solveBtn.setOnClickListener{
        println("Hello")
        answer.clearComposingText()
        solver(num)
        }
    }
}

}

再次感谢您的帮助!

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    您的 solveBtn.setOnClickListener 位于永远不会调用的 solver 函数中。只需将setOnClickListener 放在下方的一个花括号即可。

    【讨论】:

    • 虽然现在看来我的输出数字总是8194,不管输入什么数字所以我不知道那里发生了什么。
    • 那是因为您不是在阅读文本,而是在阅读inputType,这是一个常数。 var num = NumInput.inputType.toInt()
    • 如何获取文本并将其设为 Int?
    • Integer.valueOf(str)。您可能想在单独的帖子中提出其他问题,而不是在 cmets 部分。
    【解决方案2】:

    你需要在solver(..)之外设置setOnClickListener

    fun solver (num1 : Int) {
        var div = 1
        var count = 0
        if ((num1 > 0) && (num1 < 2)){
            println("$num1 is a prime number!")
        } else{
            while (div < num1){
                if ((num1 % div) == 0){
                    div++
                    count++
                }else {
                    div++
                }
            }
        }
        if (count == 0) {
            answer.text = "$num1 is not a prime number"
        } else {
            answer.text = "$num1 is a prime number"
        }
    }
    
    solveBtn.setOnClickListener{
        println("Hello")
        answer.clearComposingText()
        solver(num)
        }
    }
    

    【讨论】:

    • 谢谢,我很困惑,但我没有听懂。我修复了按钮,但现在我总是得到 8194 作为 $num1,我不知道为什么。如果你碰巧我想知道,再次感谢你。
    【解决方案3】:

    请这样使用:

     backArrow.setOnClickListener {
                finish()
            }
    

    【讨论】:

      【解决方案4】:

      检查XML,有时在使用contraintLayout时,android:enabled="false"默认设置为false,设置为true或删除它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        • 1970-01-01
        • 2021-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-02
        相关资源
        最近更新 更多