Get current value from NumberPicker

VladimirKVVladimirKV UAMember
edited December 2017 in Xamarin.Android

Hello! I use NumberPicker for choosing age. In my project I use MVVM.
I have a label with text "Choose". In XAML I use such code for this label: Text="{Binding Age}".
In XAML I use such code for the NumberPicker: Value="{Binding Age, Mode=TwoWay}".

When I click on Label, NumberPicker becomes visible and it shows 30. Text of label doesn't change.
If I start choosing values, text of Label changes.

How can I change text of Label at once when NumberPicker becomes visible?

Answers

  • DimChrisDimChris USMember ✭✭✭
    edited December 2017

    I tried it and it works. Look my example otherwise send an example what are you trying to do.

         public class MainActivity : Activity
        {
            NumberPicker number;
            TextView text;
            protected override void OnCreate(Bundle bundle)
            {
    
                base.OnCreate(bundle);
    
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);
                text = FindViewById<TextView>(Resource.Id.textView1);
                number = FindViewById<NumberPicker>(Resource.Id.numberPicker1);
                text.Click += Text_Click;
            }
    
            private void Text_Click(object sender, EventArgs e)
            {
                number.Visibility = Android.Views.ViewStates.Visible;
                text.Text = number.Value.ToString();
            }
    
        }
    }
    

    Xml

            <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <NumberPicker
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/numberPicker1" 
                android:visibility="invisible"/>
            <TextView
                android:text="Text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView1" />
        </LinearLayout>
    
Sign In or Register to comment.