仅在Android中的棒棒糖5.0上显示月份和年份的日期选择器,它可以在较低版本中完成,但我怎样才能在android 5.0中完成。
答案 0 :(得分:5)
这可以通过将DatePicker设置为微调模式并获取来完成 Spinner for Day并以编程方式隐藏它。
这是XML(dialog_date_picker.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="8dp"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:layout_weight="4"
android:layout_height="0dp"
/>
<Button
android:id="@+id/date_time_set"
android:layout_weight="1"
android:layout_width="match_parent"
android:text="Set"
android:layout_height="0dp" />
</LinearLayout>
这是执行此操作的代码。
private Calendar mCalendar;
...
mCalendar = Calendar.getInstance();
final View dialogView = View.inflate(this, R.layout.dialog_date_picker, null);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
datePicker.init(mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH),null);
LinearLayout ll = (LinearLayout)datePicker.getChildAt(0);
LinearLayout ll2 = (LinearLayout)ll.getChildAt(0);
ll2.getChildAt(0).setVisibility(View.INVISIBLE);
dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
mCalendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth());
alertDialog.dismiss();
}
});
答案 1 :(得分:4)
这适用于所有Android版本(api> 11)
datePicker.findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
答案 2 :(得分:-3)
我写了一个简单的,希望它可以帮助你
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();