android - 如何在 Android Kotlin 中使用 onclicklistner 获取按钮文本?
问题描述
我是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"
/>
解决方案
fun getJoke(v:View) {
val b = v as Button
val buttonText = b.getText().toString()
}
推荐阅读
- angular - 将角料片添加到输入框
- c# - 反序列化从 Azure 表中检索到的 DynamicTableEntity
- java - 使java输出特定数量的字符,具体取决于控制台中的输入
- vba - 如何从 VBA 中的 WSDL GET 函数返回特定数据
- vba - 自动化错误 - 在 Excel 中打开 VB 编辑器时发生灾难性故障
- c# - Xamarin Android:设置 ExpandableListView.SetOnGroupExpandListener
- angular - WebApi 和 Angular 2+ - 身份验证 - 令牌过期 - 返回登录
- safari - IE11:解析握手响应时出错:TypeError:对象不支持属性或方法“indexOf”
- workflow - 将 ServiceNow 工作流转换为 Visio
- javascript - 这个符号有什么作用?