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
96
97
98
99
100
101
|
import React, { Component } from
'react'
;
import {
AppRegistry,
StyleSheet,
TextInput,
View,
Text,
Keyboard
} from
'react-native'
;
class Main extends Component {
constructor(props){
super
(props);
this
.keyboardDidShowListener =
null
;
this
.keyboardDidHideListener =
null
;
this
.state = { KeyboardShown:
false
};
}
componentWillMount() {
//监听键盘弹出事件
this
.keyboardDidShowListener = Keyboard.addListener(
'keyboardDidShow'
,
this
.keyboardDidShowHandler.bind(
this
));
//监听键盘隐藏事件
this
.keyboardDidHideListener = Keyboard.addListener(
'keyboardDidHide'
,
this
.keyboardDidHideHandler.bind(
this
));
}
componentWillUnmount() {
//卸载键盘弹出事件监听
if
(
this
.keyboardDidShowListener !=
null
) {
this
.keyboardDidShowListener.remove();
}
//卸载键盘隐藏事件监听
if
(
this
.keyboardDidHideListener !=
null
) {
this
.keyboardDidHideListener.remove();
}
}
//键盘弹出事件响应
keyboardDidShowHandler(event) {
this
.setState({KeyboardShown:
true
});
console.log(event.endCoordinates.height);
}
//键盘隐藏事件响应
keyboardDidHideHandler(event) {
this
.setState({KeyboardShown:
false
});
}
//强制隐藏键盘
dissmissKeyboard() {
Keyboard.dismiss();
console.log(
"输入框当前焦点状态:"
+
this
.refs.bottomInput.isFocused());
}
render() {
return
(
onPress={
this
.dissmissKeyboard.bind(
this
)}>
收起键盘
当前键盘状态:{
this
.state.KeyboardShown ?
"打开"
:
"关闭"
}
);
}
}
const styles = StyleSheet.create({
container:{
flex:1,
paddingTop:15
},
flexDirection:{
flexDirection:
'row'
},
inputHeight:{
height:35,
alignItems:
'center'
},
textInputStyle:{
flex:1,
height:35,
fontSize:18,
},
buttonStyle:{
fontSize:20,
color:
'white'
,
width:100,
textAlign:
'center'
,
backgroundColor:
'#4CA300'
},
});
AppRegistry.registerComponent(
'HelloWorld'
, () => Main);
|