【问题标题】:How to get Button text using onclicklistner in Android Kotlin? [duplicate]如何在 Android Kotlin 中使用 onclicklistener 获取按钮文本? [复制]
【发布时间】:2020-12-20 19:35:34
【问题描述】:

我是 Android 的初学者。我正在制作一个笑话 api 应用程序,每个按钮都是类别。所以我需要获取按钮的文本内容。这是我的 xml 代码。我查看了很多线程,但都在 java 中

 <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/button2"
    android:layout_marginTop="20dp"
    android:text="science"
    android:onClick="getJoke"
    />

【问题讨论】:

    标签: android kotlin


    【解决方案1】:
    fun getJoke(v:View) {
      val b = v as Button
      val buttonText = b.getText().toString()
    }
    

    【讨论】:

    • 谢谢!假设如果没有人在这里回答我的问题,我应该去哪里找到我的答案,我试图在 android 文档中搜索但找不到。
    • google 你的问题,当你找到 java 的答案时,有在线转换器工具将 java 转换为 kotlin
    • @PatriciaHeimfarth 你甚至不需要为此上网。 stackoverflow.com/a/39602657
    • 请在您的代码答案中添加解释。
    猜你喜欢
    • 1970-01-01
    • 2011-05-05
    • 2015-08-27
    • 2019-11-07
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    相关资源
    最近更新 更多