Android DatePicker изменится только на месяц и год
У меня есть DatePickerDialog, и я хочу видеть только месяц и год. Как я могу изменить этот код?
public void chooseDate2(View v) {
new DatePickerDialog(
act.this,
d1,
dateAndTime1.get(Calendar.YEAR) + 2,
dateAndTime1.get(Calendar.MONTH),
dateAndTime1.get(Calendar.DAY_OF_MONTH)
).show();
}
private void updateLabel2() {
scadenza.setText(fmtDateAndTime.format(dateAndTime1.getTime()));
}
DatePickerDialog.OnDateSetListener d1=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateAndTime1.set(Calendar.YEAR, year);
dateAndTime1.set(Calendar.MONTH, monthOfYear);
dateAndTime1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel2();
}
};
Спасибо
Ответы
Ответ 1
Попробуйте использовать следующий код. Он покажет DatePicker
только год и месяц (без дня)
private DatePickerDialog createDialogWithoutDateField() {
DatePickerDialog dpd = new DatePickerDialog(this, null, 2014, 1, 24);
try {
java.lang.reflect.Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
for (java.lang.reflect.Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
java.lang.reflect.Field[] datePickerFields = datePickerDialogField.getType().getDeclaredFields();
for (java.lang.reflect.Field datePickerField : datePickerFields) {
Log.i("test", datePickerField.getName());
if ("mDaySpinner".equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
}
catch (Exception ex) {
}
return dpd;
}
Этот метод возвращает диалог выбора даты. Таким образом, в вашей кнопке onClick
добавьте следующий код для отображения вашего диалога.
createDialogWithoutDateField().show();
Ответ 2
Поскольку я недавно наткнулся на эту проблему сам, я протестировал несколько решений из этого поста и похожих вопросов на Stackoverflow.
К сожалению, я не нашел рабочего решения, особенно для Android 5+
В итоге я реализовал свой собственный простой DialogFragment, встраивающий два NumberPickers. Это должно быть совместимо со всеми версиями от 3.0 и выше.
Вот код:
public class MonthYearPickerDialog extends DialogFragment {
private static final int MAX_YEAR = 2099;
private DatePickerDialog.OnDateSetListener listener;
public void setListener(DatePickerDialog.OnDateSetListener listener) {
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
Calendar cal = Calendar.getInstance();
View dialog = inflater.inflate(R.layout.date_picker_dialog, null);
final NumberPicker monthPicker = (NumberPicker) dialog.findViewById(R.id.picker_month);
final NumberPicker yearPicker = (NumberPicker) dialog.findViewById(R.id.picker_year);
monthPicker.setMinValue(0);
monthPicker.setMaxValue(11);
monthPicker.setValue(cal.get(Calendar.MONTH));
int year = cal.get(Calendar.YEAR);
yearPicker.setMinValue(year);
yearPicker.setMaxValue(MAX_YEAR);
yearPicker.setValue(year);
builder.setView(dialog)
// Add action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
listener.onDateSet(null, yearPicker.getValue(), monthPicker.getValue(), 0);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MonthYearPickerDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
И макет
<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(getFragmentManager(), "MonthYearPickerDialog");
Ответ 3
Я не рекомендую использовать Reflection для этого.
Существует более простой и приятный способ сделать это:
((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
Помните, что метод .getDatePicker()
из DatePickerDialog
работает на API LEVEL >= 11
.
Кроме того, он не работает с API LEVEL >= 21.
Ответ 4
Дополнительная форвардная форма ответа Stephan Klein.
Поскольку у меня есть требование сделать Год необязательным. И я также обрабатывал дату как февраль 28, а также обрабатывал високосный год.
MonthYearPickerDialog
public class MonthYearPickerDialog extends DialogFragment {
private DatePickerDialog.OnDateSetListener listener;
private int daysOfMonth = 31;
private NumberPicker monthPicker;
private NumberPicker yearPicker;
private NumberPicker dayPicker;
private Calendar cal = Calendar.getInstance();
public static final String MONTH_KEY = "monthValue";
public static final String DAY_KEY = "dayValue";
public static final String YEAR_KEY = "yearValue";
int monthVal = -1 , dayVal = -1 , yearVal =-1 ;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getArguments();
if(extras != null){
monthVal = extras.getInt(MONTH_KEY , -1);
dayVal = extras.getInt(DAY_KEY , -1);
yearVal = extras.getInt(YEAR_KEY , -1);
}
}
public static MonthYearPickerDialog newInstance(int monthIndex , int daysIndex , int yearIndex) {
MonthYearPickerDialog f = new MonthYearPickerDialog();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt(MONTH_KEY, monthIndex);
args.putInt(DAY_KEY, daysIndex);
args.putInt(YEAR_KEY, yearIndex);
f.setArguments(args);
return f;
}
public void setListener(DatePickerDialog.OnDateSetListener listener) {
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//getDialog().setTitle("Add Birthday");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialog = inflater.inflate(R.layout.month_year_picker, null);
monthPicker = (NumberPicker) dialog.findViewById(R.id.picker_month);
yearPicker = (NumberPicker) dialog.findViewById(R.id.picker_year);
dayPicker = (NumberPicker) dialog.findViewById(R.id.picker_day);
monthPicker.setMinValue(1);
monthPicker.setMaxValue(12);
if(monthVal != -1)// && (monthVal > 0 && monthVal < 13))
monthPicker.setValue(monthVal);
else
monthPicker.setValue(cal.get(Calendar.MONTH) + 1);
monthPicker.setDisplayedValues(new String[]{"Jan","Feb","Mar","Apr","May","June","July",
"Aug","Sep","Oct","Nov","Dec"});
dayPicker.setMinValue(1);
dayPicker.setMaxValue(daysOfMonth);
if(dayVal != -1)
dayPicker.setValue(dayVal);
else
dayPicker.setValue(cal.get(Calendar.DAY_OF_MONTH));
monthPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
switch (newVal){
case 1:case 3:case 5:
case 7:case 8:case 10:
case 12:
daysOfMonth = 31;
dayPicker.setMaxValue(daysOfMonth);
break;
case 2:
daysOfMonth = 28;
dayPicker.setMaxValue(daysOfMonth);
break;
case 4:case 6:
case 9:case 11:
daysOfMonth = 30;
dayPicker.setMaxValue(daysOfMonth);
break;
}
}
});
int maxYear = cal.get(Calendar.YEAR);//2016
final int minYear = 1916;//1997;
int arraySize = maxYear - minYear;
String[] tempArray = new String[arraySize];
tempArray[0] = "---";
int tempYear = minYear+1;
for(int i=0 ; i < arraySize; i++){
if(i != 0){
tempArray[i] = " " + tempYear + "";
}
tempYear++;
}
Log.i("", "onCreateDialog: " + tempArray.length);
yearPicker.setMinValue(minYear+1);
yearPicker.setMaxValue(maxYear);
yearPicker.setDisplayedValues(tempArray);
if(yearVal != -1)
yearPicker.setValue(yearVal);
else
yearPicker.setValue(tempYear -1);
yearPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
try {
if(isLeapYear(picker.getValue())){
daysOfMonth = 29;
dayPicker.setMaxValue(daysOfMonth);
}
}catch (Exception e){
e.printStackTrace();
}
}
});
builder.setView(dialog)
// Add action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
int year = yearPicker.getValue();
if(year == (minYear+1)){
year = 1904;
}
listener.onDateSet(null, year, monthPicker.getValue(), dayPicker.getValue());
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MonthYearPickerDialog.this.getDialog().cancel();
}
});
return builder.create();
}
public static boolean isLeapYear(int year) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
return cal.getActualMaximum(Calendar.DAY_OF_YEAR) > 365;
}
}
И я называю это как
Calendar calendar = Calendar.getInstance();
if(etBirthday.getText().length()> 0 ){
if(checkIsYearAvailable(etBirthday.getText().toString().trim()))
calendar = DateTimeOp.getCalendarFromFormat(etBirthday.getText().toString().trim(), Constants.dateFormat21);
else
calendar = DateTimeOp.getCalendarFromFormat(etBirthday.getText().toString().trim() + ", 1917",Constants.dateFormat21);
}
MonthYearPickerDialog pd = MonthYearPickerDialog.newInstance(calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.YEAR));
pd.setListener(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
String formatedDate = "";
if(selectedYear == 1904)
{
String currentDateFormat = selectedMonth + "/" + selectedDay;// + "/" + selectedYear; //"MM/dd/yyyy"
formatedDate = DateTimeOp.oneFormatToAnother(currentDateFormat, Constants.dateFormat20, Constants.dateFormat24);
}
else{
String currentDateFormat = selectedMonth + "/" + selectedDay + "/" + selectedYear; //"MM/dd/yyyy"
formatedDate = DateTimeOp.oneFormatToAnother(currentDateFormat, Constants.dateFormat0, Constants.dateFormat21);
}
etBirthday.setText(formatedDate);
}
});
pd.show(getFragmentManager(), "MonthYearPickerDialog");
month_year_picker.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_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
android:id="@+id/picker_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp" />
<NumberPicker
android:id="@+id/picker_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Ответ 5
Ответ "Xar E Ahmer" очень прост и соответствует требованию. Я использовал то же самое со следующими модификациями согласно моему требованию.
- Kotlin преобразование "Xar E Ahmer" Ответ.
- Добавлено несколько случаев обработки ошибок.
- Больше сдачи 28/29 февраля в високосный год.
- Каждый год флажок для исключения года (упрощено для пользователя).
MonthYearPickerDialog.kt
package com.example.*******
import android.app.AlertDialog
import android.app.DatePickerDialog
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.NumberPicker
import android.app.DialogFragment
import java.util.Calendar
class MonthYearPickerDialog : DialogFragment() {
private var listener: DatePickerDialog.OnDateSetListener? = null
private var daysOfMonth = 31
private var monthPicker: NumberPicker? = null
private var yearPicker: NumberPicker? = null
private var dayPicker: NumberPicker? = null
private var isEveryYearcheckBox: CheckBox? = null
private val cal = Calendar.getInstance()
internal var monthVal = -1
internal var dayVal = -1
internal var yearVal = -1
internal var maxYearVal = -1
internal var minYearVal = -1
override fun onAttach(context: Context) {
super.onAttach(context)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val extras = arguments
if (extras != null) {
monthVal = extras.getInt(MONTH_KEY, -1)
dayVal = extras.getInt(DAY_KEY, -1)
yearVal = extras.getInt(YEAR_KEY, -1)
maxYearVal = extras.getInt(MAX_YEAR_KEY, -1)
minYearVal = extras.getInt(MIN_YEAR_KEY, -1)
}
maxYearVal = if (maxYearVal == -1) 2025 else maxYearVal
minYearVal = if (minYearVal == -1) 1925 else minYearVal
if (minYearVal > maxYearVal) {
val tempVal = maxYearVal
maxYearVal = minYearVal
minYearVal = tempVal
}
}
fun setListener(listener: DatePickerDialog.OnDateSetListener) {
this.listener = listener
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity)
// Get the layout inflater
val inflater = activity.layoutInflater
val dialog = inflater.inflate(R.layout.month_year_picker, null)
monthPicker = dialog.findViewById<View>(R.id.datepicker_month) as NumberPicker
yearPicker = dialog.findViewById<View>(R.id.datepicker_year) as NumberPicker
dayPicker = dialog.findViewById<View>(R.id.datepicker_day) as NumberPicker
isEveryYearcheckBox = dialog.findViewById<View>(R.id.datepicker_isyearcheckBox) as CheckBox
isEveryYearcheckBox!!.setOnCheckedChangeListener { compoundButton, b ->
if (b) {
yearPicker!!.isEnabled = false
yearPicker!!.value = minYearVal - 1
} else {
yearPicker!!.isEnabled = true
if (yearVal != -1 && yearVal != 1904)
yearPicker!!.value = yearVal
else {
yearPicker!!.value = cal.get(Calendar.YEAR)
}
}
if (monthPicker!!.value == 2) {
daysOfMonth = 28
if (isLeapYear(yearPicker!!.value)) {
daysOfMonth = 29
}
dayPicker!!.maxValue = daysOfMonth
}
}
monthPicker!!.minValue = 1
monthPicker!!.maxValue = 12
if (monthVal != -1)
monthPicker!!.value = monthVal
else
monthPicker!!.value = cal.get(Calendar.MONTH) + 1
monthPicker!!.displayedValues = arrayOf("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec")
dayPicker!!.minValue = 1
dayPicker!!.maxValue = daysOfMonth
if (dayVal != -1)
dayPicker!!.value = dayVal
else
dayPicker!!.value = cal.get(Calendar.DAY_OF_MONTH)
monthPicker!!.setOnValueChangedListener { picker, oldVal, newVal ->
when (newVal) {
1, 3, 5, 7, 8, 10, 12 -> {
daysOfMonth = 31
dayPicker!!.maxValue = daysOfMonth
}
2 -> {
daysOfMonth = 28
if (isLeapYear(yearPicker!!.value)) {
daysOfMonth = 29
}
dayPicker!!.maxValue = daysOfMonth
}
4, 6, 9, 11 -> {
daysOfMonth = 30
dayPicker!!.maxValue = daysOfMonth
}
}
}
val maxYear = maxYearVal
val minYear = minYearVal
val arraySize = maxYear - minYear + 2
val tempArray = arrayOfNulls<String>(arraySize)
tempArray[0] = "---"
var tempYear = minYear - 1
for (i in 0 until arraySize) {
if (i != 0) {
tempArray[i] = " $tempYear"
}
tempYear++
}
Log.i("", "onCreateDialog: " + tempArray.size)
yearPicker!!.minValue = minYear - 1
yearPicker!!.maxValue = maxYear
yearPicker!!.displayedValues = tempArray
if (yearVal != -1 && yearVal != 1904) {
yearPicker!!.value = yearVal
} else {
isEveryYearcheckBox!!.isChecked = false
yearPicker!!.isEnabled = false
yearPicker!!.value = minYear - 1
}
if (monthPicker!!.value == 2) {
daysOfMonth = 28
if (isLeapYear(yearPicker!!.value)) {
daysOfMonth = 29
}
dayPicker!!.maxValue = daysOfMonth
}
yearPicker!!.setOnValueChangedListener { picker, oldVal, newVal ->
try {
daysOfMonth = 28
if (isLeapYear(picker.value)) {
daysOfMonth = 29
}
dayPicker!!.maxValue = daysOfMonth
} catch (e: Exception) {
e.printStackTrace()
}
}
builder.setView(dialog)
// Add action buttons
.setPositiveButton(R.string.ok) { dialog, id ->
var year = yearPicker!!.value
if (year == minYear - 1) {
year = 1904
}
listener!!.onDateSet(null, year, monthPicker!!.value, dayPicker!!.value)
}
.setNegativeButton(R.string.cancel) { dialog, id -> [email protected]() }
return builder.create()
}
companion object {
val MONTH_KEY = "monthValue"
val DAY_KEY = "dayValue"
val YEAR_KEY = "yearValue"
val MAX_YEAR_KEY = "maxyearValue"
val MIN_YEAR_KEY = "minyearValue"
fun newInstance(monthIndex: Int, daysIndex: Int, yearIndex: Int, maxYearIndex: Int, minYearIndex: Int): MonthYearPickerDialog {
val f = MonthYearPickerDialog()
// Supply num input as an argument.
val args = Bundle()
args.putInt(MONTH_KEY, monthIndex)
args.putInt(DAY_KEY, daysIndex)
args.putInt(YEAR_KEY, yearIndex)
args.putInt(MAX_YEAR_KEY, maxYearIndex)
args.putInt(MIN_YEAR_KEY, minYearIndex)
f.arguments = args
return f
}
fun isLeapYear(year: Int): Boolean {
val cal = Calendar.getInstance()
cal.set(Calendar.YEAR, year)
return cal.getActualMaximum(Calendar.DAY_OF_YEAR) > 365
}
}
}
month_year_picker.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CheckBox
android:id="@+id/datepicker_isyearcheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:layout_gravity="center"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:text="Every year" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<NumberPicker
android:id="@+id/datepicker_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp" />
<NumberPicker
android:id="@+id/datepicker_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp" />
<NumberPicker
android:id="@+id/datepicker_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Пример использования с различными тестовыми примерами в JAVA
Calendar calendar = Calendar.getInstance();
// current date
MonthYearPickerDialog pd = MonthYearPickerDialog.Companion.newInstance(calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.YEAR), -1, -1);
// SSTODO for testing
// no year
//MonthYearPickerDialog pd = MonthYearPickerDialog.newInstance(calendar.get(Calendar.MONTH) + 1,
// calendar.get(Calendar.DAY_OF_MONTH), -1, -1, -1);
// minimum year 2020
//MonthYearPickerDialog pd = MonthYearPickerDialog.newInstance(calendar.get(Calendar.MONTH) + 1,
// calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.YEAR), -1, 2020);
// maximum year 2018
//MonthYearPickerDialog pd = MonthYearPickerDialog.newInstance(calendar.get(Calendar.MONTH) + 1,
// calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.YEAR), 2018, -1);
// maximum year 2019, minimum year 2019
//MonthYearPickerDialog pd = MonthYearPickerDialog.newInstance(calendar.get(Calendar.MONTH) + 1,
// calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.YEAR), 2019, 2019);
// maximum year 100, minimum year 5000
//MonthYearPickerDialog pd = MonthYearPickerDialog.newInstance(calendar.get(Calendar.MONTH) + 1,
// calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.YEAR), 100, 5000);
// maximum year 100, minimum year 5000, year 99
//MonthYearPickerDialog pd = MonthYearPickerDialog.newInstance(calendar.get(Calendar.MONTH) + 1,
// calendar.get(Calendar.DAY_OF_MONTH), 99, 100, 5000);
pd.setListener(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
String formatedDate = "";
String currentDateFormat = "";
if(selectedYear == 1904) // no year
{
currentDateFormat = selectedMonth + "/" + selectedDay; //"MM/dd"
}
else {
currentDateFormat = selectedMonth + "/" + selectedDay + "/" + selectedYear; //"MM/dd/yyyy"
}
Toast.makeText(MainActivityTestOne.this, "Selected Date ( MM/dd or. MM/dd/yyyy ) : " + currentDateFormat, Toast.LENGTH_LONG).show();
}
});
pd.show(getFragmentManager(), STRING_DATE_PICKER);
Ответ 6
Это старый вопрос, но теперь вы можете просто использовать "BetterPickers":
https://github.com/code-troopers/android-betterpickers
И используйте ExpirationPicker.
Надеюсь, что это решает это для некоторых других потерянных душ, которые искали Google по всему миру.
Ответ 7
он отлично работает API < 21.
public class MonPickerDialog extends DatePickerDialog {
public MonPickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
this.setTitle(year + " / " + (monthOfYear + 1));
int field = TFKBApp.getInstance().getApplicationLanguage().equals(getContext().getString(R.string.turkish_tr)) ? 0 : 1; // it change depend on language date type.
((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(field).setVisibility(View.GONE);
}
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
super.onDateChanged(view, year, month, day);
this.setTitle(year + " / " + (month + 1));
}
}
календарь, вызванный в классе.
public void showMonPicker() {
final Calendar localCalendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
try {
Date date = sdf.parse("2013/08");
localCalendar.setTime(date);
} catch (Exception e) {
}
new MonPickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
localCalendar.set(Calendar.YEAR, year);
localCalendar.set(Calendar.MONTH, monthOfYear);
fragTaxOthersInfoTaxDate.setText(year + "/" + monthOfYear);
}
}, localCalendar.get(Calendar.YEAR), localCalendar.get(Calendar.MONTH), localCalendar.get(Calendar.DATE)).show();
}
Ответ 8
вам нужно создать новый компонент.
MonthYearDialog.java
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import com.pozitron.commons.customviews.ButtonFont;
import com.pozitron.commons.customviews.TextViewFont;
import com.pozitron.commons.utils.DeviceUtil;
import com.pozitron.tfkb.R;
import com.pozitron.tfkb.core.dialog.TFKBBaseDialog;
import java.text.DateFormatSymbols;
import java.util.Calendar;
/**
* Created by so12607 on 28/11/2017.
*/
public class MonthYearDialog extends TFKBBaseDialog implements View.OnClickListener {
private TextViewFont dialogMonthYearOnlyYear;
private TextViewFont dialogYearHeader;
private TextViewFont dialogMonthHeader;
private TextViewFont dialogMonthYearJanuary;
private TextViewFont dialogMonthYearFebruary;
private TextViewFont dialogMonthYearMarch;
private TextViewFont dialogMonthYearApril;
private TextViewFont dialogMonthYearMay;
private TextViewFont dialogMonthYearJune;
private TextViewFont dialogMonthYearJuly;
private TextViewFont dialogMonthYearAugust;
private TextViewFont dialogMonthYearSeptember;
private TextViewFont dialogMonthYearOctober;
private TextViewFont dialogMonthYearNovember;
private TextViewFont dialogMonthYearDecember;
public static int year, month, constantYear;
private MonthYearDialog.OnDoneButtonClickListener listener;
private Context context;
private static MonthYearDialog monthYearDialog;
public interface OnDoneButtonClickListener {
void onDoneButtonClick();
}
public static MonthYearDialog getInstance(Context context, final MonthYearDialog.OnDoneButtonClickListener listener) {
return monthYearDialog = monthYearDialog == null ? new MonthYearDialog(context, listener) : monthYearDialog;
}
public MonthYearDialog(Context context, final MonthYearDialog.OnDoneButtonClickListener listener) {
super(context, R.style.FullScreenDialog_Floating);
this.context = context;
this.listener = listener;
setCancelable(true);
setCanceledOnTouchOutside(true);
initView(context, R.layout.dialog_month_year);
year = Calendar.getInstance().get(Calendar.YEAR);
constantYear = Calendar.getInstance().get(Calendar.YEAR);
month = Calendar.getInstance().get(Calendar.MONTH) + 1;
dialogYearHeader = (TextViewFont) findViewById(R.id.dialogYearHeader);
dialogMonthHeader = (TextViewFont) findViewById(R.id.dialogMonthHeader);
dialogMonthYearOnlyYear = (TextViewFont) findViewById(R.id.dialogMonthYearOnlyYear);
dialogMonthYearOnlyYear.setText(String.valueOf(year));
dialogMonthYearJanuary = (TextViewFont) findViewById(R.id.dialogMonthYearJanuary);
dialogMonthYearFebruary = (TextViewFont) findViewById(R.id.dialogMonthYearFebruary);
dialogMonthYearMarch = (TextViewFont) findViewById(R.id.dialogMonthYearMarch);
dialogMonthYearApril = (TextViewFont) findViewById(R.id.dialogMonthYearApril);
dialogMonthYearMay = (TextViewFont) findViewById(R.id.dialogMonthYearMay);
dialogMonthYearJune = (TextViewFont) findViewById(R.id.dialogMonthYearJune);
dialogMonthYearJuly = (TextViewFont) findViewById(R.id.dialogMonthYearJuly);
dialogMonthYearAugust = (TextViewFont) findViewById(R.id.dialogMonthYearAugust);
dialogMonthYearSeptember = (TextViewFont) findViewById(R.id.dialogMonthYearSeptember);
dialogMonthYearOctober = (TextViewFont) findViewById(R.id.dialogMonthYearOctober);
dialogMonthYearNovember = (TextViewFont) findViewById(R.id.dialogMonthYearNovember);
dialogMonthYearDecember = (TextViewFont) findViewById(R.id.dialogMonthYearDecember);
dialogMonthYearJanuary.setOnClickListener(this);
dialogMonthYearFebruary.setOnClickListener(this);
dialogMonthYearMarch.setOnClickListener(this);
dialogMonthYearApril.setOnClickListener(this);
dialogMonthYearMay.setOnClickListener(this);
dialogMonthYearJune.setOnClickListener(this);
dialogMonthYearJuly.setOnClickListener(this);
dialogMonthYearAugust.setOnClickListener(this);
dialogMonthYearSeptember.setOnClickListener(this);
dialogMonthYearOctober.setOnClickListener(this);
dialogMonthYearNovember.setOnClickListener(this);
dialogMonthYearDecember.setOnClickListener(this);
((ImageButton) findViewById(R.id.dialogMonthYearLeftButton)).setOnClickListener(this);
((ImageButton) findViewById(R.id.dialogMonthYearRightButton)).setOnClickListener(this);
((ButtonFont) findViewById(R.id.dialogMonthYearCancel)).setOnClickListener(this);
((ButtonFont) findViewById(R.id.dialogMonthYearOk)).setOnClickListener(this);
initializeMonths();
setDate();
setMonth(month);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = DeviceUtil.getDeviceWidth(context) - DeviceUtil.pxToDp(context, 2 * 16);
getWindow().setAttributes(params);
}
private void initializeMonths() {
String[] months = new DateFormatSymbols().getShortMonths();
dialogMonthYearJanuary.setText(months[0]);
dialogMonthYearFebruary.setText(months[1]);
dialogMonthYearMarch.setText(months[2]);
dialogMonthYearApril.setText(months[3]);
dialogMonthYearMay.setText(months[4]);
dialogMonthYearJune.setText(months[5]);
dialogMonthYearJuly.setText(months[6]);
dialogMonthYearAugust.setText(months[7]);
dialogMonthYearSeptember.setText(months[8]);
dialogMonthYearOctober.setText(months[9]);
dialogMonthYearNovember.setText(months[10]);
dialogMonthYearDecember.setText(months[11]);
}
private void setDate() {
dialogYearHeader.setText(String.valueOf(year));
String[] months = new DateFormatSymbols().getMonths();
dialogMonthHeader.setText(months[month - 1]);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.dialogMonthYearLeftButton:
if (1990 < year) {
--year;
}
dialogMonthYearOnlyYear.setText(String.valueOf(year));
break;
case R.id.dialogMonthYearRightButton:
if (constantYear > year) {
++year;
}
dialogMonthYearOnlyYear.setText(String.valueOf(year));
break;
case R.id.dialogMonthYearJanuary:
month = 1;
setMonth(dialogMonthYearJanuary);
break;
case R.id.dialogMonthYearFebruary:
month = 2;
setMonth(dialogMonthYearFebruary);
break;
case R.id.dialogMonthYearMarch:
month = 3;
setMonth(dialogMonthYearMarch);
break;
case R.id.dialogMonthYearApril:
month = 4;
setMonth(dialogMonthYearApril);
break;
case R.id.dialogMonthYearMay:
month = 5;
setMonth(dialogMonthYearMay);
break;
case R.id.dialogMonthYearJune:
month = 6;
setMonth(dialogMonthYearJune);
break;
case R.id.dialogMonthYearJuly:
month = 7;
setMonth(dialogMonthYearJuly);
break;
case R.id.dialogMonthYearAugust:
month = 8;
setMonth(dialogMonthYearAugust);
break;
case R.id.dialogMonthYearSeptember:
month = 9;
setMonth(dialogMonthYearSeptember);
break;
case R.id.dialogMonthYearOctober:
month = 10;
setMonth(dialogMonthYearOctober);
break;
case R.id.dialogMonthYearNovember:
month = 11;
setMonth(dialogMonthYearNovember);
break;
case R.id.dialogMonthYearDecember:
month = 12;
setMonth(dialogMonthYearDecember);
break;
case R.id.dialogMonthYearOk:
listener.onDoneButtonClick();
dismiss();
break;
case R.id.dialogMonthYearCancel:
dismiss();
break;
}
setDate();
}
private void setMonth(TextViewFont monthItem) {
dialogMonthYearJanuary.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearFebruary.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearMarch.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearApril.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearMay.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearJune.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearJuly.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearAugust.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearSeptember.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearOctober.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearNovember.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearDecember.setTextColor(context.getResources().getColor(R.color.black));
dialogMonthYearJanuary.setBackgroundResource(R.drawable.circle_item_white);
dialogMonthYearFebruary.setBackgroundResource(R.drawable.circle_item_white);
dialogMonthYearMarch.setBackgroundResource(R.drawable.circle_item_white);
dialogMonthYearApril.setBackgroundResource(R.drawable.circle_item_white);
dialogMonthYearMay.setBackgroundResource(R.drawable.circle_item_white);
dialogMonthYearJune.setBackgroundResource(R.drawable.circle_item_white);
dialogMonthYearJuly.setBackgroundResource(R.drawable.circle_item_white);
dialogMonthYearAugust.setBackgroundResource(R.drawable.circle_item_white);
dialogMonthYearSeptember.setBackgroundResource(R.drawable.circle_item_white);
dialogMonthYearOctober.setBackgroundResource(R.drawable.circle_item_white);
dialogMonthYearNovember.setBackgroundResource(R.drawable.circle_item_white);
dialogMonthYearDecember.setBackgroundResource(R.drawable.circle_item_white);
monthItem.setBackgroundResource(R.drawable.circle_item);
monthItem.setTextColor(context.getResources().getColor(R.color.white));
}
private void setMonth(int month) {
switch (month) {
case 1:
setMonth(dialogMonthYearJanuary);
break;
case 2:
setMonth(dialogMonthYearFebruary);
break;
case 3:
setMonth(dialogMonthYearMarch);
break;
case 4:
setMonth(dialogMonthYearApril);
break;
case 5:
setMonth(dialogMonthYearMay);
break;
case 6:
setMonth(dialogMonthYearJune);
break;
case 7:
setMonth(dialogMonthYearJuly);
break;
case 8:
setMonth(dialogMonthYearAugust);
break;
case 9:
setMonth(dialogMonthYearSeptember);
break;
case 10:
setMonth(dialogMonthYearOctober);
break;
case 11:
setMonth(dialogMonthYearNovember);
break;
case 12:
setMonth(dialogMonthYearDecember);
break;
}
}
private void initView(Context context, int layoutResId) {
setContentView(layoutResId);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = DeviceUtil.getDeviceWidth(context) - DeviceUtil.pxToDp(context, 2 * 16);
getWindow().setAttributes(params);
}
}
dialog_month_year.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="16dp"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:gravity="center"
android:background="@color/colorPrimary2"
android:orientation="vertical">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogYearHeader"
android:textSize="16dp"
android:layout_width="match_parent"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="2017"
android:textColor="@color/background" />
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthHeader"
android:textSize="24dp"
android:paddingBottom="5dp"
android:layout_width="match_parent"
android:background="@color/colorPrimary2"
android:text="Agustos"
android:textColor="@color/white" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="@+id/dialogMonthYearLeftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="4dp"
android:background="@null"
android:onClick="onClick"
android:src="@drawable/ic_keyboard_arrow_left" />
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearOnlyYear"
android:textSize="14dp"
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center"
tools:text="2017" />
<ImageButton
android:id="@+id/dialogMonthYearRightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="4dp"
android:background="@null"
android:onClick="onClick"
android:src="@drawable/ic_keyboard_arrow_right" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearJanuary"
style="@style/TextViewLight.month"
android:text="Ock" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearFebruary"
style="@style/TextViewLight.month"
android:text="Şub" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearMarch"
style="@style/TextViewLight.month"
android:text="Mar" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearApril"
style="@style/TextViewLight.month"
android:text="Nis" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearMay"
style="@style/TextViewLight.month"
android:text="May" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearJune"
style="@style/TextViewLight.month"
android:text="Haz" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearJuly"
style="@style/TextViewLight.month"
android:text="Tem" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearAugust"
style="@style/TextViewLight.month"
android:text="Aug" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearSeptember"
style="@style/TextViewLight.month"
android:text="Eyl" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearOctober"
style="@style/TextViewLight.month"
android:text="Ekm" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearNovember"
style="@style/TextViewLight.month"
android:text="Kas" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<com.pozitron.commons.customviews.TextViewFont
android:id="@+id/dialogMonthYearDecember"
style="@style/TextViewLight.month"
android:text="Ark" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="horizontal">
<com.pozitron.commons.customviews.ButtonFont
android:id="@+id/dialogMonthYearCancel"
style="@style/TextViewLight.16"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_weight="5"
android:background="@color/white"
android:text="@string/cancel"
android:textColor="@color/colorPrimary2" />
<com.pozitron.commons.customviews.ButtonFont
android:id="@+id/dialogMonthYearOk"
style="@style/TextViewLight.16"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_weight="4"
android:background="@color/white"
android:text="@string/ok"
android:textColor="@color/colorPrimary2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
как вызвать сборщик
new MonthYearDialog(context, new MonthYearDialog.OnDoneButtonClickListener() {
@Override
public void onDoneButtonClick() {
month = String.valueOf(MonthYearDialog.month);
year = String.valueOf(MonthYearDialog.year);
layoutDateText.setText(getFormattedDate());
layoutDateText.setError(null);
if (listener != null) {
listener.onClick(v);
}
}
}).show();
![Month and year datepicker]()
Ответ 9
Это работает для меня хорошо:
DatePickerDialog monthDatePickerDialog = new DatePickerDialog(activity,
AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
monthTextView.setText(year + "/" + (month + 1));
}
}, yearNow, monthNow, dayNow){
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getDatePicker().findViewById(getResources().getIdentifier("day","id","android")).setVisibility(View.GONE);
}
};
monthDatePickerDialog.setTitle("select_month");
monthDatePickerDialog.show();