[英]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:onClick="markSquare" />
</GridLayout>
Description: Each button is inside a cell and points to a method called "markSquare", which looks like this:描述:每个按钮都在一个单元格内,并指向一个名为“markSquare”的方法,如下所示:
public void markSquare(View v) {
}
How can I get the position of the "v" parameter?如何获得“v”参数的position? (the same position I specified in "layout_row" and "layout_column")
(我在“layout_row”和“layout_column”中指定的相同的 position)
There is no direct way to get the position of the cell.没有直接的方法可以得到电池的 position。 However you use the view id to get exact view that click.
但是,您使用视图 ID 来获得点击的确切视图。
1: Give the specific ids to the button.ie button1, button2 1:给button具体的id。即button1,button2
2:And check your view clicks id's like: 2:并检查您的视图点击 id 如下:
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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.