mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-09 22:43:10 +08:00
Add TimePicker modes
Summary: In the spirit of #10932, I added the `mode` option to the `TimePicker` Android API. There is only one mode available for **Android < 5**, the `spinner` one. If we are on **Android >= 5** we can choose between `spinner` or `clock`. If we specify `default` it will use the default of the current Android version. On **Android < 5**, whatever we choose it will be this:  On **Android >= 5**, with the `spinner` mode:  And with the `clock` mode, the default:  Closes https://github.com/facebook/react-native/pull/12384 Differential Revision: D6006689 Pulled By: hramos fbshipit-source-id: fcd37c867c4061b9982b1687f2c10211e54df7cf
This commit is contained in:
committed by
Facebook Github Bot
parent
f9be64aea0
commit
1c24440644
@@ -9,19 +9,22 @@
|
||||
|
||||
package com.facebook.react.modules.timepicker;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.app.DialogFragment;
|
||||
import android.app.TimePickerDialog;
|
||||
import android.app.TimePickerDialog.OnTimeSetListener;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnDismissListener;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.text.format.DateFormat;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@SuppressWarnings("ValidFragment")
|
||||
public class TimePickerDialogFragment extends DialogFragment {
|
||||
|
||||
@@ -44,6 +47,11 @@ public class TimePickerDialogFragment extends DialogFragment {
|
||||
int minute = now.get(Calendar.MINUTE);
|
||||
boolean is24hour = DateFormat.is24HourFormat(activityContext);
|
||||
|
||||
TimePickerMode mode = TimePickerMode.DEFAULT;
|
||||
if (args != null && args.getString(TimePickerDialogModule.ARG_MODE, null) != null) {
|
||||
mode = TimePickerMode.valueOf(args.getString(TimePickerDialogModule.ARG_MODE).toUpperCase(Locale.US));
|
||||
}
|
||||
|
||||
if (args != null) {
|
||||
hour = args.getInt(TimePickerDialogModule.ARG_HOUR, now.get(Calendar.HOUR_OF_DAY));
|
||||
minute = args.getInt(TimePickerDialogModule.ARG_MINUTE, now.get(Calendar.MINUTE));
|
||||
@@ -52,12 +60,42 @@ public class TimePickerDialogFragment extends DialogFragment {
|
||||
DateFormat.is24HourFormat(activityContext));
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
if (mode == TimePickerMode.CLOCK) {
|
||||
return new DismissableTimePickerDialog(
|
||||
activityContext,
|
||||
activityContext.getResources().getIdentifier(
|
||||
"ClockTimePickerDialog",
|
||||
"style",
|
||||
activityContext.getPackageName()
|
||||
),
|
||||
onTimeSetListener,
|
||||
hour,
|
||||
minute,
|
||||
is24hour
|
||||
);
|
||||
} else if (mode == TimePickerMode.SPINNER) {
|
||||
return new DismissableTimePickerDialog(
|
||||
activityContext,
|
||||
activityContext.getResources().getIdentifier(
|
||||
"SpinnerTimePickerDialog",
|
||||
"style",
|
||||
activityContext.getPackageName()
|
||||
),
|
||||
onTimeSetListener,
|
||||
hour,
|
||||
minute,
|
||||
is24hour
|
||||
);
|
||||
}
|
||||
}
|
||||
return new DismissableTimePickerDialog(
|
||||
activityContext,
|
||||
onTimeSetListener,
|
||||
hour,
|
||||
minute,
|
||||
is24hour);
|
||||
activityContext,
|
||||
onTimeSetListener,
|
||||
hour,
|
||||
minute,
|
||||
is24hour
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,8 +9,6 @@
|
||||
|
||||
package com.facebook.react.modules.timepicker;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.DialogFragment;
|
||||
import android.app.FragmentManager;
|
||||
@@ -31,6 +29,8 @@ import com.facebook.react.bridge.WritableNativeMap;
|
||||
import com.facebook.react.common.annotations.VisibleForTesting;
|
||||
import com.facebook.react.module.annotations.ReactModule;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* {@link NativeModule} that allows JS to show a native time picker dialog and get called back when
|
||||
* the user selects a time.
|
||||
@@ -46,6 +46,7 @@ public class TimePickerDialogModule extends ReactContextBaseJavaModule {
|
||||
/* package */ static final String ARG_HOUR = "hour";
|
||||
/* package */ static final String ARG_MINUTE = "minute";
|
||||
/* package */ static final String ARG_IS24HOUR = "is24Hour";
|
||||
/* package */ static final String ARG_MODE = "mode";
|
||||
/* package */ static final String ACTION_TIME_SET = "timeSetAction";
|
||||
/* package */ static final String ACTION_DISMISSED = "dismissedAction";
|
||||
|
||||
@@ -148,6 +149,9 @@ public class TimePickerDialogModule extends ReactContextBaseJavaModule {
|
||||
if (options.hasKey(ARG_IS24HOUR) && !options.isNull(ARG_IS24HOUR)) {
|
||||
args.putBoolean(ARG_IS24HOUR, options.getBoolean(ARG_IS24HOUR));
|
||||
}
|
||||
if (options.hasKey(ARG_MODE) && !options.isNull(ARG_MODE)) {
|
||||
args.putString(ARG_MODE, options.getString(ARG_MODE));
|
||||
}
|
||||
return args;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.modules.timepicker;
|
||||
|
||||
|
||||
public enum TimePickerMode {
|
||||
CLOCK,
|
||||
SPINNER,
|
||||
DEFAULT
|
||||
}
|
||||
Reference in New Issue
Block a user