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

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

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

问题

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

fun createTable(rows: Int, cols: Int) {
        /* 这里,'i'代表行数,由应用程序中活动的位置列表的长度确定。 */
        for (i in 0 until locationList.size) {
            /* 实例化将用于生成每个表格的行。 */
            val row = TableRow(this)

            /* 设置用于包含表格的textView的基本布局参数。 */
            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("此位置没有关联的过渡。您只能向现有过渡添加注释。")
                } else {
                    isEnterOrExit()
                }
            }

            row.gravity = Gravity.CENTER

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

                textView.apply {
                    // 美观/UI相关代码
                    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

                    /* 这个决策结构的目的是根据列单元格'j'表示的内容来正确填充表格的单元格。当j = 0且i = 0时,已选择第一行的第一列 - 因此,应将翻转的locationList中第一个位置的第一个值(消息)放在那里。 */
                    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)
    }

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

英文:

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:

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)
    }

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
textView.tag = "$i $j"

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

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

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
textView.tag = "$i $j"

i is the row and j is the column.

  1. Now You can add one listener for every 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
}

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:

确定