我在Kotlin中有一个函数,并试图在Android Studio中使用它,但它不起作用



如果我点击按钮,函数必须改变文本。正文在这里:

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

我想改变文本"Hello World"从我的函数:

fun aufruf() {
val items = mutableListOf<BisHunterMarksman>()
items.add(BisHunterMarksman("Head", "Rattling Deadeye Hoodt", "tThe Necrotic Wake"))
items.add(BisHunterMarksman("Necklace", "Trailspinner Pendantt", "tMists of Tirna Scithe"))
for (element in items) {
println("${element.itemSlot}t" + element.name + element.itemsource)
}
}

这必须打印fun aufruf

中的文本
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val button1 = findViewById(R.id.button) as Button
val myTextView = findViewById(R.id.textView) as TextView



button1.setOnClickListener(){


val myText = aufruf()
myTextView.text = "$myText"
}
}

您可以尝试以下操作:

fun aufruf(): String {
val items = mutableListOf<BisHunterMarksman>()
items.add(BisHunterMarksman("Head", "Rattling Deadeye Hoodt", "tThe Necrotic Wake"))
items.add(BisHunterMarksman("Necklace", "Trailspinner Pendantt", "tMists of Tirna Scithe"))
var myText = ""
items.forEach {
myText = "$myText${it.itemSlot}t${it.name}${it.itemSource}"
}
return myText
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val button1 = findViewById(R.id.button) as Button
val myTextView = findViewById(R.id.textView) as TextView

button1.setOnClickListener(){
val myText = aufruf()
myTextView.text = myText
}
}
}

现在我可以输入"it":

var myText = ""
items.forEach {
myText = "$myText${it.itemSlot}t${it.name}"
}
return myText
}

相关内容

  • 没有找到相关文章

最新更新