Home  >  Article  >  Web Front-end  >  Detailed explanation of React Native open source time and date picker component

Detailed explanation of React Native open source time and date picker component

巴扎黑
巴扎黑Original
2017-09-15 09:12:102827browse

This article mainly introduces the detailed explanation of the React Native open source time and date picker component (react-native-datetime), which has certain reference value. Those who are interested can learn more about

Project Introduction

This component encapsulates a time and date picker and is adapted to both Android and iOS platforms. This component is developed based on @remobile/react-native-datetime-picker

Configuration and installation


npm install react-native-datetime --save

1.1. iOS environment configuration

After completing the above steps, directly Just write js code in the frontend

1.2. Android environment configuration

Configure as follows in the android/setting.gradle file


...
include ':react-native-datetime'
project(':react-native-datetime').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-datetime/android')

Configure the following in the android/app/build.gradle file


...
dependencies {
  ...
  compile project(':react-native-datetime')
}

Register the module in MainActivity.java

①.React Native>=0.18 starts


##

import com.keyee.datetime.*; // <--- import
 
public class MainActivity extends ReactActivity {
 ......
 
 /**
  * A list of packages used by the app. If the app uses additional views
  * or modules besides the default ones, add more packages here.
  */
  @Override
  protected List<ReactPackage> getPackages() {
   return Arrays.<ReactPackage>asList(
    new RCTDateTimePickerPackage(this), // <------ add here
    new MainReactPackage());
  }
}

①.React Native<=0.17 version


import com.keyee.datetime.*; // <--- import
 
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
 ......
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  mReactRootView = new ReactRootView(this);
 
  mReactInstanceManager = ReactInstanceManager.builder()
   .setApplication(getApplication())
   .setBundleAssetName("index.android.bundle")
   .setJSMainModuleName("index.android")
   .addPackage(new MainReactPackage())
   .addPackage(new RCTDateTimePickerPackage(this))       // <------ add here
   .setUseDeveloperSupport(BuildConfig.DEBUG)
   .setInitialLifecycleState(LifecycleState.RESUMED)
   .build();
 
  mReactRootView.startReactApplication(mReactInstanceManager, "ExampleRN", null);
 
  setContentView(mReactRootView);
 }
 
 ......
}

Running screenshot


ios running effect

##android running effect

Usage method

<DateTimePicker ref={(picker)=>{this.picker=picker}}/>
...
this.picker.showDatePicker(...)
this.picker.showTimePicker(...)
this.picker.showDateTimePicker(...)

When used on the ios platform, you need to ensure that the current DataTimePicker view is at the top

Usage example

&#39;use strict&#39;;
 
var React = require(&#39;react-native&#39;);
var {
  StyleSheet,
  TouchableOpacity,
  View,
  Text,
} = React;
 
var DateTimePicker = require(&#39;react-native-datetime&#39;);
var Button = require(&#39;@remobile/react-native-simple-button&#39;);
 
module.exports = React.createClass({
  getInitialState() {
    return {
      date: new Date(),
    }
  },
  showDatePicker() {
    var date = this.state.date;
    this.picker.showDatePicker(date, (d)=>{
      this.setState({date:d});
    });
  },
  showTimePicker() {
    var date = this.state.date;
    this.picker.showTimePicker(date, (d)=>{
      this.setState({date:d});
    });
  },
  showDateTimePicker() {
    var date = this.state.date;
    this.picker.showDateTimePicker(date, (d)=>{
      this.setState({date:d});
    });
  },
  render() {
    return (
      <View style={styles.container}>
        <Text style={{textAlign: &#39;center&#39;}}>
          {this.state.date.toString()}
        </Text>
        <View style={{height:40}} />
        <Button onPress={this.showDatePicker}>showDatePicker</Button>
        <View style={{height:40}} />
        <Button onPress={this.showTimePicker}>showTimePicker</Button>
        <View style={{height:40}} />
        <Button onPress={this.showDateTimePicker}>showDateTimePicker</Button>
        <DateTimePicker ref={(picker)=>{this.picker=picker}}/>
      </View>
    );
  },
});
 
var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: &#39;center&#39;,
    paddingTop:20,
  },
});

Method introduction

    showDatePicker(date, callback(date))
  • ##showTimePicker(date, callback(date))
  • showDateTimePicker(date, callback(date))
  • Property introduction

cancelText (default: Cancel)
  • okText (default: Ok)

The above is the detailed content of Detailed explanation of React Native open source time and date picker component. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn