如何在Android中从动态生成的表格中点击时获取列号?

huangapple go评论102阅读模式
英文:

How to get column number on click from dynamically generated table in Android?

问题

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

  1. fun createTable(rows: Int, cols: Int) {
  2. /* 这里,'i'代表行数,由应用程序中活动的位置列表的长度确定。 */
  3. for (i in 0 until locationList.size) {
  4. /* 实例化将用于生成每个表格的行。 */
  5. val row = TableRow(this)
  6. /* 设置用于包含表格的textView的基本布局参数。 */
  7. row.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
  8. row.setOnClickListener {
  9. selectedRow = i.toString().toInt()
  10. if (locationList[selectedRow].entranceNoteArray.size == 0) {
  11. showAlert("此位置没有关联的过渡。您只能向现有过渡添加注释。")
  12. } else {
  13. isEnterOrExit()
  14. }
  15. }
  16. row.gravity = Gravity.CENTER
  17. for (j in 0 until columns) {
  18. var rowIterator = 0
  19. val textView = TextView(this)
  20. textView.apply {
  21. // 美观/UI相关代码
  22. textView.setPadding(10, 5, 10, 5)
  23. layoutParams = TableRow.LayoutParams(350, TableRow.LayoutParams.WRAP_CONTENT)
  24. textView.setTextColor(Color.DKGRAY)
  25. textView.gravity = Gravity.CENTER
  26. textView.textSize = 12F
  27. /* 这个决策结构的目的是根据列单元格'j'表示的内容来正确填充表格的单元格。当j = 0且i = 0时,已选择第一行的第一列 - 因此,应将翻转的locationList中第一个位置的第一个值(消息)放在那里。 */
  28. if (j == 0) {
  29. text = locationList[i].message
  30. textView.setTypeface(null, Typeface.BOLD)
  31. } else if (j == 1) {
  32. text = locationList[i].entered
  33. textView.setTypeface(null, Typeface.NORMAL)
  34. } else if (j == 2) {
  35. text = locationList[i].exited
  36. }
  37. }
  38. row.addView(textView)
  39. rowIterator++
  40. }
  41. tableLayout.addView(row)
  42. }
  43. logLayout.addView(tableLayout)
  44. }

有没有一种简单的方法来做到这一点,还是我需要从视图本身获取点击坐标,以便以迂回的方式实现这一点?

英文:

I have a table that is generated programmatically in an application. I'm able to get the position of a selected row and assign it to a variable, but I'm not sure how to perform the equivalent for columns, like, at all. All I really need to know is how to get the column number on tap like I do with rows here:

  1. fun createTable(rows: Int, cols: Int) {
  2. /* Here, 'i' represents the number of rows, which is determined by
  3. * the length of the location list active in the application. */
  4. for (i in 0 until locationList.size) {
  5. /* Instantiate the row that will be used to generate each table. */
  6. val row = TableRow(this)
  7. /* Set the basic layout parameters for the textView, which is being used to contain the table. */
  8. row.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
  9. row.setOnClickListener {
  10. selectedRow = i.toString().toInt()
  11. if (locationList[selectedRow].entranceNoteArray.size == 0) {
  12. showAlert("There are no transitions associated with this location. You may only add a note to an existent transition.")
  13. } else {
  14. isEnterOrExit()
  15. }
  16. }
  17. row.gravity = Gravity.CENTER
  18. for (j in 0 until columns) {
  19. var rowIterator = 0
  20. val textView = TextView(this)
  21. textView.apply {
  22. // Cosmetic/UI Related Code
  23. textView.setPadding(10, 5, 10, 5)
  24. layoutParams = TableRow.LayoutParams(350, TableRow.LayoutParams.WRAP_CONTENT)
  25. textView.setTextColor(Color.DKGRAY)
  26. textView.gravity = Gravity.CENTER
  27. textView.textSize = 12F
  28. /* The purpose of this decision structure is to fill the
  29. * table's cells properly based upon which column cell 'j'
  30. * represents. When j = 0 and i = 0, the first column of
  31. * the first row has been selected - therefore, the first
  32. * value (message) of the first location in the reversed
  33. * locationList should be placed there. */
  34. if (j == 0) {
  35. text = locationList[i].message
  36. textView.setTypeface(null, Typeface.BOLD)
  37. } else if (j == 1) {
  38. text = locationList[i].entered
  39. textView.setTypeface(null, Typeface.NORMAL)
  40. } else if (j == 2) {
  41. text = locationList[i].exited
  42. }
  43. }
  44. row.addView(textView)
  45. rowIterator++
  46. }
  47. tableLayout.addView(row)
  48. }
  49. logLayout.addView(tableLayout)
  50. }

Is there a simple way to do this, or do I need to get the on tap coordinates from the view itself in order to achieve this in a roundabout way?

答案1

得分: 1

如果您想知道在表格中点击了哪个TextView,可以按照以下方式进行操作:

  1. 对于每个TextView,您需要添加一个tag
  1. textView.tag = "$i $j"

其中,i 表示行数,j 表示列数。

  1. 现在,您可以为每个TextView添加一个监听器:
  1. textView.setOnClickListener {
  2. val tag = it.tag.toString()
  3. val row = tag.substring(0, tag.indexOf(' ')).toInt()
  4. val column = tag.substring(tag.indexOf(' ') + 1).toInt()
  5. // 现在,您知道点击了哪个位置
  6. }
英文:

If You want to know which exactly TextView was clicked in Table You can do this in the following way:

  1. Foreach TextView You have to add tag
  1. textView.tag = "$i $j"

i is the row and j is the column.

  1. Now You can add one listener for every TextView
  1. textView.setOnClickListener {
  2. val tag = it.tag.toString()
  3. val row = tag.substring(0, tag.indexOf(' ')).toInt()
  4. val column = tag.substring(tag.indexOf(' ') + 1).toInt()
  5. //now You know which position was clicked
  6. }

huangapple
  • 本文由 发表于 2020年8月5日 04:15:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63254440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定