如何在Android菜单中使用CheckBox?

时间:2015-03-05 05:20:41

标签: android xamarin xamarin.android

我是Android新手。我想在菜单中使用一个复选框,当用户触摸时,该复选框必须被选中和取消选中。如果选中复选框,我需要执行一些操作。现在我有一个在程序运行时检查的复选框。

我已设法在菜单中插入复选框,并在应用启动时对其进行检查,就像它应该的那样。但是当我尝试取消选中它时,程序崩溃了。

menu.xml文件:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:id="@+id/checkBox1"
            android:showAsAction="never"
            android:title="Allow Check"
            android:checkable="true"
            android:checked="true" />

        <item
            android:id="@+id/action_help"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="Hai"/>

        <item
             android:id="@+id/checkBox2"
             android:showAsAction="never" 
             android:title="Allow Check 2" 
             android:checkable="true"
             android:checked="true" />
    </menu>

MainActivity.cs

    public override bool OnCreateOptionsMenu (IMenu menu)
    {
        base.OnCreateOptionsMenu (menu);
        MenuInflater inflater = this.MenuInflater;
        inflater.Inflate (Resource.Menu.menu, menu);
    return true;
            }

    public override bool OnOptionsItemSelected (Android.Views.IMenuItem item)
    {
        switch (item.ItemId) {
            case Resource.Id.action_help:
                return true;
            case Resource.Id.checkBox1:
                CheckBox check1 = (CheckBox)FindViewById (Resource.Id.checkBox1);
//Here the check1 come as null, which leads to the crash.
                if (check1.Checked == true)
                    check1.Checked = false;
                else
                    check1.Checked = true;
                check1.Click += check1_Click;
                return true;
            case Resource.Id.checkBox2:
                CheckBox check2 = (CheckBox)FindViewById (Resource.Id.checkBox2);
                if (check2.Checked == true)
                    check2.Checked = false;
                else
                    check2.Checked = true;
                check2.Click += check2_Click;
                return true;

                default:
                    return base.OnOptionsItemSelected (item);
                }
        }

在尝试检索菜单中声明的​​复选框时,它变为null。我怎样才能使这个工作?

3 个答案:

答案 0 :(得分:2)

请使用代码段,这可以帮助您

public override bool OnOptionsItemSelected (Android.Views.IMenuItem item)
    {
        switch (item.ItemId) {
            case Resource.Id.action_help:
                return true;
            case Resource.Id.checkBox1:

                if (item.IsChecked)
                    item.SetChecked(false);
                else
                    item.SetChecked(true);
                item.Click += check1_Click;
                return true;
            case Resource.Id.checkBox2:

                if (item.IsChecked)
                    item.SetChecked(false);
                else
                    item.SetChecked(true);
                item.Click += check2_Click;
                return true;

                default:
                    return base.OnOptionsItemSelected (item);
                }
        }

答案 1 :(得分:0)

尝试通过调用item.IsChecked来获取项目的已检查状态。

答案 2 :(得分:0)

您必须在OnCreate()

中附加这些控件
public class MainActivity : Activity
{
    private CheckBox check1;
    private CheckBox check2;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Create your application here
        SetContentView(Resource.Layout.yourLayout);

        //find controls in here
        check1 = (CheckBox)FindViewById (Resource.Id.checkBox1);
        check2 = (CheckBox)FindViewById (Resource.Id.checkBox2);
    }   

.... 然后做你的检查

    public override bool OnOptionsItemSelected (Android.Views.IMenuItem item)
    {
        switch (item.ItemId) {
            case Resource.Id.action_help:
                return true;
            case Resource.Id.checkBox1:                    
                if (check1.Checked == true)
                    check1.Checked = false;
                else
                    check1.Checked = true;
                check1.Click += check1_Click;
                return true;
            case Resource.Id.checkBox2:
                if (check2.Checked == true)
                    check2.Checked = false;
                else
                    check2.Checked = true;
                check2.Click += check2_Click;
                return true;

                default:
                    return base.OnOptionsItemSelected (item);
                }
    }