Android Kotlin:尝试使用Kotlin打印多行
我是Kotlin/Java的初学者。我试着制作一个想法很简单的移动应用程序:一个按钮将当前时间打印到文本表中,每次按下一个按钮都会打印新行 我设法使按钮打印当前行,但当我再次按下按钮时,它会覆盖先前打印的时间 当前我的代码如下所示:Android Kotlin:尝试使用Kotlin打印多行,android,kotlin,multiline,Android,Kotlin,Multiline,我是Kotlin/Java的初学者。我试着制作一个想法很简单的移动应用程序:一个按钮将当前时间打印到文本表中,每次按下一个按钮都会打印新行 我设法使按钮打印当前行,但当我再次按下按钮时,它会覆盖先前打印的时间 当前我的代码如下所示: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val CryButton = findViewById<Button>(R.id.CryButton)
val CryTable = findViewById<TextView>(R.id.CryTable)
CryButton.setOnClickListener {
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("hh-mm")
val formatted = current.format(formatter)
CryTable.text = formatted.toString()
override-fun-onCreate(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val CryButton=findviewbyd(R.id.CryButton)
val CryTable=findViewById(R.id.CryTable)
CryButton.setOnClickListener{
val current=LocalDateTime.now()
val formatter=DateTimeFormatter.of模式(“hh-mm”)
val formatted=当前格式(格式化程序)
CryTable.text=格式化的.toString()
我不知道如何让代码记住最后打印的文本,也无法在web上找到任何解决方案。使用append-
val CryButton = findViewById<Button>(R.id.CryButton)
val CryTable = findViewById<TextView>(R.id.CryTable)
CryTable.text = ""
CryButton.setOnClickListener {
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("hh-mm")
val formatted = current.format(formatter)
CryTable.append(formatted.toString())
val CryButton=findviewbyd(R.id.CryButton)
val CryTable=findViewById(R.id.CryTable)
CryTable.text=“”
CryButton.setOnClickListener{
val current=LocalDateTime.now()
val formatter=DateTimeFormatter.of模式(“hh-mm”)
val formatted=当前格式(格式化程序)
CryTable.append(格式化的.toString())
使用append-
val CryButton = findViewById<Button>(R.id.CryButton)
val CryTable = findViewById<TextView>(R.id.CryTable)
CryTable.text = ""
CryButton.setOnClickListener {
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("hh-mm")
val formatted = current.format(formatter)
CryTable.append(formatted.toString())
val CryButton=findviewbyd(R.id.CryButton)
val CryTable=findViewById(R.id.CryTable)
CryTable.text=“”
CryButton.setOnClickListener{
val current=LocalDateTime.now()
val formatter=DateTimeFormatter.of模式(“hh-mm”)
val formatted=当前格式(格式化程序)
CryTable.append(格式化的.toString())
您需要更改的不是替换TextView的文本,而是在其中添加新行
解决方法是将此行CryTable.text=formatted.toString()
替换为此行CryTable.text=CryTable.text+“\n”+formatted.toString()
这将使您的文本视图保留旧文本,在其中添加新行,然后添加新文本
希望这能有所帮助。您需要更改的不是替换文本视图的文本,而是在其中添加一行新行 解决方法是将此行
CryTable.text=formatted.toString()
替换为此行CryTable.text=CryTable.text+“\n”+formatted.toString()
这将使您的文本视图保留旧文本,在其中添加新行,然后添加新文本
希望这有帮助。请尝试在一个变量中使用take old time,当您来打印新时间时,concat new time with old time with the \n,它会工作。请尝试在一个变量中使用take old time,当您来打印新时间时,concat new time with old time with the \n,它会工作。欢迎使用堆栈溢出:)和kotlin,无需执行
findViewById
您可以在此处查看更多信息:DHi!是的,您不必在kotlin中使用findViewById。我不太明白您想做什么,但也许您可以将上次打印的文本设置为全局变量,每次单击按钮都可以检查此变量。欢迎使用堆栈溢出:)w对于kotlin,无需执行findViewById
您可以在此处查看更多信息:DHi!是的,您不必在kotlin中使用findViewById。我不太明白您想要做什么,但也许您可以将上次打印的文本设置为全局变量,每次单击按钮都可以检查此变量。请将其标记为如果它解决了您的问题,请回答正确。谢谢:)@Aleksimm如果它解决了您的问题,请将其标记为正确答案。谢谢:)@Aleksimm