如何解决日期选择器:点击按钮跳转到一年中的不同月份
我使用的是没有箭头的日期选择器,因为我想在点击特定按钮时跳转到任何月份。
例如,如果我点击 march,让我跳转到日期选择器上的 march。
我也使用 textview 来输出月份和年份,但我觉得这是一个糟糕的解决方案。
下面的代码是我看了类似的答案后拼凑起来的,但是效果很差。有些月份我无法点击日期,每个月的天数以及日期所属的天数都不正确。
下图: [在此处输入图片说明][1]
xml
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/CalenderViewDateCustomText"
android:layout_marginTop="-100dp"
android:layout_centerHorizontal="true" />
<androidx.appcompat.widget.AppCompatimageView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:background="@drawable/whiteblock" />
</RelativeLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/selectTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="680dp"
android:layout_marginEnd="0dp"
android:text="Select a Time"
android:textAlignment="center"
android:fontFamily="@font/lato_bold"
android:textSize="20sp"
android:textColor="#202224"/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/dateoutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="July 2020"
android:layout_marginTop="331dp"
android:fontFamily="@font/josefinsans_semibold"
android:textSize="20dp"
android:textColor="#202224"
android:layout_marginStart="16dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="316dp"
android:background="@drawable/mask3x"/>
<com.google.android.material.textview.MaterialTextView
android:id="@+id/registerheading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="54dp"
android:fontFamily="@font/josefin_sans_bold"
android:gravity="center_horizontal"
android:text="Schedule"
android:textColor="#202224"
android:textSize="20dp"/>
<androidx.appcompat.widget.AppCompatimageButton
android:id="@+id/ca_arrow"
android:layout_width="34.5dp"
android:layout_height="26.5dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="35dp"
android:layout_marginTop="56.75dp"
android:background="@drawable/left_arrow"/>
<horizontalscrollview
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="610dp"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="16dp">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/jan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="Jan"
android:textColor="#979797"
android:textSize="12dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/feb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="feb"
android:textColor="#979797"
android:textSize="12dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/march"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="march"
android:textColor="#979797"
android:textSize="12dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/April"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="April"
android:textColor="#979797"
android:textSize="12dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/May"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="May"
android:textColor="#979797"
android:textSize="12dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/June"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="June"
android:textColor="#979797"
android:textSize="12dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/July"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="July"
android:textColor="#979797"
android:textSize="12dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/August"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="August"
android:textColor="#979797"
android:textSize="12dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/September"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="September"
android:textColor="#979797"
android:textSize="12dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/October"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="October"
android:textColor="#979797"
android:textSize="12dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/November"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="November"
android:textColor="#979797"
android:textSize="12dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/December"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="December"
android:textColor="#979797"
android:textSize="12dp"/>
</LinearLayout>
</horizontalscrollview>
Schedule.java
Calendar calendar = Calendar.getInstance();
timepicker();
SimpleDateFormat month_date = new SimpleDateFormat("MMMM yyyy");
String date = month_date.format(calendar.getTime());
outputdate.setText(date);
datePicker.init(calendar.get(calendar.YEAR),calendar.get(calendar.MONTH),calendar.get(calendar.DAY_OF_MONTH),new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view,int year,int monthOfYear,int dayOfMonth) {
}
});
jan.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar jan = Calendar.getInstance();
jan.set(2020,1,0);
datePicker.setMinDate(jan.getTimeInMillis());
Calendar janlast = Calendar.getInstance();
janlast.set(2020,2,0);
datePicker.setMaxDate(janlast.getTimeInMillis());
outputdate.setText("January 2020");
}
});
feb.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar feb = Calendar.getInstance();
feb.set(2020,0);
datePicker.setMinDate(feb.getTimeInMillis());
Calendar feblast = Calendar.getInstance();
feblast.set(2020,0);
datePicker.setMaxDate(feblast.getTimeInMillis());
outputdate.setText("February 2020");
}
});
march.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar march = Calendar.getInstance();
march.set(2020,0);
datePicker.setMinDate(march.getTimeInMillis());
Calendar marchlast = Calendar.getInstance();
marchlast.set(2020,3,0);
datePicker.setMaxDate(marchlast.getTimeInMillis());
outputdate.setText("march 2020");
}
});
april.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar april = Calendar.getInstance();
april.set(2020,4,0);
datePicker.setMinDate(april.getTimeInMillis());
Calendar aprillast = Calendar.getInstance();
aprillast.set(2020,5,0);
datePicker.setMaxDate(aprillast.getTimeInMillis());
outputdate.setText("April 2020");
}
});
may.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar may = Calendar.getInstance();
may.set(2020,0);
datePicker.setMinDate(may.getTimeInMillis());
Calendar maylast = Calendar.getInstance();
maylast.set(2020,31,0);
datePicker.setMaxDate(maylast.getTimeInMillis());
outputdate.setText("May 2020");
}
});
june.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar june = Calendar.getInstance();
june.set(2020,6,0);
datePicker.setMinDate(june.getTimeInMillis());
Calendar junelast = Calendar.getInstance();
junelast.set(2020,0);
datePicker.setMaxDate(junelast.getTimeInMillis());
outputdate.setText("June 2020");
}
});
july.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar july = Calendar.getInstance();
july.set(2020,7,0);
datePicker.setMinDate(july.getTimeInMillis());
Calendar julylast = Calendar.getInstance();
julylast.set(2020,0);
datePicker.setMaxDate(julylast.getTimeInMillis());
outputdate.setText("July 2020");
}
});
august.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar august = Calendar.getInstance();
august.set(2020,8,0);
datePicker.setMinDate(august.getTimeInMillis());
Calendar augustlast = Calendar.getInstance();
augustlast.set(2020,0);
datePicker.setMaxDate(augustlast.getTimeInMillis());
outputdate.setText("August 2020");
}
});
september.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar september = Calendar.getInstance();
september.set(2020,9,0);
datePicker.setMinDate(september.getTimeInMillis());
Calendar septemberlast = Calendar.getInstance();
septemberlast.set(2020,0);
datePicker.setMaxDate(septemberlast.getTimeInMillis());
outputdate.setText("September 2020");
}
});
october.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar october = Calendar.getInstance();
october.set(2020,10,0);
datePicker.setMinDate(october.getTimeInMillis());
Calendar octoberlast = Calendar.getInstance();
octoberlast.set(2020,0);
datePicker.setMaxDate(octoberlast.getTimeInMillis());
outputdate.setText("October 2020");
}
});
november.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar november = Calendar.getInstance();
november.set(2020,11,0);
datePicker.setMinDate(november.getTimeInMillis());
Calendar novemberlast = Calendar.getInstance();
novemberlast.set(2020,0);
datePicker.setMaxDate(novemberlast.getTimeInMillis());
outputdate.setText("November 2020");
}
});
december.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar december = Calendar.getInstance();
december.set(2020,12,0);
datePicker.setMinDate(december.getTimeInMillis());
Calendar decemberlast = Calendar.getInstance();
decemberlast.set(2020,0);
datePicker.setMaxDate(decemberlast.getTimeInMillis());
outputdate.setText("December 2020");
}
});
```
[1]: https://i.stack.imgur.com/42Yol.png
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。