gpt4 book ai didi

java - 如何从Android动态生成的表格中获得点击时的列号?

转载 作者:行者123 更新时间:2023-12-02 13:27:04 25 4
gpt4 key购买 nike

我有一个在应用程序中以编程方式生成的表。我能够获取所选行的位置并将其分配给变量,但是我不确定如何对列执行等效操作,例如。我真正需要知道的是如何像在这里对行进行操作一样,获取列号:

fun createTable(rows: Int, cols: Int) {
/* Here, 'i' represents the number of rows, which is determined by
* the length of the location list active in the application. */
for (i in 0 until locationList.size) {
/* Instantiate the row that will be used to generate each table. */
val row = TableRow(this)

/* Set the basic layout parameters for the textView, which is being used to contain the table. */
row.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

row.setOnClickListener {
selectedRow = i.toString().toInt()

if (locationList[selectedRow].entranceNoteArray.size == 0) {
showAlert("There are no transitions associated with this location. You may only add a note to an existent transition.")
} else {
isEnterOrExit()
}
}

row.gravity = Gravity.CENTER

for (j in 0 until columns) {
var rowIterator = 0
val textView = TextView(this)

textView.apply {
// Cosmetic/UI Related Code
textView.setPadding(10, 5, 10, 5)
layoutParams = TableRow.LayoutParams(350, TableRow.LayoutParams.WRAP_CONTENT)
textView.setTextColor(Color.DKGRAY)
textView.gravity = Gravity.CENTER
textView.textSize = 12F

/* The purpose of this decision structure is to fill the
* table's cells properly based upon which column cell 'j'
* represents. When j = 0 and i = 0, the first column of
* the first row has been selected - therefore, the first
* value (message) of the first location in the reversed
* locationList should be placed there. */
if (j == 0) {
text = locationList[i].message
textView.setTypeface(null, Typeface.BOLD)
} else if (j == 1) {
text = locationList[i].entered
textView.setTypeface(null, Typeface.NORMAL)
} else if (j == 2) {
text = locationList[i].exited
}
}
row.addView(textView)
rowIterator++
}
tableLayout.addView(row)
}
logLayout.addView(tableLayout)
}
有没有简单的方法可以做到这一点,或者我需要从 View 本身获取点击坐标才能以环形方式实现这一点?

最佳答案

如果您想知道在表中确切单击了哪个TextView,可以通过以下方式进行操作:

  • Foreach TextView您必须添加tag
  • textView.tag = "$i $j"
    i是行, j是列。
  • 现在您可以为每个TextView添加一个侦听器
  • textView.setOnClickListener {
    val tag = it.tag.toString()
    val row = tag.substring(0, tag.indexOf(' ')).toInt()
    val column = tag.substring(tag.indexOf(' ') + 1).toInt()

    //now You know which position was clicked
    }

    关于java - 如何从Android动态生成的表格中获得点击时的列号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63254440/

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