我想使用数字日期选择器对话框和时间戳对话框,我已经写了代码,我的选择器对话框正在运行,但我希望它们采用其他格式。
这是我的代码:
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.linearDate: {
DatePickerDialog dp = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mDatetext.setText(dayOfMonth+"-"+monthOfYear+"-"+year);
}
},year,month,day);
dp.show();
}break;
case R.id.linearTime: {
TimePickerDialog Tp = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mTimetext.setText(hourOfDay+ ":" +minute);
}
},hour,min,false);
Tp.show();
}break;
case R.id.linearPlace:{ startActivityForResult(new Intent(this,Location.class),CITY);
}break;
}
}
答案 0 :(得分:7)
非常简单,我的代码工作正常,我需要添加主题及其完成
case R.id.linearDate: {
DatePickerDialog dp = new DatePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mDatetext.setText(dayOfMonth+"-"+monthOfYear+"-"+year);
}
},year,month,day);
dp.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dp.show();
}break;
case R.id.linearTime: {
TimePickerDialog Tp = new TimePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mTimetext.setText(hourOfDay+ ":" +minute);
}
},hour,min,false);
Tp.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Tp.show();
}break;
答案 1 :(得分:1)
虽然这是一个迟到的答案,但 Nougat 设备中存在 TimePickerDialog 主题的错误。在找到此修复程序之前我无法找到解决方案,这可能有助于搜索相同的人。
创建扩展 TimePickerDialog
的自定义 TimePickerDialogFixedNougatSpinner 类并按以下方式自定义: -
public TimePickerDialogFixedNougatSpinner(Context context, int themeResId, OnTimeSetListener listener, int hourOfDay, int minute, boolean is24HourView) {
super(context, themeResId, listener, hourOfDay, minute, is24HourView);
fixSpinner(context, hourOfDay, minute, is24HourView);
}
private void fixSpinner(Context context, int hourOfDay, int minute, boolean is24HourView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // android:timePickerMode spinner and clock began in Lollipop
try {
// Get the theme's android:timePickerMode
final int MODE_SPINNER = 2;
Class<?> styleableClass = Class.forName("com.android.internal.R$styleable");
Field timePickerStyleableField = styleableClass.getField("TimePicker");
int[] timePickerStyleable = (int[]) timePickerStyleableField.get(null);
final TypedArray a = context.obtainStyledAttributes(null, timePickerStyleable, android.R.attr.timePickerStyle, 0);
Field timePickerModeStyleableField = styleableClass.getField("TimePicker_timePickerMode");
int timePickerModeStyleable = timePickerModeStyleableField.getInt(null);
final int mode = a.getInt(timePickerModeStyleable, MODE_SPINNER);
a.recycle();
if (mode == MODE_SPINNER) {
TimePicker timePicker = (TimePicker) findField(TimePickerDialog.class, TimePicker.class, "mTimePicker").get(this);
Class<?> delegateClass = Class.forName("android.widget.TimePicker$TimePickerDelegate");
Field delegateField = findField(TimePicker.class, delegateClass, "mDelegate");
Object delegate = delegateField.get(timePicker);
Class<?> spinnerDelegateClass;
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.LOLLIPOP) {
spinnerDelegateClass = Class.forName("android.widget.TimePickerSpinnerDelegate");
} else {
// TimePickerSpinnerDelegate was initially misnamed TimePickerClockDelegate in API 21!
spinnerDelegateClass = Class.forName("android.widget.TimePickerClockDelegate");
}
// In 7.0 Nougat for some reason the timePickerMode is ignored and the delegate is TimePickerClockDelegate
if (delegate.getClass() != spinnerDelegateClass) {
delegateField.set(timePicker, null); // throw out the TimePickerClockDelegate!
timePicker.removeAllViews(); // remove the TimePickerClockDelegate views
Constructor spinnerDelegateConstructor = spinnerDelegateClass.getConstructor(TimePicker.class, Context.class, AttributeSet.class, int.class, int.class);
spinnerDelegateConstructor.setAccessible(true);
// Instantiate a TimePickerSpinnerDelegate
delegate = spinnerDelegateConstructor.newInstance(timePicker, context, null, android.R.attr.timePickerStyle, 0);
delegateField.set(timePicker, delegate); // set the TimePicker.mDelegate to the spinner delegate
// Set up the TimePicker again, with the TimePickerSpinnerDelegate
timePicker.setIs24HourView(is24HourView);
timePicker.setCurrentHour(hourOfDay);
timePicker.setCurrentMinute(minute);
timePicker.setOnTimeChangedListener(this);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
如需更多参考,请点击此链接: - https://gist.github.com/jeffdgr8/6bc5f990bf0c13a7334ce385d482af9f
确保在fixSpinner函数中传递MODE_SPINNER = 2
答案 2 :(得分:0)
通过扩展android.support.v4.app.DialogFragment的对话框实现此目的。
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
//action on date selection
}
}
同样适用于TimePicker
答案 3 :(得分:0)
可能有帮助
int mHour,mMinute;
TimePickerDialog Tp = new TimePickerDialog(getContext(),R.style.abirStyle, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//mTimetext.setText(hourOfDay+ ":" +minute);
}
},mHour,mMinute,false);
Tp.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Tp.show();
样式中的
<style name="abirStyle" parent="@android:style/Theme.Holo.Light.Dialog.NoActionBar">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:background">@null</item>
<item name="android:textColorPrimary">@color/colorPrimary</item>
</style>
答案 4 :(得分:0)
我想出了如何在不使用Dialog的情况下使用数字TimePicker。他们实现了一项功能,您只需将布局资源文件中TimePicker的timePickerMode
属性设置为spinner
或clock
。