Xamarin Android Checkbox Clickable不起作用

时间:2019-01-21 08:00:24

标签: c# xamarin.android mvvmcross

我目前正在Visual Studio中使用Mvvm.cross框架和Xamarin进行项目。这是我的问题。

我有2个自定义复选框:

checkbox_approve:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_checked="true"
          android:drawable="@drawable/approved" />

  <item android:state_checked="false"
          android:drawable="@drawable/circle_unchecked" />

</selector>

checkbox_reject:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_checked="true"
          android:drawable="@drawable/rejected" />

  <item android:state_checked="false"
          android:drawable="@drawable/circle_unchecked" />

</selector>

以下是ReportResponseView.axml文件中的代码:

                  <dc.AlphaRelativeLayout 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    local:MvxBind="Click ApprovedCommand"> *//this is binding to ViewModel for different purposes*
                        <CheckBox 
                        android:id="@+id/approvedCheckBox"
                        android:button="@drawable/checkbox_approve"
                        android:clickable="false"
                        local:MvxBind="Checked IsApproved" /> *//this is binding to ViewModel for different purposes*
                    </dc.AlphaRelativeLayout>
                    <dc.AlphaRelativeLayout 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    local:MvxBind="Click RejectedCommand">
                        <CheckBox
                        android:id="@+id/rejectedCheckBox"
                        android:button="@drawable/checkbox_reject"
                        android:clickable="false"
                        local:MvxBind="Checked IsRejected" />
                    </dc.AlphaRelativeLayout>

我要的是:如果用户单击1个复选框,则第二个复选框将不可单击。这是ReportResponseView.cs:

中的代码
 protected override void InitView(View view)
        {
            approveCheckBox = (CheckBox)view.FindViewById(Resource.Id.approvedCheckBox);
            rejectCheckBox = (CheckBox)view.FindViewById(Resource.Id.rejectedCheckBox);

            approveCheckBox.CheckedChange += (s, e) =>
            {
                if (approveCheckBox.Checked)
                {
                    rejectCheckBox.Clickable = false;
                }
                else
                {
                    rejectCheckBox.Clickable = true;
                }
            };

            rejectCheckBox.CheckedChange += (s, e) =>
            {
                if (rejectCheckBox.Checked)
                {
                    approveCheckBox.Clickable = false;
                }
                else
                {
                    approveCheckBox.Clickable = true;
                }
            };
        }

approveCheckBox.Clickable对于这两个复选框均无法正常工作。当我单击进入1复选框时,第二个仍可单击。 我想知道我做错了吗?

2 个答案:

答案 0 :(得分:0)

您可以使用已启用的false属性来防止可点击

       protected override void InitView(View view)
        {
           approveCheckBox = 
          (CheckBox)view.FindViewById(Resource.Id.approvedCheckBox);
            rejectCheckBox = 
            (CheckBox)view.FindViewById(Resource.Id.rejectedCheckBox);

            approveCheckBox.CheckedChange += (s, e) =>
            {
                if (approveCheckBox.Checked)
                {
                    rejectCheckBox.Enabled = false;
                }
                else
                {
                    rejectCheckBox.Enabled = true;
                }
            };

            rejectCheckBox.CheckedChange += (s, e) =>
            {
                if (rejectCheckBox.Checked)
                {
                    approveCheckBox.Enabled = false;
                }
                else
                {
                    approveCheckBox.Enabled = true;
                }
            };
        }

答案 1 :(得分:0)

问题是我在2个click event上有AlphaRelativeLayout,其中包含2个复选框,始终允许用户单击AlphaRelativeLayout,然后使2个复选框明显可点击< / p>

这是解决方案:我在ViewModel中调整了两种方法ApprovedCommandRejectedCommand,然后只需将Click Event从AlphaRelativeLayout移到{ {1}}个属性

Checkbox