如何仅在android的日期选择器中显示年份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10793811/
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
How to display year only in date picker in android
提问by user386430
Can anybody tell how to display year only in date picker in android
任何人都可以告诉如何仅在 android 的日期选择器中显示年份
Thanks
谢谢
回答by MH.
You could also use some simple reflection to hide the day and month NumberPicker
s of a DatePicker
widget. Do note that this may stop working if Google decides to rename the member field names. Also, this approach of course makes sense when using android:datePickerMode="spinner"
.
你也可以使用一些简单的反射隐藏日期和月份NumberPicker
一个第DatePicker
部件。请注意,如果 Google 决定重命名成员字段名称,这可能会停止工作。此外,这种方法在使用android:datePickerMode="spinner"
.
// inflate DatePicker, or e.g. get it from a DatePickerDialog:
DatePicker datepicker = ...
// pre-Honeycomb fields:
findAndHideField(datepicker, "mDayPicker");
findAndHideField(datepicker, "mMonthPicker");
// Honeycomb(+) fields:
findAndHideField(datepicker, "mDaySpinner");
findAndHideField(datepicker, "mMonthSpinner");
// Lollipop(+) fields (wrapped in a delegate):
final Object mDatePickerDelegate = findFieldInstance(picker, "mDelegate");
findAndHideField(mDatePickerDelegate, "mDaySpinner");
findAndHideField(mDatePickerDelegate, "mMonthSpinner");
/** find a member field by given name and hide it */
private static void findAndHideField(Object object, String name) {
try {
final Field field = object.getClass().getDeclaredField(name);
field.setAccessible(true);
final View fieldInstance = (View) field.get(object);
fieldInstance.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
/* find a member field by given name and return its instance value */
private static Object findFieldInstance(DatePicker datepicker, String name) {
try {
final Field field = DatePicker.class.getDeclaredField(name);
field.setAccessible(true);
return field.get(datepicker);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Update:Added code illustrating how to make this approach work with Android 'Lollipop' 5.0 (API 21+).
更新:添加了说明如何使这种方法适用于 Android 'Lollipop' 5.0 (API 21+) 的代码。
Disclaimer: I cannot stress enough that this is hacky solution that may stop working with any framework update (as it did when Android 3.0 was first introduced, and again with Android 5.0).
免责声明:我不能强调这是一个可能会停止与任何框架更新一起工作的hacky解决方案(就像首次引入Android 3.0和Android 5.0时一样)。
回答by Agilanbu
First, create MonthYearPickerDialog.javaclass extends with DialogFragment
首先,创建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();
}
}
in styles.xmlpaste this code.
在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>
create month_year_picker_dialog.xml
创建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>
In your main class(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;
}
Finally ,call wherever you want
最后,随心所欲地打电话
createDialogWithoutDateField().show();
回答by sparkle ramani
you can hide day and month from custom datepicker like this:
您可以像这样从自定义日期选择器中隐藏日期和月份:
DatePicker datePicker = (DatePicker) datePickerDialogField.get(obj);
DatePicker monPickerPicker = (DatePicker) datePickerDialogField.get(obj);
Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
for (Field datePickerField : datePickerFields) {
//datePickerField
//mMonthSpinner
if (“mDayPicker”.equals(datePickerField.getName()) || “mDaySpinner”.equals(datePickerField
.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
if (“mMonthpicker”.equals(datePickerField.getName()) || “mMonthSpinner”.equals(datePickerField
.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
回答by ROHIT LIEN
I have modified @agilanbu 's answer and it can be used as :
我修改了@agilanbu 的答案,它可以用作:
private fun createDialogWithoutDateField() {
私人乐趣 createDialogWithoutDateField() {
val alertDialog: AlertDialog?
val builder = AlertDialog.Builder(activity, R.style.AlertDialogStyle)
val inflater = activity!!.layoutInflater
val cal = Calendar.getInstance()
val dialog = inflater.inflate(R.layout.month_year_picker_dialog, null)
val monthPicker = dialog.findViewById(R.id.picker_month) as NumberPicker
val yearPicker = dialog.findViewById(R.id.picker_year) as NumberPicker
monthPicker.minValue = 1
monthPicker.maxValue = 12
monthPicker.value = cal.get(Calendar.MONTH) + 1
val year = cal.get(Calendar.YEAR)
yearPicker.minValue = 1900
yearPicker.maxValue = 3500
yearPicker.value = year
builder.setView(dialog).setPositiveButton(Html.fromHtml("<font color='#FF4081'>Ok</font>")){dialogInterface, which ->
//Toast.makeText(applicationContext,"clicked yes",Toast.LENGTH_LONG).show()
val value = yearPicker.value
dialogInterface.cancel()
}
builder.setNegativeButton(Html.fromHtml("<font color='#FF4081'>Cancel</font>")){dialogInterface, which ->
dialogInterface.cancel()
}
alertDialog = builder.create()
// Set other dialog properties
alertDialog.setCancelable(true)
alertDialog.show()
}
You can get the layout file and styles in @agilanbu 's answer.
您可以在@agilanbu 的回答中获取布局文件和样式。
回答by Dheeresh Singh
as it looks coustmizing is not an easy option here from :
but you can try some solution from here : Custom DatePicker
因为它看起来 coustmizing 不是一个简单的选择:
但您可以从这里尝试一些解决方案: 自定义 DatePicker
but it not easy one as :
但这并不容易,因为:
but hotveryspicy answer is good option here
但 hotveryspicy 的答案在这里是不错的选择
even can also try this one : http://code.google.com/p/android-wheel/
甚至也可以试试这个:http: //code.google.com/p/android-wheel/
回答by Abhay Bansod
Custom Year Picker, this is easy option to for only Year Picker You also look
Custom Year Picker in androidtry this it's working for me
自定义年份选择器,这是仅适用于年份选择器的简单选项您也可以
在 android 中查看自定义年份选择器试试这个它对我有用
year picker screenshotI am share my screenshot ,only Year Picker
年份选择器截图我正在分享我的截图,只有年份选择器