简体   繁体   中英

How to add data to a table layout with checkbox in Xamarin android

How to add data to a table layout with checkbox in Xamarin android.

sample like below:

在此处输入图片说明

I wrote a demo that add data to a table layout, you could refer to it.

This is GIF of this demo.

在此处输入图片说明

Following is code. MainActivity.cs

namespace CheckBoxDemo
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    TableLayout tl_view;
    CheckBox cb;
    TextView tv_view1;
    TextView tv_view2;
    TextView tv_view3;
    TextView tv_view4;

    TableRow tb;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

         tl_view = FindViewById<TableLayout>(Resource.Id.tl_view);
        int count = 1;

        Button bt_add = FindViewById<Button>(Resource.Id.bt_add);
        bt_add.Click += (e, o) => {
            addRole(ref count);
        };
    }

    public void addRole( ref int count)
    {
         cb = new CheckBox(this);
         tv_view1 = new TextView(this);
        if (count < 10)
        {

            tv_view1.Text = "0"+count;

        }
        else
        {
            tv_view1.Text = "" + count;

        }
        count++;
        tv_view2 = new TextView(this);
        tv_view2.Text = "test";

        tv_view3 = new TextView(this);
        tv_view3.Text = "test";

        tv_view4 = new TextView(this);
        tv_view4.Text = "1";


        tb = new TableRow(this);
        tb.AddView(cb);
        tb.AddView(tv_view1);
        tb.AddView(tv_view2);
        tb.AddView(tv_view3);
        tb.AddView(tv_view4);
        tl_view.AddView(tb);
    }

}
}

activity_main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll_layout ">

<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tl_view "
    >
<TableRow>  

   <TextView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:text="   "
        android:textColor="@android:color/black"
        android:textSize="15dp"
    />
   <TextView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:text="  Col1             "
        android:textColor="@android:color/black"
        android:textSize="15dp"
    />
   <TextView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:text="Col2              "
        android:textColor="@android:color/black"
        android:textSize="15dp"
    />
   <TextView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:text="Col3              "
        android:textColor="@android:color/black"
        android:textSize="15dp"
    />
    <TextView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:text="Col4              "
        android:textColor="@android:color/black"
        android:textSize="15dp"
    />

</TableRow> 

</TableLayout>

<Button
 android:id="@+id/bt_add"
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"
 android:text="add"
/>
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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