向表格单元格添加随机值

时间:2019-03-29 05:53:43

标签: c# android xamarin xamarin.android

我有TableLayout。我通过使用循环将3行3列添加到其中。

<TableLayout
android:id="@+id/tableLayout"
android:minHeight="180dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*"
android:background="#B2BEB5">
</TableLayout>

这是循环代码

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.activity_main);
    var tableLayout = FindViewById<TableLayout>(Resource.Id.tableLayout);
    rowCount = 3; columnCount = 3;
    TableLayout table = new TableLayout(this);
    for (int i = 0; i < rowCount; i++)
    {
        TableRow row = new TableRow(this);
        for (int j = 0; j < columnCount; j++)
        {
            var cell = new TextView(this);
            cell.SetText("(" + i + ", " + j + ")", TextView.BufferType.Normal);
            row.AddView(cell);
        }
        tableLayout.AddView(row);
    }
}

现在每个cell上我都想从charList来源添加随机值。

var charList = new List<string> { "A", "B", "C" };

    FindViewById<TableLayout>(Resource.Id.btnRun).Click += delegate
    {
        for (int i = 0; i < rowCount; i++)
        {
            TableRow row = new TableRow(this);
            for (int j = 0; j < columnCount; j++)
            {
                var cell = new TextView(this);
                cell.SetText("(" + i + ", " + j + ")", TextView.BufferType.Normal);
            }
        }
    };

我该怎么做?

2 个答案:

答案 0 :(得分:1)

您想要这种效果吗?

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        var tableLayout = FindViewById<TableLayout>(Resource.Id.tableLayout);
        var rowCount = 3;
        var columnCount = 3;
        for (int i = 0; i < rowCount; i++)
        {
            Android.Widget.TableRow row = new Android.Widget.TableRow(this);
            for (int j = 0; j < columnCount; j++)
            {
                var cell = new TextView(this);
                cell.SetText("(" + i + ", " + j + ")", TextView.BufferType.Normal);
                row.AddView(cell);
            }
            tableLayout.AddView(row);
        }
        var charList = new List<string> { "A", "B", "C" };

        FindViewById<Button>(Resource.Id.btnRun).Click += delegate
        {
            for (int i = 0; i < rowCount; i++)
            {
                Android.Widget.TableRow row = (Android.Widget.TableRow)tableLayout.GetChildAt(i);
                for (int j = 0; j < columnCount; j++)
                {
                    TextView cell = (TextView) row.GetChildAt(j);
                    Random random = new Random();
                    var num = random.NextInt(charList.Count);
                    cell.SetText(charList[num], TextView.BufferType.Normal);
                }
            }
        };
    }

在xaml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TableLayout
     android:id="@+id/tableLayout"
     android:minHeight="180dp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:stretchColumns="*"
     android:background="#B2BEB5">
  </TableLayout>
  <Button
     android:id="@+id/btnRun"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="CLICK"
     />
<LinearLayout>

答案 1 :(得分:1)

Random rndm=new Random();
int c= rndm.nextInt(charList.length()-1);


在charList.length()处添加数组的大小
这样,您就可以循环接收随机数。