如何为这些平铺对象随机分配值到属性

时间:2019-03-25 20:44:19

标签: java android xml

我正在学习Android Studio,并且正在构建Minesweeper类型游戏的早期版本。我在屏幕上有16个磁贴,单击时会更改其文本。现在,所有mine_count都设置为0,这是单击时显示的内容,但是我想将其中的三个随机分配为1,以表示是我的地雷。我想在应用加载时在主要活动中的onCreate()方法中执行此操作。这是该应用程序和XML布局的屏幕截图。我还有一个方法可以将我所有的图块作为列表返回,如下所示:

enter image description here

private ArrayList<Tile> getAllTiles() {
    ArrayList<Tile> l = new ArrayList<>();
    ViewGroup vg = (ViewGroup) findViewById(R.id.label_grid);
    for (int i = 0, len = vg.getChildCount(); i < len; i++) {
        View v = vg.getChildAt(i);
        if (v instanceof Tile) {
            Tile w = (Tile) v;
            l.add(w);
        }
    }
    return l;
}

|

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:grid="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="minesweepervb.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:id="@+id/label_top"
    android:text="@string/app_name" />

<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/label_top"
    android:useDefaultMargins="true"
    android:alignmentMode="alignBounds"
    android:columnOrderPreserved="false"
    android:columnCount="2"
    >
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="1"
        grid:col="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="1"
        grid:col="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="2"
        grid:col="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="2"
        grid:col="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="3"
        grid:col="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="3"
        grid:col="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="4"
        grid:col="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="4"
        grid:col="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="5"
        grid:col="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="5"
        grid:col="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="6"
        grid:col="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="6"
        grid:col="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="7"
        grid:col="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="7"
        grid:col="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="8"
        grid:col="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
    <minesweepervb.Tile
        android:onClick="probe"
        android:background="@drawable/bg_tile"
        grid:mine_count="0"
        grid:row="8"
        grid:col="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1.0"
        android:layout_rowWeight="1.0"
        android:textAlignment="center"
        android:gravity="center"
        android:text="X" />
</GridLayout>

0 个答案:

没有答案