Show date picker with month and year only for lollipop 5.0 in android, it can be done in lower versions but how can i do it for android 5.0.
显示日期选择器的月份和年份仅适用于Android中的棒棒糖5.0,它可以在较低版本中完成,但我怎样才能为Android 5.0做到这一点。
3 个解决方案
#1
This can be done by setting your DatePicker to spinner mode and getting the Spinner for Day and hiding it programmatically.
这可以通过将DatePicker设置为spinner模式并获取Spinner for Day并以编程方式隐藏它来完成。
Here is the XML (dialog_date_picker.xml):
这是XML(dialog_date_picker.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:padding="8dp"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:layout_weight="4"
android:layout_height="0dp"
/>
<Button
android:id="@+id/date_time_set"
android:layout_weight="1"
android:layout_width="match_parent"
android:text="Set"
android:layout_height="0dp" />
</LinearLayout>
Here is the code to do it.
这是执行此操作的代码。
private Calendar mCalendar;
...
mCalendar = Calendar.getInstance();
final View dialogView = View.inflate(this, R.layout.dialog_date_picker, null);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
datePicker.init(mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH),null);
LinearLayout ll = (LinearLayout)datePicker.getChildAt(0);
LinearLayout ll2 = (LinearLayout)ll.getChildAt(0);
ll2.getChildAt(0).setVisibility(View.INVISIBLE);
dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
mCalendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth());
alertDialog.dismiss();
}
});
#2
This should work for all android versions (api > 11)
这适用于所有Android版本(api> 11)
datePicker.findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
#3
i wrote a simple, hope it help you
我写了一个简单的,希望它能帮助你
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();
#1
This can be done by setting your DatePicker to spinner mode and getting the Spinner for Day and hiding it programmatically.
这可以通过将DatePicker设置为spinner模式并获取Spinner for Day并以编程方式隐藏它来完成。
Here is the XML (dialog_date_picker.xml):
这是XML(dialog_date_picker.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:padding="8dp"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:layout_weight="4"
android:layout_height="0dp"
/>
<Button
android:id="@+id/date_time_set"
android:layout_weight="1"
android:layout_width="match_parent"
android:text="Set"
android:layout_height="0dp" />
</LinearLayout>
Here is the code to do it.
这是执行此操作的代码。
private Calendar mCalendar;
...
mCalendar = Calendar.getInstance();
final View dialogView = View.inflate(this, R.layout.dialog_date_picker, null);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
datePicker.init(mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH),null);
LinearLayout ll = (LinearLayout)datePicker.getChildAt(0);
LinearLayout ll2 = (LinearLayout)ll.getChildAt(0);
ll2.getChildAt(0).setVisibility(View.INVISIBLE);
dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
mCalendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth());
alertDialog.dismiss();
}
});
#2
This should work for all android versions (api > 11)
这适用于所有Android版本(api> 11)
datePicker.findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
#3
i wrote a simple, hope it help you
我写了一个简单的,希望它能帮助你
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();