TextView文本会更改,但不会在布局中更新(Kotlin-Android)

问题描述 投票:0回答:3

TextView文本会更改,但不会在布局中更新。我尝试了所有可以找到的方法,但是没有任何效果。我有一个非常简单的应用程序,具有一个活动和3种布局*。*这是我制作的第一个应用程序,所以我想这样会更简单我面临的主要问题是两个:几乎所有的信息都是旧的,并且都在Java中,并且我的textView文本没有更改。.该应用程序是我尝试作为练习的一个简单的Rock-Paper-Scissor游戏。

textViews.text值得到更新,但布局始终显示相同的文本...我不知道可能是什么问题。我也正在努力确切地了解所有这些工作原理,例如InflateLayout,Context和Android。我对android的参考了解不多。

[[没有INFLATE(),POSTINFLATE(),FORCELAYOUT(),可见性切换,因为它们都不起作用:(

代码摘录

class MainActivity : AppCompatActivity() { lateinit var TITLE:TextView lateinit var PARAGRAPH:TextView override fun onCreate(savedInstanceState :Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val InflaterInitializer = LayoutInflater.from(applicationContext) as LayoutInflater val inflater = InflaterInitializer.inflate(R.layout.activity_2, null, false) TITLE= inflater.findViewById(R.id.title) as TextView PARAGRAPH= inflater.findViewById(R.id.paragraph) as TextView }

共有三个功能:

fun FUNCTION(v :View) { val userChoice = "XXX" val computerChoice = getComputerChoice() if (userChoice == computerChoice) { FUNCTION_2(computerChoice) } else { runOnUiThread { TITLE.text = if (computerChoice == "YYY") getString(R.string.YOU_WON) else getString(R.string.YOU_LOSE); PARAGRAPH.text = getString(R.string.STRING, computerChoice) } }; resultScreen() }

Function_2 ...

private fun FUNCTION_2(cc :String) { runOnUiThread { TITLE.text = getString(R.string.STRING) PARAGRAPH.text = getString(R.string.STRING, cc) }; resultScreen() }
resultScreen()只是对setContentView(LAYOUT)的调用

代码在这里完成:https://github.com/noiwyr/MorraCinese

以下是该应用和更新问题的视频:https://imgur.com/a/iWCRMkq

android kotlin textview refresh invalidation
3个回答
0
投票
您不需要任何东西,您可以通过代码创建,我建议您没有问题

val InflaterInitializer = LayoutInflater.from(applicationContext)as LayoutInflater val inflater = InflaterInitializer.inflate(R.layout.activity_outcome,null,false)

注释上面的代码,无需在Kotlin中进行

motivRisultato.text = getString(R.string.scelta_pc,computerChoice)

简单地编写这种类型的代码


0
投票
通过调用InflaterInitializer.inflate(R.layout.activity_2, null, false),您从指定的xml资源中填充了一个新的视图层次结构,该资源未附加到您的任何视图中(这些新视图未显示在屏幕上)。然后,您从该

new视图层次结构中找到了文本视图,并更改了它们的标题。

因此,您的onCreate方法必须看起来像这样:

override fun onCreate(savedInstanceState :Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_2) TITLE = findViewById(R.id.title) PARAGRAPH = findViewById(R.id.paragraph) }

[另外,使用方法runOnUiThread()(您的代码已经在Ui线程上运行)和resultScreen()是多余的。

0
投票
您的代码中有很多错误,因此我将用您的代码来分解答案。是否找到内嵌的注释

class MainActivity : AppCompatActivity() { /** * First, Follow conventions for naming variables, they are usually in camelcase for variables and functions, Capitalized for Constants. * Second, lateinit is used to defer the initialization of a variable, for views, such as * TextView's, you could use the Kotlin Synthentic library which automatically references the Views of your layout. */ lateinit var TITLE:TextView lateinit var PARAGRAPH:TextView override fun onCreate(savedInstanceState :Bundle?) { super.onCreate(savedInstanceState) /** * Set content view, internally set's the layout file after inflation using the Activity context. Which means, that you do not * need to specifically inflate the view. */ setContentView(R.layout.activity_main) /** * This is the reason why your layout doesn't know refresh, what you're doing here is inflating another layout, but not setting it to your activity. * This is not required as explained above */ val InflaterInitializer = LayoutInflater.from(applicationContext) as LayoutInflater /** * Inflater inflates a View Object. one would use this approach if they were programatically adding Views */ val inflater = InflaterInitializer.inflate(R.layout.activity_2, null, false) /** * the below views are pointing to a reference of TextView for the non visible inflated view. Which is the reason why the text is not updated. */ TITLE= inflater.findViewById(R.id.title) as TextView PARAGRAPH= inflater.findViewById(R.id.paragraph) as TextView } }

这是使事情正常运行的代码

class MainActivity : AppCompatActivity() { private var title:TextView? = null private var paragraph:TextView? = null override fun onCreate(savedInstanceState :Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title= inflater.findViewById(R.id.title) as TextView paragraph= inflater.findViewById(R.id.paragraph) as TextView } fun function(v :View) { val userChoice = "XXX" val computerChoice = getComputerChoice() if (userChoice == computerChoice) { function2(computerChoice) } else { title.text = if (computerChoice == "YYY") getString(R.string.YOU_WON) else getString(R.string.YOU_LOSE); paragraph.text = getString(R.string.STRING, computerChoice) } resultScreen() } private fun function2(cc :String) { title.text = getString(R.string.STRING) paragraph.text = getString(R.string.STRING, cc) resultScreen() } }

如果您的用例是显示不同的屏幕,请考虑启动多个活动并使用Intents在它们之间进行转换>
© www.soinside.com 2019 - 2024. All rights reserved.