【问题标题】:Change TextView text on button click in Android Studio ( Kotlin)在 Android Studio(Kotlin)中单击按钮时更改 TextView 文本
【发布时间】:2020-12-03 01:06:13
【问题描述】:

我最近开始学习 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
        }
    }
}

【问题讨论】:

  • 只需将myTextView.setText("Some text...") 放在按钮点击的回调中

标签: android kotlin


【解决方案1】:

var text = intArrayOf("One","Two,"Three")
text_view.text=text[索引]

【讨论】:

    【解决方案2】:

    在 kotlin 中

    textview.text= "My text"
    

    在 Java 中

    textview.setText("My text");
    

    【讨论】:

      【解决方案3】:

      您应该将setOnClickListener 添加到您的Button 中,并在其中设置TextView 的文本:

      button.setOnClickListener {
          text_view.text = "Your text"
      }
      

      【讨论】:

        猜你喜欢
        • 2012-06-03
        • 1970-01-01
        • 1970-01-01
        • 2021-07-07
        • 2022-01-12
        • 2019-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多