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

标签 java android kotlin

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

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/

    相关文章:

    java - 安卓开发 : Decrease horizontal swipe sensitivity OR turn off vertical swiping

    java - HashMap 有重复并导致 gridview 中图像重复

    java - 使用 ShareActionProvider 共享内容时检测成功/失败

    java - 无效的 JSON 文本 - SQL(Java JSON 对象)

    android - 将现有的 WebSocket 实现转换为 Android Java 中的 Reactive WebSocket

    kotlin - 通过 IntelliJ 运行 Google App Engine Standard 时出错 - 找不到 appengine-web.xml

    kotlin - Kotlin `shl`无法正常工作

    java - 根据 Spinner 1 中的选择动态更新 Spinner2

    java - Spring Boot自动配置包括

    java - 如何将 Java 注解添加到 JNI SWIG API?