用于Android的AlertIOS.prompt的React-Native替代?

时间:2016-10-14 23:25:38

标签: android react-native

嘿伙计们,所以我正在关注react-native的教程,但是他们正在为IOS做这个,有一部分他们使用AlertIOS.prompt这样

AlertIOS.prompt(
  'Add New Item',
  null,
  [
    {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
    {
      text: 'Add',
      onPress: (text) => {
        this.itemsRef.push({ title: text })
      }
    },
  ],
  'plain-text'
);

我正在尝试为Android重制此功能但无法使其正常工作,我确实找到了这个https://www.npmjs.com/package/react-native-prompt

import Prompt from 'react-native-prompt';
<Prompt
title="Say something"
placeholder="Start typing"
defaultValue="Hello"
visible={ this.state.promptVisible }
onCancel={ () => this.setState({
  promptVisible: false,
  message: "You cancelled"
}) }
onSubmit={ (value) => this.setState({
  promptVisible: false,
  message: `You said "${value}"`
}) }/>

但是我也无法使用它,当我按下按钮但是没有任何反应时它应该显示提示..

以下是AlertIOS的完整原始代码

'use strict';

import React, {Component} from 'react';
import ReactNative from 'react-native';
const firebase = require('firebase');
const StatusBar = require('./components/StatusBar');
const ActionButton = require('./components/ActionButton');
const ListItem = require('./components/ListItem');
const styles = require('./styles.js')

const {
  AppRegistry,
  ListView,
  StyleSheet,
  Text,
  View,
  TouchableHighlight,
  AlertIOS,
} = ReactNative;

// Initialize Firebase
const firebaseConfig = {
  apiKey: "AIzaSyA9y6Kv10CAl-QOnSkMehOyCUejwvKZ91E",
  authDomain: "dontforget.firebaseapp.com",
  databaseURL: "https://dontforget-bd066.firebaseio.com",
  storageBucket: "dontforget-bd066.appspot.com",
};
const firebaseApp = firebase.initializeApp(firebaseConfig);

class dontforget extends Component {

  constructor(props) {
    super(props);
    this.state = {
      dataSource: new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
      })
    };
    this.itemsRef = this.getRef().child('items');
  }

  getRef() {
    return firebaseApp.database().ref();
  }

  listenForItems(itemsRef) {
    itemsRef.on('value', (snap) => {

      // get children as an array
      var items = [];
      snap.forEach((child) => {
        items.push({
          title: child.val().title,
          _key: child.key
        });
      });

      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(items)
      });

    });
  }

  componentDidMount() {
    this.listenForItems(this.itemsRef);
  }

  render() {
    return (
      <View style={styles.container}>

        <StatusBar title="Grocery List" />

        <ListView
          dataSource={this.state.dataSource}
          renderRow={this._renderItem.bind(this)}
          enableEmptySections={true}
          style={styles.listview}/>

        <ActionButton onPress={this._addItem.bind(this)} title="Add" />
      </View>
    )

  }

  _addItem() {
    AlertIOS.prompt(
      'Add New Item',
      null,
      [
        {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
        {
          text: 'Add',
          onPress: (text) => {
            this.itemsRef.push({ title: text })
          }
        },
      ],
      'plain-text'
    );
  }




  _renderItem(item) {

    const onPress = () => {
      AlertIOS.alert(
        'Complete',
        null,
        [
          {text: 'Complete', onPress: (text) => this.itemsRef.child(item._key).remove()},
          {text: 'Cancel', onPress: (text) => console.log('Cancelled')}
        ]
      );
    };

    return (
      <ListItem item={item} onPress={onPress} />
    );
  }

}

AppRegistry.registerComponent('dontforget', () => dontforget);

有谁可以告诉我如何才能让这项功能成为Android?

3 个答案:

答案 0 :(得分:1)

我认为您可以使用以下库:https://github.com/mmazzarolo/react-native-dialog

获取用户输入的示例代码如下(不在docs中)

<Dialog.Container visible={true}>
          <Dialog.Title>Account delete</Dialog.Title>
          <Dialog.Description>
            Do you want to delete this account? You cannot undo this action.
          </Dialog.Description>
          <Dialog.Input label="Cancel" onChangeText={(text) => this.setState({username : text})}  />
          <Dialog.Button label="Delete" onPress={() => {
              console.log(this.state.username);
              this.setState({promptUser : false});
            }} />
        </Dialog.Container>

答案 1 :(得分:0)

实际上,您可以使用在Android和Ios上都能正常工作的跨平台提示组件。链接如下。

https://www.npmjs.com/package/react-native-prompt-crossplatform及其 存储库

链接是 https://github.com/Ramiah-Viknesh/react-native-prompt-crossplatform

答案 2 :(得分:0)

我开发了一个小实用程序来解决这个问题。可能有用。

https://github.com/eleviven/react-native-alertbox