我的应用程序上有日期选择器,但我想删除/隐藏datepicker中的日期。仅显示月份和年份。注意:我使用的是xamarin android。
这是我对datepicker的扩展类:
class DatePickerFragment : DialogFragment, DatePickerDialog.IOnDateSetListener
{
// TAG can be any string of your choice.
public static readonly string TAG = "X:" + typeof(DatePickerFragment).Name.ToUpper();
// Initialize this value to prevent NullReferenceExceptions.
Action<DateTime> _dateSelectedHandler = delegate { };
public static DatePickerFragment NewInstance(Action<DateTime> onDateSelected)
{
DatePickerFragment frag = new DatePickerFragment();
frag._dateSelectedHandler = onDateSelected;
return frag;
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
DateTime currently = DateTime.Now;
DatePickerDialog dialog = new DatePickerDialog(Activity,
this,
currently.Year,
currently.Month - 1,
currently.Day);
return dialog;
}
public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
// Note: monthOfYear is a value between 0 and 11, not 1 and 12!
DateTime selectedDate = new DateTime(year, monthOfYear + 1, dayOfMonth);
Log.Debug(TAG, selectedDate.ToLongDateString());
_dateSelectedHandler(selectedDate);
}
}
这是我的Activity类:
private void DateSelect_OnClick(object sender, EventArgs eventArgs)
{
DatePickerFragment frag = DatePickerFragment.NewInstance(delegate (DateTime time)
{
_dateDisplay.Text = time.ToLongDateString();
});
frag.Show(FragmentManager, DatePickerFragment.TAG);
}
谢谢!
答案 0 :(得分:1)
从日期选择器中删除/隐藏日期
我测试Akshay Raiyani的代码,它适用于我的模拟器(Android 6.0,7.0,7.1),但它不适用于某些设备,例如我的MI 5,Android 7.0,这很奇怪。
所以我实现了DialogFragment
嵌入两个NumberPickers
,它适用于大多数Android设备。
MonthYearPickerDialog :
public class MonthYearPickerDialog : DialogFragment
{
private const int MAX_YEAR = 2099;
private DatePickerDialog.IOnDateSetListener listener;
public void setListener(DatePickerDialog.IOnDateSetListener listener)
{
this.listener = listener;
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
// Get the layout inflater
LayoutInflater inflater = Activity.LayoutInflater;
Calendar cal = Calendar.Instance;
View dialog = inflater.Inflate(Resource.Layout.date_picker_dialog, null);
NumberPicker monthPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_month);
NumberPicker yearPicker = (NumberPicker)dialog.FindViewById(Resource.Id.picker_year);
monthPicker.MinValue = 1;
monthPicker.MaxValue = 12;
monthPicker.Value = cal.Get(CalendarField.Month) + 1;
int year = cal.Get(CalendarField.Year);
yearPicker.MinValue = year;
yearPicker.MaxValue = MAX_YEAR;
yearPicker.Value = year;
builder.SetView(dialog)
.SetPositiveButton("Ok", (sender,e)=>
{
listener.OnDateSet(null, yearPicker.Value, monthPicker.Value, 0);
})
.SetNegativeButton("Cancel", (sender, e) =>
{
this.Dialog.Cancel();
});
return builder.Create();
}
}
date_picker_dialog.axml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<NumberPicker
android:id="@+id/picker_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp">
</NumberPicker>
<NumberPicker
android:id="@+id/picker_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</NumberPicker>
</LinearLayout>
</LinearLayout>
如果您想使用它:
MonthYearPickerDialog pd = new MonthYearPickerDialog();
pd.setListener(this);
pd.Show(FragmentManager, "MonthYearPickerDialog");
答案 1 :(得分:0)
检查此代码。它完美适合我
private DatePickerDialog createDialogWithoutDateField() {
myCalendar = Calendar.getInstance();
date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
dlg = new DatePickerDialog(ProfilePaymentInfo.this, date,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int day = getContext().getResources().getIdentifier("android:id/day", null, null);
myCalendar.add(Calendar.MONTH, 0);
myCalendar.add(Calendar.YEAR, 0);
dlg.getDatePicker().setMinDate(myCalendar.getTimeInMillis());
if (day != 0) {
View dayPicker = findViewById(day);
if (dayPicker != null) {
//Set Day view visibility Off/Gone
dayPicker.setVisibility(View.GONE);
}
}
}
};
return dlg;
}