我试图从日期选择器小部件中隐藏年份字段。这可能看起来像一个重复的问题但是之前问题的答案并没有帮助我隐藏年份字段..
这是我的代码::( onCreate)
int month = 0;
int year = 0;
int day = 0;
OnDateSetListener datePickerListener = null;
CustomDatePickerDialog dp = new CustomDatePickerDialog(getBaseContext(), android.R.style.Theme_Holo_Light_Dialog, datePickerListener, year, month, day);
DatePickerDialog obj = dp.getPicker();
picker = (DatePicker) findViewById(R.id.datePicker1);
try {
java.lang.reflect.Field[] f = obj.getClass().getDeclaredFields();
// Field f[] = picker.getClass().getDeclaredFields();
for (java.lang.reflect.Field field : f) {
if (field.getName().equals("prabs")) {
field.setAccessible(true);
DatePicker datePicker = (DatePicker) field.get(obj);
java.lang.reflect.Field datePickerFields[] = obj.getClass().getDeclaredFields();
for (java.lang.reflect.Field datePickerField : datePickerFields) {
if ("mYearPicker".equals(datePickerField.getName()) || "mYearSpinner".equals(datePickerField
.getName()))
{
datePickerField.setAccessible(true);
Object yearPicker = new Object();
yearPicker = datePickerField.get(datePicker);
((View) yearPicker).setVisibility(View.GONE);
}
}
/* Object yearPicker = new Object();
yearPicker = field.get(picker);
((View) yearPicker).setVisibility(View.GONE);*/
}
}
}
catch (SecurityException e) {
Log.d("ERROR", e.getMessage());
}
catch (IllegalArgumentException e) {
Log.d("ERROR", e.getMessage());
}
catch (IllegalAccessException e) {
Log.d("ERROR", e.getMessage());
}
obj.show();
在主类::
之外class CustomDatePickerDialog extends DatePickerDialog implements OnDateChangedListener
{
private DatePickerDialog prabs;
@SuppressLint("NewApi")
public CustomDatePickerDialog(Context context, int theme,
OnDateSetListener listener, int year, int monthOfYear,
int dayOfMonth)
{
super(context, theme, listener, year, monthOfYear, dayOfMonth);
// TODO Auto-generated constructor stub
prabs = new DatePickerDialog(context, theme, listener, year, monthOfYear, dayOfMonth);
prabs.getDatePicker().init(2015, 01, 8, this);
updateTitle(monthOfYear,dayOfMonth);
}
private void updateTitle(int Month, int Day)
{
// TODO Auto-generated method stub
Calendar mCalendar = Calendar.getInstance();
mCalendar.set(Calendar.MONTH, Month);
mCalendar.set(Calendar.DAY_OF_MONTH, Day);
prabs.setTitle(getFormat().format(mCalendar.getTime()));
}
public SimpleDateFormat getFormat() {
// TODO Auto-generated method stub
return new SimpleDateFormat("MMM, dd");
}
public void onDateChanged(DatePicker view,int year, int month,int day)
{
updateTitle(month,day);
}
public DatePickerDialog getPicker()
{
return this.prabs;
}
}
在添加obj.show();
之前它正在使用日历显示整个小部件,但我只想要包含日期和月份的小部件
试图从许多天来解决这个问题..但每次失败:( 任何建议都会有很大的帮助.. 谢谢..
EDIT ::
年份字段表示日期选择器窗口小部件的年度字段
我提到了How to display year only in date picker in android和Custom Date Picker Dialog
错误日志::
01-09 11:26:20.780: E/AndroidRuntime(23840): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application this is a crash report
答案 0 :(得分:0)
试试这个,它对我有用。
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// hide year from datepicker
DatePickerDialog dlg = new DatePickerDialog(this,
datePickerListener, year, month, day) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int year = getContext().getResources().getIdentifier(
"android:id/year", null, null);
if (year != 0) {
View yearPicker = findViewById(year);
if (yearPicker != null) {
yearPicker.setVisibility(View.GONE);
}
}
}
};
return dlg;
}
return null;
}
答案 1 :(得分:0)
尝试以下代码
final Calendar cal = Calendar.getInstance();
mDialog = new CustomerDatePickerDialog(getContext(), this,
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
mDialog.show();
DatePicker dp = findDatePicker((ViewGroup) mDialog.getWindow().getDecorView());
if (dp != null) {
((ViewGroup) dp.getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
}
CustomerDatePickerDialog
class CustomerDatePickerDialog extends DatePickerDialog {
public CustomerDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
}
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
super.onDateChanged(view, year, month, day);
mDialog.setTitle((month + 1) + "-" + day + "-");
}
}
日期选择器
private DatePicker findDatePicker(ViewGroup group) {
if (group != null) {
for (int i = 0, j = group.getChildCount(); i < j; i++) {
View child = group.getChildAt(i);
if (child instanceof DatePicker) {
return (DatePicker) child;
} else if (child instanceof ViewGroup) {
DatePicker result = findDatePicker((ViewGroup) child);
if (result != null)
return result;
}
}
}
return null;
}