Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Xamarin android中使用复选框向表布局添加数据_C#_Android_Xamarin.android_Tablelayout_Android Tablelayout - Fatal编程技术网

C# 如何在Xamarin android中使用复选框向表布局添加数据

C# 如何在Xamarin android中使用复选框向表布局添加数据,c#,android,xamarin.android,tablelayout,android-tablelayout,C#,Android,Xamarin.android,Tablelayout,Android Tablelayout,如何在Xamarin android中使用复选框向表布局添加数据 样本如下: 我写了一个演示,向表格布局添加数据,您可以参考它 这是这个演示的GIF 下面是代码。 MainActivity.cs namespace CheckBoxDemo { [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)] public class MainActivity : AppCompatAc

如何在Xamarin android中使用复选框向表布局添加数据

样本如下:


我写了一个演示,向表格布局添加数据,您可以参考它

这是这个演示的GIF

下面是代码。 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);
    }

}
}
名称空间CheckBoxDemo
{
[活动(Label=“@string/app_name”,Theme=“@style/AppTheme”,MainLauncher=true)]
公共类MainActivity:AppCompativeActivity
{
表布局TLU视图;
复选框cb;
TextView tv_VIEW 1;
TextView tv_view 2;
TextView电视视图3;
TextView电视视图4;
结核病;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.activity_main);
tl_视图=findviewbyd(Resource.Id.tl_视图);
整数计数=1;
按钮bt_add=findviewbyd(Resource.Id.bt_add);
点击+=(e,o)=>{
addRole(ref计数);
};
}
public void addRole(ref int count)
{
cb=新复选框(此复选框);
tv_view1=新文本视图(此);
如果(计数<10)
{
tv_view1.Text=“0”+计数;
}
其他的
{
tv_view1.Text=”“+计数;
}
计数++;
tv_view2=新文本视图(此);
tv_view2.Text=“测试”;
tv_view3=新文本视图(本);
tv_view3.Text=“测试”;
tv_view4=新文本视图(此);
tv_view4.Text=“1”;
tb=新表格行(此);
tb.AddView(cb);
tb.AddView(电视视图1);
tb.AddView(电视视图2);
tb.AddView(电视视图3);
tb.AddView(电视视图4);
tl_视图。添加视图(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>

发布的问题似乎根本不包括解决问题的任何尝试。StackOverflow希望您能这样做,因为您的尝试有助于我们更好地了解您的需求。请编辑以显示您尝试过的内容,以便举例说明您遇到的具体障碍。欲了解更多信息,请参阅如何询问并采取行动