Android Studio > GridLayout > how to get the position of a cell?

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

Android Studio > GridLayout > how to get the position of a cell?

问题

<GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_column="0"
            android:onClick="markSquare" />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_column="1"
            android:onClick="markSquare" />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_column="0"
            android:onClick="markSquare" />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_column="1"
            android:onClick="markSquare" />

</GridLayout>

Description:
每个按钮都在一个单元格内,并指向一个名为 "markSquare" 的方法,该方法如下:

public void markSquare(View v) {
}

如何获取 "v" 参数的位置?(与我在 "layout_row" 和 "layout_column" 中指定的位置相同)

英文:
<GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_column="0"
            android:onClick="markSquare" />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_column="1"
            android:onClick="markSquare" />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_column="0"
            android:onClick="markSquare" />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:onClick="markSquare" />

</GridLayout>

Description:
Each button is inside a cell and points to a method called "markSquare", which looks like this:

public void markSquare(View v) {
}

How can I get the position of the "v" parameter? (the same position I specified in "layout_row" and "layout_column")

答案1

得分: 0

没有直接获取单元格位置的方法。但是,您可以使用视图ID来获取确切的被点击的视图。

1:给按钮分配特定的ID,例如button1,button2。

2:并检查您的视图点击的ID,如下所示:

public void markSquare(View v) {
    switch(v.getId()){
    case R.id.button1:{
    //在这里执行您想要的操作
    break;
    }
    
    case R.id.button2:{
    //在这里执行您想要的操作
    
    break;
    }
    
    }
}
英文:

There is no direct way to get the position of the cell. However you use the view id to get exact view that click.

1: Give the specific ids to the button.i.e button1, button2

2:And check your view clicks id's like:

 public void markSquare(View v) {
    switch(v.getId()){
    case R.id.button1:{
    //Do here what you want
    break;
    }
    
    case R.id.button2:{
    //Do here what you want
    
    break;
    }
    
    }
 }

huangapple
  • 本文由 发表于 2020年5月3日 23:56:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61577464.html
匿名

发表评论

匿名网友

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

确定