android - 在 Android Studio(Kotlin)中单击按钮时更改 TextView 文本
问题描述
我最近开始学习 Kotlin,在课程中,我研究了我们在 Android 中创建了一个应用程序,它在按下按钮时会更改图像,问题是我如何创建相同的应用程序但在按钮时更改 TextView 的文本按下?
以下是适用于图像的应用程序的代码。
class MainActivity : AppCompatActivity() {
var Images = intArrayOf(R.drawable.sophh, R.drawable.soph2, R.drawable.soph3)
var index = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun ChangeImage(view: View) {
var image_view = findViewById(R.id.image_view1) as ImageView
image_view.setImageResource(Images[index])
index++
if (index == Images.size) {
index = 0
}
}
}
解决方案
您应该在其中设置文本的setOnClickListener
地方添加一个:Button
TextView
button.setOnClickListener {
text_view.text = "Your text"
}
推荐阅读
- function - 使用功能 swiftUI 将视图添加到 contentView 不起作用
- multipartform-data - 如何解释缺少 CRLF 的多部分?
- azure - 将 Function App 连接到特定设备?
- css - 下拉菜单高度动画
- javascript - 如何使用 NextJS 和 useContext 挂钩跨页面保持状态?
- list - 有人可以用我的 Haskell 代码诊断问题吗?
- pg-promise - 在 pg-promise 查询中返回未定义的空值
- multithreading - 在阿姆达尔定律中估计 P
- java - 用asm如何声明一个非泛型类实现泛型接口
- c# - WPF绑定到静态资源不起作用