Android:隐藏DatePicker标题(选择日,月和年)

时间:2017-01-15 23:54:31

标签: java android datepicker android-6.0-marshmallow

当我运行它时,我试图在Lollipop中创建一个CalendarView我没有得到与Marshmallow相同的设计,所以我改为DatePicker但我不希望标题部分只是日历。这可能吗?

我在我的MainActivity中试过这段代码,但没有运气(指的是link

public class MainActivity extends AppCompatActivity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initMonthPicker();
}
public void initMonthPicker() {
    DatePicker dp_mes = (DatePicker) findViewById(R.id.datePicker2);

    int year = dp_mes.getYear();
    int month = dp_mes.getMonth();
    int day = dp_mes.getDayOfMonth();
    dp_mes.init(year, month, day, new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            int month_i = monthOfYear + 1;
            Log.e("selected month:", Integer.toString(month_i));
            //Add whatever you need to handle Date changes
        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
        if (daySpinnerId != 0) {
            View daySpinner = dp_mes.findViewById(daySpinnerId);
            if (daySpinner != null) {
                daySpinner.setVisibility(View.GONE);
            }
        }

        int monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
        if (monthSpinnerId != 0) {
            View monthSpinner = dp_mes.findViewById(monthSpinnerId);
            if (monthSpinner != null) {
                monthSpinner.setVisibility(View.VISIBLE);
            }
        }

        int yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
        if (yearSpinnerId != 0) {
            View yearSpinner = dp_mes.findViewById(yearSpinnerId);
            if (yearSpinner != null) {
                yearSpinner.setVisibility(View.GONE);
            }
        }
    } else { //Older SDK versions
        Field f[] = dp_mes.getClass().getDeclaredFields();
        for (Field field : f) {
            if (field.getName().equals("mDayPicker") || field.getName().equals("mDaySpinner")) {
                field.setAccessible(true);
                Object dayPicker = null;
                try {
                    dayPicker = field.get(dp_mes);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                ((View) dayPicker).setVisibility(View.GONE);
            }

            if (field.getName().equals("mMonthPicker") || field.getName().equals("mMonthSpinner")) {
                field.setAccessible(true);
                Object monthPicker = null;
                try {
                    monthPicker = field.get(dp_mes);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                ((View) monthPicker).setVisibility(View.VISIBLE);
            }

            if (field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner")) {
                field.setAccessible(true);
                Object yearPicker = null;
                try {
                    yearPicker = field.get(dp_mes);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                ((View) yearPicker).setVisibility(View.GONE);
            }
        }
    }
}
}

布局xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.calendarviewtest.MainActivity">


    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/datePicker2" />
</RelativeLayout>

我有什么问题吗?

0 个答案:

没有答案