【问题标题】:How can I fix this Android button onClick problem?如何解决此 Android 按钮 onClick 问题?
【发布时间】:2021-07-04 20:07:54
【问题描述】:

我尝试在 Android Studio 中编写一个简单的按钮,以便在单击时显示一些简单的消息,但一切都是徒劳的。以下是我的代码摘录,请帮助我了解我可能会出错的地方。谢谢!

package com.example.clinicappointment

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.view.Display
import android.view.View
import android.widget.Button
import android.widget.TextView

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

    fun onTap(view: View) {
        textView3.text="My Summary to be displayed here!"
    }
}

【问题讨论】:

  • 这是我的按钮设计
  • 知情问题是可编辑的

标签: java android algorithm kotlin android-layout


【解决方案1】:

@Geofrey Wasonga xml 中的 textView 在哪里,您希望在单击此按钮时设置文本? 如果它在您的实际 xml 中,那么您需要获取对 textview 的引用 私人 val textView3:TextView? =空 在 onCreate -> textView3 = findViewById(R.id.textViewID) 然后您的按钮单击应该能够按预期工作。

【讨论】:

  • 感谢您的帮助。我的 textView 在我的 xml 中,它被命名为 textView3。谢谢
  • @Geofrey Wasonga 如果这解决了您的答案,您可以投票,以便其他人也可以从答案中获得帮助。
猜你喜欢
  • 2023-04-08
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多