gpt4 book ai didi

android - 如何仅在android中的日期选择器中显示年份

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:51:19 24 4
gpt4 key购买 nike

谁能告诉我如何在 android 的日期选择器中只显示年份

谢谢

最佳答案

首先,创建 MonthYearPickerDialog.java 类扩展 DialogFragment

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;
}

@SuppressLint("ResourceAsColor")
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle);
LayoutInflater inflater = getActivity().getLayoutInflater();

Calendar cal = Calendar.getInstance();

View dialog = inflater.inflate(R.layout.month_year_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(1);
monthPicker.setMaxValue(12);
monthPicker.setValue(cal.get(Calendar.MONTH) + 1);

int year = cal.get(Calendar.YEAR);
yearPicker.setMinValue(1900);
yearPicker.setMaxValue(3500);
yearPicker.setValue(year);

builder.setView(dialog).setPositiveButton(Html.fromHtml("<font color='#FF4081'>Ok</font>"), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
listener.onDateSet(null, yearPicker.getValue(), monthPicker.getValue(), 0);
}
}).setNegativeButton(Html.fromHtml("<font color='#FF4081'>Cancel</font>"), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MonthYearPickerDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}

styles.xml 中粘贴此代码。

  <style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="android:textColor">#FF4081</item> // you can change the color of the yearPiker dialog
<item name="android:textColorPrimary">#FF4081</item> // you can change the color of the yearPiker dialog
</style>

创建 month_year_picker_dialog.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="220dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@color/colorAccent"
android:gravity="top">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="8dp"
android:gravity="center"
android:text="Year"
android:textColor="#ffffff"
android:textSize="22dp"
android:textStyle="bold" />

</LinearLayout>

<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:descendantFocusability="blocksDescendants"
android:visibility="gone"
tools:ignore="RtlCompat">

</NumberPicker>

<NumberPicker
android:id="@+id/picker_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
>

</NumberPicker>

</LinearLayout>

</LinearLayout>

</LinearLayout>

在你的主类( Activity )

 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;
}

最后,想去哪里就去哪里

createDialogWithoutDateField().show();

enter image description here

enter image description here

关于android - 如何仅在android中的日期选择器中显示年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10793811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com