React-Native中Android的AlertIOS.prompt替代方案是什么?

6

我正在学习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);

有人能告诉我如何在安卓设备上使这个工作吗?


尝试使用 https://github.com/mmazzarolo/react-native-dialog。它提供本地外观。 - Khurshid Ansari
以及更多的自定义选项 - Khurshid Ansari
这是一项工作:https://www.geeksforgeeks.org/how-to-create-custom-dialog-box-with-text-input-react-native/ - Roberta Meireles
4个回答

2
我认为你可以使用以下库:https://github.com/mmazzarolo/react-native-dialog 获取用户输入的示例代码如下(不在文档中)
<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

看起来图书馆已经不再维护了。 - buechel

0

0

请在任何链接中添加上下文,以便您的答案是自包含的,这意味着答案需要在答案本身中。请尽可能回答如何使用该问题。展示它如何回答问题。 - Scratte

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接