【问题标题】:Add WebView via onClick in Expo React-Native在 Expo React-Native 中通过 onClick 添加 WebView
【发布时间】:2020-09-24 11:11:26
【问题描述】:

我需要一个创建带有 web 视图的新屏幕的函数。

我的 expo 应用程序的主屏幕包含一个“添加新页面”按钮,该按钮链接到带有添加域、用户名、密码的表单的页面。

如果这样做了,我想在我的主屏幕中列出一个包含所有页面的页面列表。例如,如果我使用生成的链接 http://user:password@myWebsite1.com 单击“myWebsite1”,则此页面应显示在 web 视图中。网站 2、3 等也是如此。

有人知道怎么做吗?

编辑:我添加了一些我现在拥有的代码。对于我创建的每个任务,webview 文件的域、用户和密码都应更改并保存在此特定任务中。 (并且还打开onclick)。我转了一圈

这是我的 app.js,它首先打开 expo,它包含一个 Flatlist:

import React, { Component } from "react";
import { AppRegistry, StyleSheet, Text, View, FlatList, AsyncStorage, Button, TextInput, Keyboard, Platform, TouchableWithoutFeedback } from "react-native";
const isAndroid = Platform.OS == "android";
const viewPadding = 10;
const things = {things}

export default class NodeList extends Component {
  state = {
    tasks: [ ], 
    text: ""
  };

  changeTextHandler = text => {
    this.setState({ text: text });
  };

  addTask = () => {
    let notEmpty = this.state.text.trim().length > 0;

    if (notEmpty) {
      this.setState(
        prevState => {
          let { tasks, text } = prevState;
          return {
            tasks: tasks.concat({ key: tasks.length, text: text }),
            text: ""
          };
        },
        () => Tasks.save(this.state.tasks)
      );
    }
  };

  deleteTask = i => {
    this.setState(
      prevState => {
        let tasks = prevState.tasks.slice();

        tasks.splice(i, 1);

        return { tasks: tasks };
      },
      () => Tasks.save(this.state.tasks)
    );
  };

  componentDidMount() {
    Keyboard.addListener(
      isAndroid ? "keyboardDidShow" : "keyboardWillShow",
      e => this.setState({ viewMargin: e.endCoordinates.height + viewPadding })
    );

    Keyboard.addListener(
      isAndroid ? "keyboardDidHide" : "keyboardWillHide",
      () => this.setState({ viewMargin: viewPadding })
    );

    Tasks.all(tasks => this.setState({ tasks: tasks || [] }));
  }

  render() {
    return (
      <View
        style={[styles.container, { paddingBottom: this.state.viewMargin }]}
      >
         <Text style={styles.appTitle}>Nodes</Text>
        <FlatList
          style={styles.list}
          data={this.state.tasks}
          renderItem={({ item, index }) =>
            <View>
              <View style={styles.listItemCont}>
              <TouchableWithoutFeedback onPress={() => Linking.openURL('')}>
                <Text style={styles.listItem}>
                  {item.text}
                </Text>
                </TouchableWithoutFeedback> 
                <Button color= "#00BC8C" title="&#10006;" onPress={() => this.deleteTask(index)} />
              </View>
              <View style={styles.hr} />
            </View>}
        />
        <TextInput
          style={styles.textInput}
          onChangeText={this.changeTextHandler}
          onSubmitEditing={this.addTask}
          value={this.state.text}
          placeholder="+ add Node"
          returnKeyType="done"
          returnKeyLabel="done"
        />
      </View>
    );
  }
}

let Tasks = {
  convertToArrayOfObject(tasks, callback) {
    return callback(
      tasks ? tasks.split("||").map((task, i) => ({ key: i, text: task })) : []
    );
  },
  convertToStringWithSeparators(tasks) {
    return tasks.map(task => task.text).join("||");
  },
  all(callback) {
    return AsyncStorage.getItem("TASKS", (err, tasks) =>
      this.convertToArrayOfObject(tasks, callback)
    );
  },
  save(tasks) {
    AsyncStorage.setItem("TASKS", this.convertToStringWithSeparators(tasks));
  }
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#252525",
    padding: viewPadding,
    paddingTop: 24
  },
  list: {
    width: "100%"
  },
  listItem: {
    paddingTop: 4,
    paddingBottom: 2,
    fontSize: 18,
    color:"#ffff"
  },
  hr: {
    height: 1,
    backgroundColor: "gray"
  },
  listItemCont: {
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "space-between",
    paddingTop: 10,
  },
  textInput: {
    height: 40,
    paddingRight: 10,
    paddingLeft: 10,
    borderColor: "gray",
    borderWidth: isAndroid ? 0 : 1,
    width: "100%",
    color:"#ffff"
  },
appTitle: {
  alignItems:"center",
  justifyContent: "center",
  paddingBottom: 45,
  marginTop: 50,
  fontSize: 25,
  color:"#ffff"
}
});

AppRegistry.registerComponent("NodeList", () => NOdeList);

这是我的第二个屏幕,其中包含 WebView 的组件:

import React, { Component, useState } from "react";
import { BackHandler } from 'react-native';
import { WebView } from 'react-native-webview';
var $ = require( "jquery" );


export default class nodeView extends Component {

constructor(props) {
    super(props);
    this.WEBVIEW_REF = React.createRef();
}

componentDidMount() {
    BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);
}

componentWillUnmount() {
  BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
}

handleBackButton = ()=>{
   this.WEBVIEW_REF.current.goBack();
   return true;
}

onNavigationStateChange(navState) {
  this.setState({
    canGoBack: navState.canGoBack
  });
}

render(){
   return (
    <WebView
        source={{ uri: `https://${user}:${password}@${domain}` }}
        ref={this.WEBVIEW_REF}
        style={{ marginTop: 20 }}
        onNavigationStateChange={this.onNavigationStateChange.bind(this)}
     />
    )

 }
}

【问题讨论】:

    标签: javascript android react-native mobile expo


    【解决方案1】:

    创建 Web 视图。创建控制 WebView 的状态。将侦听器添加到按钮

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-11
      • 2020-05-20
      • 2016-08-25
      • 2020-01-25
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      相关资源
      最近更新 更多