Android - 在 datepickerdialog 中隐藏日期字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10673842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Hide date field in datepickerdialog
提问by Ishu
How to hide/remove date field in datepickerdialog. Actually my requirement is to use the native date picker dialog but it shows up with day, month, and year. But as per my requirement i need only month and year. So i want to hide or remove the day field from the view.
如何在 datepickerdialog 中隐藏/删除日期字段。实际上,我的要求是使用本机日期选择器对话框,但它会显示日、月和年。但根据我的要求,我只需要每月和每年。所以我想从视图中隐藏或删除日期字段。
Any help will be much usefull.
任何帮助都会非常有用。
采纳答案by Shankar Agarwal
Private DatePickerDialog createDialogWithoutDateField(){
DatePickerDialog dpd = new DatePickerDialog(_activity, ExpiryDateSetListener,cyear,cmonth, cday);
try{
Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
for (Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
for (Field datePickerField : datePickerFields) {
if ("mDayPicker".equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
}catch(Exception ex){
}
return dpd;
}
Use the above code for hiding the day field in DatePickerDialog.
使用上面的代码隐藏 DatePickerDialog 中的日期字段。
Refer this LINK
参考这个链接
回答by Everton Fernandes Rosario
I don't recommend using Reflection to do this kind of thing.
我不建议使用反射来做这种事情。
There is a simpler and more pretty way to do so:
有一种更简单、更漂亮的方法来做到这一点:
((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
Be aware that .getDatePicker()method from DatePickerDialogworks on API LEVEL >= 11.
请注意,该.getDatePicker()方法DatePickerDialog适用于API LEVEL >= 11.
回答by CommonsWare
The source code to DatePickerand DatePickerDialogare available for you to clone, refactor into your own package, and modify to suit.
源代码DatePicker和DatePickerDialog可供你克隆,重构到自己的包,并修改,以适应。
回答by Rohit Agrawal
((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
This works properly
这工作正常
回答by ULHAS PATIL
private Calendar mCalendar;
mCalendar = Calendar.getInstance();// Initialize Calendar
dialog_date_picker.xml
dialog_date_picker.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corners"
android:orientation="vertical"
android:padding="8dp">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:calendarViewShown="false"
android:datePickerMode="spinner" />
<Button
android:id="@+id/date_time_set"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:text="Set"
android:textColor="@color/colorWhite" /></LinearLayout>
DialogMethod called on Click:
单击时调用的 DialogMethod:
private void ExpiryDialog() {
System.out.println("Inside Dialog Box");
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_date_picker);
dialog.show();
final DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.date_picker);
date_time_set = (Button) dialog.findViewById(R.id.date_time_set);
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(1).setVisibility(View.INVISIBLE);
date_time_set.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view){
dialog.dismiss();
Integer month = datePicker.getMonth()+1;
Integer year = datePicker.getYear();
expMonth = (month.toString().length() == 1 ? "0"+month.toString():month.toString());
expYear = year.toString();
etExpiryDate.setText(expMonth + "/" + expYear);
}
});
}
This code works and hides the day in a DatePicker, so I can show Expiry Date for Cards. Enjoy!!
此代码有效并将日期隐藏在 DatePicker 中,因此我可以显示卡片的到期日期。享受!!
回答by Thinesh
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_DARK,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
txtMonth.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
datePickerDialog.show();
refer this link Disable day selection on datepicker example
请参阅此链接禁用日期选择器示例上的日期选择
回答by Tancho
I don't think you need to go that deep unless you wanna customize it.. there is the :
我不认为你需要深入,除非你想定制它..有:
mDatePicker.getDatePicker().setCalendarViewShown(false);
which is a very legitimate call to remove the calendar segment.
这是删除日历段的非常合理的调用。

