gpt4 book ai didi

android - 如何拥有更好看的DatePicker?

转载 作者:搜寻专家 更新时间:2023-11-01 08:06:46 25 4
gpt4 key购买 nike

我必须开发一个需要在 2.2 设备上运行的 Android 应用;我正在使用 UI 非常好的 HoloEverywhereLib。我需要使用 DatePicker;如果我使用默认组件,它的外观真的很难看:

enter image description here

我想要一个像新的一样的东西:

enter image description here

有办法吗?

最佳答案

如果你想拥有 Android HC+ 的默认日期选择器,你可以通过复制 Android github framework mirror 中的代码来实现它。 :

您将需要 DatePickerDialog:

package.android.app;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;

import com.android.internal.R;

import java.util.Calendar;

/**
* A simple dialog containing an {@link android.widget.DatePicker}.
*
* <p>See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
* guide.</p>
*/
public class DatePickerDialog extends AlertDialog implements OnClickListener,
OnDateChangedListener {

private static final String YEAR = "year";
private static final String MONTH = "month";
private static final String DAY = "day";

private final DatePicker mDatePicker;
private final OnDateSetListener mCallBack;
private final Calendar mCalendar;

private boolean mTitleNeedsUpdate = true;

/**
* The callback used to indicate the user is done filling in the date.
*/
public interface OnDateSetListener {

/**
* @param view The view associated with this listener.
* @param year The year that was set.
* @param monthOfYear The month that was set (0-11) for compatibility
* with {@link java.util.Calendar}.
* @param dayOfMonth The day of the month that was set.
*/
void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
}

/**
* @param context The context the dialog is to run in.
* @param callBack How the parent is notified that the date is set.
* @param year The initial year of the dialog.
* @param monthOfYear The initial month of the dialog.
* @param dayOfMonth The initial day of the dialog.
*/
public DatePickerDialog(Context context,
OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth) {
this(context, 0, callBack, year, monthOfYear, dayOfMonth);
}

/**
* @param context The context the dialog is to run in.
* @param theme the theme to apply to this dialog
* @param callBack How the parent is notified that the date is set.
* @param year The initial year of the dialog.
* @param monthOfYear The initial month of the dialog.
* @param dayOfMonth The initial day of the dialog.
*/
public DatePickerDialog(Context context,
int theme,
OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth) {
super(context, theme);

mCallBack = callBack;

mCalendar = Calendar.getInstance();

Context themeContext = getContext();
setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_done), this);
setIcon(0);

LayoutInflater inflater =
(LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.date_picker_dialog, null);
setView(view);
mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
mDatePicker.init(year, monthOfYear, dayOfMonth, this);
updateTitle(year, monthOfYear, dayOfMonth);
}

public void onClick(DialogInterface dialog, int which) {
tryNotifyDateSet();
}

public void onDateChanged(DatePicker view, int year,
int month, int day) {
mDatePicker.init(year, month, day, this);
updateTitle(year, month, day);
}

/**
* Gets the {@link DatePicker} contained in this dialog.
*
* @return The calendar view.
*/
public DatePicker getDatePicker() {
return mDatePicker;
}

/**
* Sets the current date.
*
* @param year The date year.
* @param monthOfYear The date month.
* @param dayOfMonth The date day of month.
*/
public void updateDate(int year, int monthOfYear, int dayOfMonth) {
mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
}

private void tryNotifyDateSet() {
if (mCallBack != null) {
mDatePicker.clearFocus();
mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
}
}

@Override
protected void onStop() {
tryNotifyDateSet();
super.onStop();
}

private void updateTitle(int year, int month, int day) {
if (!mDatePicker.getCalendarViewShown()) {
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
mCalendar.set(Calendar.DAY_OF_MONTH, day);
String title = DateUtils.formatDateTime(mContext,
mCalendar.getTimeInMillis(),
DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_SHOW_WEEKDAY
| DateUtils.FORMAT_SHOW_YEAR
| DateUtils.FORMAT_ABBREV_MONTH
| DateUtils.FORMAT_ABBREV_WEEKDAY);
setTitle(title);
mTitleNeedsUpdate = true;
} else {
if (mTitleNeedsUpdate) {
mTitleNeedsUpdate = false;
setTitle(R.string.date_picker_dialog_title);
}
}
}

@Override
public Bundle onSaveInstanceState() {
Bundle state = super.onSaveInstanceState();
state.putInt(YEAR, mDatePicker.getYear());
state.putInt(MONTH, mDatePicker.getMonth());
state.putInt(DAY, mDatePicker.getDayOfMonth());
return state;
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int year = savedInstanceState.getInt(YEAR);
int month = savedInstanceState.getInt(MONTH);
int day = savedInstanceState.getInt(DAY);
mDatePicker.init(year, month, day, this);
}
}

还有 AltertDialog .

您还必须导入资源。你可以找到他们here .并且您必须将 AlertDialog 中的 Holo Theme 替换为相当于深色和浅色主题的 HoloEverwhere

关于android - 如何拥有更好看的DatePicker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13511210/

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