将Checkbox文本数据传递到Xamarin C#Android的单独活动页面上的另一个复选框?

时间:2014-05-30 02:24:01

标签: c# android listview checkbox xamarin

我已经设置了我的复选框,当我选中时,会将其发送到另一个活动的anther复选框。我需要弄清楚如何将每个选中的复选框移动到它自己的复选框。这是我现在的代码,它将所有选中的复选框作为一个复选框发送到另一个活动页面。

[Activity (Label = "Songression")]          
public class numbersActivity : Activity
{
    protected override void OnCreate (Bundle bundle)
    { 
        base.OnCreate (bundle);
        //Variables
        SetContentView (Resource.Layout.numbers);
        CheckBox cb0 = FindViewById<CheckBox> (Resource.Id.checkBox0);
        CheckBox cb1 = FindViewById<CheckBox> (Resource.Id.checkBox1);
        CheckBox cb2 = FindViewById<CheckBox> (Resource.Id.checkBox2);
        CheckBox cb3 = FindViewById<CheckBox> (Resource.Id.checkBox3);
        Button button0 = FindViewById<Button> (Resource.Id.button0);
        //Back Button
        button0.Click += delegate {
            StartActivity(typeof(MainActivity));
            };
        //Get Results button
        Button button01 = FindViewById<Button> (Resource.Id.button01);
            button01.Click += delegate {
            if (cb0.Checked) {
                myResources.result100 += cb0.Text + System.Environment.NewLine;

                    };
            if (cb1.Checked) {
                myResources.result100 += cb1.Text + System.Environment.NewLine;

                    };
            if (cb2.Checked) {
                myResources.result100 += cb2.Text + System.Environment.NewLine;
                    };
            if (cb3.Checked) {
                myResources.result100 += cb3.Text + System.Environment.NewLine;
                    };
            StartActivity (typeof(results));
        };
    }
}

}

以下是结果页面的代码,支持将复选框发送到

[Activity (Label = "Songression")]          
public class results : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.results);
        //Variables



        CheckBox post = FindViewById<CheckBox> (Resource.Id.result100);
        post.Text = myResources.result100;

        //Back Button
        Button button0 = FindViewById<Button> (Resource.Id.button0);
            button0.Click += delegate {
                StartActivity(typeof(MainActivity));
                };
        //Save Button
        Button buttonS = FindViewById<Button> (Resource.Id.buttonS);
            buttonS.Click += delegate {
        };
    }
}

}

以下是结果页面的xml

`                                          

        <CheckBox
            android:text="CheckBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/result100" />
    </LinearLayout>
</ScrollView>

`

1 个答案:

答案 0 :(得分:1)

将信息放入意图附加内容中。下面是如何传递它们的快速示例。

        button.Click += (sender, args) =>
            {
                var intent = new Intent(this, typeof(Results));
                intent.PutExtra("cbs", new[] { cb1.Checked, cb2.Checked, cb3.Checked, cb4.Checked });
                intent.PutExtra("texts", new[] { cb1.Text, cb2.Text, cb3.Text, cb4.Text });
                this.StartActivity(intent);
            };
    }
}

[Activity(Label = "Results", MainLauncher = false, Icon = "@drawable/icon")]
public class Results : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        var cbs = this.Intent.Extras.GetBooleanArray("cbs");
        var texts = this.Intent.Extras.GetStringArray("texts");

        foreach (var cb in cbs)
        {
            System.Diagnostics.Debug.WriteLine(cb);
        }

        foreach (var text in texts)
        {
            System.Diagnostics.Debug.WriteLine(text);
        }

    }
}