-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebViewTest.js
95 lines (94 loc) · 1.82 KB
/
WebViewTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React, {Component} from 'react'
import {
View,
StyleSheet,
Text,
WebView,
TextInput,
DeviceEventEmitter
} from 'react-native'
import NavigationBar from './js/common/NavigationBar'
const URL = 'https://www.v2ex.com/'
export default class WebViewTest extends Component {
constructor (props) {
super(props)
this.state = {
url: URL,
title: '',
canGoBack: false
}
}
goBack () {
if (this.state.canGoBack) {
this.webView.goBack()
} else {
DeviceEventEmitter.emit('showToast', '到顶了')
}
}
go () {
this.setState({
url: this.text
})
}
onNavigationStateChange (e) {
this.setState({
canGoBack: e.canGoBack,
title: e.title
})
}
render () {
return <View style={styles.container}>
<NavigationBar
title='WebView'
style={{backgroundColor: '#6cf'}}
/>
<View style={styles.row}>
<Text
style={styles.tips}
onPress={
() => {
this.goBack()
}
}
>返回</Text>
<TextInput
style={styles.input}
defaultValue={URL}
onChangeText={text => this.text = text}
/>
<Text
style={styles.tips}
onPress={
() => {
this.go()
}
}
>前往</Text>
</View>
<WebView
ref={webView => this.webView = webView}
source={{uri: this.state.url}}
onNavigationStateChange={(e) => this.onNavigationStateChange(e)}
/>
</View>
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
row: {
flexDirection: 'row',
alignItems: 'center',
margin: 10
},
input: {
height: 40,
flex: 1,
borderWidth: 1,
margin: 1
},
tips: {
fontSize: 20
}
})