Search code examples
c#xmlxamarinmvvmcross

EditText Edit Feature in MVVMCross


I wonder how could I able to enable and disable edit feature in the EditText in mvvmcross.

<EditText
   style="@style/InputNumbersEditText"
   android:layout_weight="1"
   android:layout_width="0dp"
   android:focusable="true"
   android:layout_height="wrap_content"
   android:inputType="numberDecimal|numberSigned"
   local:MvxBind="Text Age" />

Solution

  • Since android:editable="false" is deprecated, you should set android:inputType="none" to disable input on the EditText. If you're looking to bind the inputType of the EditText with MvvmCross, you can create a Value Converter which takes an input value from your ViewModel, and returns an answer of type Android.Text.InputTypes.

    Example implementation:

    Add a class to your Android project with the following in it:

    public class EditTextEnabledValueConverter : MvxValueConverter<bool, InputTypes>
    {
        protected override InputTypes Convert(bool value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value)
                return InputTypes.ClassNumber | InputTypes.NumberFlagDecimal | InputTypes.NumberFlagSigned;
    
            return InputTypes.Null;
        }
    }
    

    and in your layout file:

    <EditText
       style="@style/InputNumbersEditText"
       android:layout_weight="1"
       android:layout_width="0dp"
       android:focusable="true"
       android:layout_height="wrap_content"
       local:MvxBind="Text Age; InputType EditTextEnabled(MyProperty)" />
    

    Where MyProperty is a bindable boolean on your ViewModel. You can use any type as a source type, it doesn't have to be a boolean. Happy converting!