将随机值添加到表格单元Android

问题描述 投票:1回答:2

我有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);
            }
        }
    };

我怎样才能做到这一点?

c# android xamarin xamarin.android
2个回答
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
投票
Random rndm=new Random();
int c= rndm.nextInt(charList.length()-1);

在charList.length()添加数组的大小 这样你就可以在循环中收到随机数。

© www.soinside.com 2019 - 2024. All rights reserved.