如何在Android的菜单中使用CheckBox?

How to use CheckBox in the menu in 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);
                }
        }

在尝试检索菜单中声明的​​复选框时,它显示为空。我怎样才能让它工作?

尝试通过调用 item.IsChecked 获取项目的选中状态。

请使用代码片段,这对您有帮助吗

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

你必须在你身上附加这些控件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);
                }
    }