gpt4 book ai didi

android - 我们可以获取在 android/kotlin 中动态创建的网格布局的单元格文本值吗?

转载 作者:行者123 更新时间:2023-11-29 23:17:21 26 4
gpt4 key购买 nike

它是一个动态创建的6*6 gridlayout。我想获取每个单元格中文本的值,以便当用户单击板上具有特定文本(例如:动物)的单元格时,该单元格应该被禁用并且不能在该单元格上进行任何点击。

如何获取网格布局中动态创建的单元格的文本值?

创建看板代码(acticity.kt):

 fun createBoard(context: Context, board: GridLayout, size: Int, listofThings: List<String>) {
destroyBoard()
board.columnCount = size
board.rowCount = size
var iterator = 0
for(col in 1..size) {
for (row in 1..size) {
cell = RelativeLayout(context)
val cellSpecifications = { GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1f) }
val params = GridLayout.LayoutParams(cellSpecifications(), cellSpecifications())
params.width = 0
cell.layoutParams = params
cell.setBackgroundResource(R.drawable.bordered_rectangle)
cell.gravity = Gravity.CENTER
cell.setPadding(5, 0, 5, 0)

text = TextView(context)
text.text = people[iterator++]
words.add(text.text as String)

text.maxLines = 5
text.setSingleLine(false)
text.gravity = Gravity.CENTER
text.setTextColor(0xFF000000.toInt())

cell.addView(text)
board.addView(cell)
cells.add(GameCell(cell, text, false, row, col) { })
}
}
}

activity.xml 中的代码:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.GridLayout
android:id="@+id/grid"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_weight="1"
>

</android.support.v7.widget.GridLayout>

</LinearLayout>

最佳答案

在调用 createBoard 方法后,您可以在 GridLayout 中的每个单元格上设置一个监听器。

val board = findViewById<android.support.v7.widget.GridLayout>(R.id.grid)

createBoard(this, board, 3, listOf<String>())

val childCount = board.childCount
for (i in 0 until childCount) {
val cell = board.getChildAt(i) as RelativeLayout
cell.setOnClickListener { view ->
val textView = (view as RelativeLayout).getChildAt(0) as TextView
val textValue = textView.text
// Process textValue here
Toast.makeText(this, textValue, Toast.LENGTH_SHORT).show()
}
}

关于android - 我们可以获取在 android/kotlin 中动态创建的网格布局的单元格文本值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55091880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com