0
如何在設置日期選擇器時自動生成和編輯文本?使用datepicker設置日期時自動生成年齡 - Android Xamarin
我的XML文件是這樣的:
<TextView
android:text="Birthdate:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<DatePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/in_bDate"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:spinnersShown="true" />
<TextView
android:text="Age:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/in_age"
android:layout_marginBottom="10dp" />
和我的CS文件看起來像這樣:
DatePicker bDate = FindViewById<DatePicker>(Resource.Id.in_bDate);
EditText age = FindViewById<EditText>(Resource.Id.in_age);
bDate.Click += delegate {
var year = DateTime.Now.Year;
int auto_age = year - bDate.Year;
age.SetText(auto_age.ToString(), TextView.BufferType.Editable);
};
編輯:改變了方法