简体   繁体   English

我如何在Android中的表格布局中随机播放内容

[英]How can i shuffle the content in my tablelayout in android

Hi i'm new in creating android application and i would like to ask for help to solve my problem. 嗨,我是创建Android应用程序的新手,我想寻求帮助来解决我的问题。 my problem is i want to shuffle my Questionnaire using the tablelayout. 我的问题是我想使用表格布局对问卷进行洗牌。 it is possible for me to shuffle it? 我有可能洗牌吗? sorry because i'm new in java. 抱歉,因为我是Java新手。

This is the code in xml i use: 这是我使用的xml中的代码:

<TableLayout            
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:shrinkColumns="*"  android:stretchColumns="*" android:background="#000000">
                    <!-- Row 1 with single column -->
                    <TableRow
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:gravity="center_horizontal">
                        <TextView
                            android:layout_width="match_parent" android:layout_height="wrap_content"
                            android:textSize="18dp" android:text="1. What color is at the bottom of the rainbow??"  android:layout_span="4"
                            android:padding="18dip"
                            android:textColor="#ffffff"/>
                   </TableRow>
                    <!-- Row 2 with 3 columns -->
                    <TableRow
                        android:id="@+id/tableRow1"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent">
                        <RadioGroup
        android:id="@+id/answer1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.4" >
        <RadioButton
            android:id="@+id/answer1A"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="Violet" />
        <RadioButton
            android:id="@+id/answer1B"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="Red" />
        <RadioButton
            android:id="@+id/answer1C"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="Blue" />
    </RadioGroup>
                    </TableRow>
                  <TableRow
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:gravity="center_horizontal">
                        <TextView
                            android:layout_width="match_parent" android:layout_height="wrap_content"
                            android:textSize="18dp" android:text="2. A farmer has 20 sheep and all but 15 die. How many sheep are left? "  android:layout_span="4"
                              android:padding="18dip"
                            android:textColor="#ffffff"/>
                   </TableRow>
                    <!-- Row 2 with 3 columns -->
                    <TableRow
                        android:id="@+id/tableRow2"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent">
                        <RadioGroup
        android:id="@+id/answer2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.4" >
        <RadioButton
            android:id="@+id/answer2A"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="5" />
        <RadioButton
            android:id="@+id/answer2B"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="10" />
        <RadioButton
            android:id="@+id/answer2C"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="15" />
    </RadioGroup>
                    </TableRow>
                     <TableRow
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:gravity="center_horizontal">
                        <TextView
                            android:layout_width="match_parent" android:layout_height="wrap_content"
                            android:textSize="18dp" android:text="3. Yard is to inch as quart is to ?"  android:layout_span="4"
                             android:padding="18dip"
                            android:textColor="#ffffff"/>
                   </TableRow>
                    <!-- Row 2 with 3 columns -->
                    <TableRow
                        android:id="@+id/tableRow3"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent">
                        <RadioGroup
        android:id="@+id/answer3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.4" >
        <RadioButton
            android:id="@+id/answer3A"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="Gallon" />
        <RadioButton
            android:id="@+id/answer3B"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="Ounce" />
        <RadioButton
            android:id="@+id/answer3C"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="Milk" />
    </RadioGroup>
                    </TableRow>
                    <TableRow
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:gravity="center_horizontal">
                        <TextView
                            android:layout_width="match_parent" android:layout_height="wrap_content"
                            android:textSize="18dp" android:text="4. Pen is to poet as needle is to?"  android:layout_span="4"
                             android:padding="18dip"
                            android:textColor="#ffffff"/>
                   </TableRow>
                    <!-- Row 2 with 3 columns -->
                    <TableRow
                        android:id="@+id/tableRow4"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent">
                        <RadioGroup
        android:id="@+id/answer4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.4" >
        <RadioButton
            android:id="@+id/answer4A"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="Thread" />
        <RadioButton
            android:id="@+id/answer4B"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="Sewing" />
        <RadioButton
            android:id="@+id/answer4C"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="Tailor" />
    </RadioGroup>
                    </TableRow>
                     <TableRow
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:gravity="center_horizontal">

                         <Button
                             android:id="@+id/submit"
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:gravity="center"
                             android:text="Submit" />

                   </TableRow>
  </TableLayout>

Currently you have set up a fixed layout using the layout editor. 当前,您已经使用布局编辑器设置了固定布局。 Due to this you cannot shuffle the existing content. 因此,您无法将现有内容混洗。

What you need is to dynamically change the layout. 您需要的是动态更改布局。 To do so you would need to just set the parent layout in your editor. 为此,您只需要在编辑器中设置父布局。 Then in your oncreate method of the mainactivity you need to fill the table layout with table rows dynamically. 然后,在mainactivity的oncreate方法中,您需要动态地用表行填充表布局。 This is how you will ensure that everytime the app is started the table rows are shuffled. 这样,您将确保每次启动应用程序时都对表行进行混洗。 Yes you will need a code change. 是的,您将需要更改代码。 Something like this to begin with 像这样的东西开始

TableLayout ll = (TableLayout) findViewById(R.id.tableLayout);


for (int i = 0; i < 4; i++) {

    TableRow row= new TableRow(this);
    TableRow.LayoutParams lp = new  TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(lp);
    //add your buttons and stuff
    ll.addView(row,i);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM