在Xamarin Android中将数字转换为字符串的SetFormatter方法实现

jtjikinw  于 2022-11-20  发布在  Android
关注(0)|答案(1)|浏览(180)

下面我的示例代码在OnCreateDialog事件中调用下面的方法

SetNumericMonth(true, null, dialog);

private int monthSpinnerResId;
        public void SetNumericMonth(bool onCreateEventOrNot, DatePicker view, DatePickerDialog dialog)
        {
            if (DatePickerNumericMonthFormat == false)
            {
                monthSpinnerResId = Activity.Resources.GetIdentifier("android:id/month", null, null);
                if (monthSpinnerResId != 0)
                {
                    var numberPicker = (NumberPicker)dialog.DatePicker.FindViewById<NumberPicker>(monthSpinnerResId);
                    numberPicker.SetFormatter(new NumericMonthFormatter());
                    numberPicker.MinValue = 0;
                    numberPicker.MaxValue = 9;
                }
            }
        }

public class NumericMonthFormatter: Java.Lang.Object, NumberPicker.IFormatter
     {
         public string Format1(int value)
         {
             return string.Format("{0:D2}", value);
         }
     }
e4yzc0pl

e4yzc0pl1#

您可以尝试建立DatePickerDialog,并设定月份编号选择器的显示值。例如:

public class MyDialog : DatePickerDialog
    {
        public MyDialog(Context context) : base(context)
        {
        }
        public override void OnDateChanged(DatePicker view, int year, int month, int dayOfMonth)
        {
            base.OnDateChanged(view, year, month, dayOfMonth);
            var monthSpinnerResId = Android.App.Application.Context.Resources.GetIdentifier("android:id/month", null, null);
            var numberPicker = (NumberPicker)(this.DatePicker.FindViewById<NumberPicker>(monthSpinnerResId));
            string[] arrays = new string[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
            numberPicker.SetDisplayedValues(arrays);
        }
    }

而在主要活动中:

MyDialog dialog = new MyDialog(this);
dialog.Show();

而且还有一些点适合你的代码:
1.方法名称应为Format而非Format 1:

public class NumericMonthFormatter: Java.Lang.Object, NumberPicker.IFormatter
     {
         public string Format(int value)
         {
             return string.Format("{0:D2}", value);
         }
     }
  1. DatePickerDialog在比Api 21更高的Android版本中的默认模式是日历,因此您需要将其更改为微调器。
    在styles.xml中:
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
            <item name="android:datePickerStyle">@style/MySpinnerDatePicker</item>
    </style>
      <style name="MySpinnerDatePicker" parent="android:Widget.Material.DatePicker">
            <item name="android:datePickerMode">spinner</item>
      </style>
</resources>

如果默认样式为日历,则var numberPicker = (NumberPicker)dialog.DatePicker.FindViewById<NumberPicker>(monthSpinnerResId);将为空。
另外,如果只使用格式化程序,当日期改变时,显示值将被重置。所以我尝试使用自定义的日期选择器对话框。

相关问题