最近项目需要单独显示选择年份、选择年月和选中年月日的控件需求,就自己研究了一下,顺便做记录。话不多说,进入正题。。。
先看一下效果图:
首先写一个activity页面:
public class DateActivity extends Activity {
private EditText etYear, etMonth, etDay;
private String month;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_history_q);
//年
etYear = findViewById(R.id.et_year);
etYear.setText(new SimpleDateFormat("yyyy").format(new Date()));
findViewById(R.id.iv_year).setOnClickListener(new OnPickYearClickListener(this, etYear));
//年月
etMonth = findViewById(R.id.et_month);
etMonth.setText(new SimpleDateFormat("yyyy-MM").format(new Date()));
findViewById(R.id.iv_month).setOnClickListener(new OnPickMonthClickListener(this, etMonth));
//年月日
etDay = findViewById(R.id.et_day);
etDay.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
findViewById(R.id.iv_day).setOnClickListener(new OnPickDateClickListener(this, etDay));
}
}
activity对应的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="@dimen/btn_height"
android:gravity="center"
android:text="年份:" />
<EditText
android:id="@+id/et_year"
android:layout_width="@dimen/sp_width"
android:layout_height="@dimen/btn_height"
android:enabled="false"
android:gravity="center"
android:textColor="@color/contact_catalog" />
<ImageView
android:id="@+id/iv_year"
android:layout_width="@dimen/btn_height"
android:layout_height="@dimen/btn_height"
android:gravity="center"
android:src="@drawable/ic_date" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="@dimen/btn_height"
android:gravity="center"
android:text="月份:" />
<EditText
android:id="@+id/et_month"
android:layout_width="@dimen/sp_width"
android:layout_height="@dimen/btn_height"
android:enabled="false"
android:gravity="center"
android:textColor="@color/contact_catalog" />
<ImageView
android:id="@+id/iv_month"
android:layout_width="@dimen/btn_height"
android:layout_height="@dimen/btn_height"
android:gravity="center"
android:src="@drawable/ic_date" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="@dimen/btn_height"
android:gravity="center"
android:text="日期:" />
<EditText
android:id="@+id/et_day"
android:layout_width="@dimen/sp_width"
android:layout_height="@dimen/btn_height"
android:enabled="false"
android:gravity="center"
android:textColor="@color/contact_catalog" />
<ImageView
android:id="@+id/iv_day"
android:layout_width="@dimen/btn_height"
android:layout_height="@dimen/btn_height"
android:gravity="center"
android:src="@drawable/ic_date" />
</LinearLayout>
</LinearLayout>
最后关键代码来了,分别写年、年月、年月日对应DatePickerDialog实现,代码如下:
public class OnPickYearClickListener implements OnClickListener {
private Context context;
private EditText editText;
public OnPickYearClickListener(Context context, EditText editText) {
this.context = context;
this.editText = editText;
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
setTextYear(year);
}
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int monthOfYear = c.get(Calendar.MONTH);
int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(context, DatePickerDialog.THEME_HOLO_LIGHT, mDateSetListener, year, monthOfYear, dayOfMonth);
DatePicker datePicker = datePickerDialog.getDatePicker();
//隐藏月份
((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE);
//隐藏日
((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
datePickerDialog.show();
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
setTextYear(year);
}
};
private void setTextYear(int year) {
editText.setText(new StringBuilder().append(year));
}
}
public class OnPickMonthClickListener implements OnClickListener {
private Context context;
private EditText editText;
public OnPickMonthClickListener(Context context, EditText editText) {
this.context = context;
this.editText = editText;
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int monthOfYear = c.get(Calendar.MONTH);
setTextMonth(year, monthOfYear);
}
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int monthOfYear = c.get(Calendar.MONTH);
int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(context, DatePickerDialog.THEME_HOLO_LIGHT, mDateSetListener, year, monthOfYear, dayOfMonth);
DatePicker datePicker = datePickerDialog.getDatePicker();
//隐藏月份
//((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE);
//隐藏日
((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
datePickerDialog.show();
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
setTextMonth(year, monthOfYear);
}
};
private void setTextMonth(int year, int monthOfYear) {
editText.setText(new StringBuilder()
.append(year).append("-")
.append((monthOfYear + 1) < 10 ? "0" + (monthOfYear + 1) : (monthOfYear + 1)));
}
}
public class OnPickDateClickListener implements OnClickListener {
private Context context;
private EditText editText;
public OnPickDateClickListener(Context context, EditText editText) {
this.context = context;
this.editText = editText;
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int monthOfYear = c.get(Calendar.MONTH);
int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
setTextDate(year, monthOfYear, dayOfMonth);
}
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int monthOfYear = c.get(Calendar.MONTH);
int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
String dateStr = editText.getText().toString().trim();
int monthOfYeartemp = monthOfYear + 1;
String monthOfYeartempStr = String.valueOf(monthOfYeartemp);
if (monthOfYeartempStr.length() == 1) {
monthOfYeartempStr = "0" + monthOfYeartempStr;
}
String dayOfMonthtempStr = String.valueOf(dayOfMonth);
if (dayOfMonthtempStr.length() == 1) {
dayOfMonthtempStr = "0" + dayOfMonth;
}
String dateStrCur = year + "-" + monthOfYeartempStr + "-" + dayOfMonthtempStr;
if (dateStr.equals(dateStrCur) || null == dateStr) {
setTextDate(year, monthOfYear, dayOfMonth);
new DatePickerDialog(context, mDateSetListener, year, monthOfYear, dayOfMonth).show();
} else {
int year1 = Integer.parseInt(dateStr.substring(0, 4));
String monthStr = dateStr.substring(5, 7).toString().trim();
int monthOfYear1 = -1;
if (monthStr.startsWith("0")) {
monthStr = monthStr.substring(1, 2);
monthOfYear1 = Integer.parseInt(monthStr);
monthOfYear1 = monthOfYear1 - 1;
} else {
monthOfYear1 = Integer.parseInt(monthStr);
monthOfYear1 = monthOfYear1 - 1;
}
String dayStr = dateStr.substring(8, 10);
int dayOfMonth1 = -1;
if (dayStr.startsWith("0")) {
dayStr = dayStr.substring(1, 2);
dayOfMonth1 = Integer.parseInt(dayStr);
} else {
dayOfMonth1 = Integer.parseInt(dayStr);
}
new DatePickerDialog(context, mDateSetListener, year1, monthOfYear1, dayOfMonth1).show();
}
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
setTextDate(year, monthOfYear, dayOfMonth);
}
};
private void setTextDate(int year, int monthOfYear, int dayOfMonth) {
editText.setText(new StringBuilder()
.append(year).append("-")
.append((monthOfYear + 1) < 10 ? "0" + (monthOfYear + 1) : (monthOfYear + 1)).append("-")
.append((dayOfMonth < 10) ? "0" + dayOfMonth : dayOfMonth));
}
}
OK,到这里就结束了。。。。