gpt4 book ai didi

android - React-Native 替代 AlertIOS.prompt for android?

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:04 24 4
gpt4 key购买 nike

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

最佳答案

我认为您可以使用以下库: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>

关于android - React-Native 替代 AlertIOS.prompt for android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40053355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com