{"id":487,"date":"2023-02-25T14:06:27","date_gmt":"2023-02-25T06:06:27","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=487"},"modified":"2023-04-29T20:51:45","modified_gmt":"2023-04-29T12:51:45","slug":"react-native-learning-textinput-component","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/react-native-learning-textinput-component\/","title":{"rendered":"React Native\u5b66\u4e60\u4e4bTextInput\u7ec4\u4ef6"},"content":{"rendered":"<h2>TextInput\u7ec4\u4ef6<\/h2>\n<p>TextInput\uff1a\u6587\u672c\u8f93\u5165\u6846\uff0c\u6587\u672c\u7f16\u8f91\u6846<\/p>\n<h3>TextInput\u7684\u4e3b\u8981\u5c5e\u6027<\/h3>\n<p><!-- more --><\/p>\n<ul>\n<li>autoCapitalize\uff1a\u679a\u4e3e\u7c7b\u578b\uff0c\u53ef\u9009\u503c\u6709none sentences words characters \u5f53\u7528\u6237\u8f93\u5165\u65f6\uff0c\u7528\u4e8e\u63d0\u793a<\/li>\n<li>placeholder\uff1a\u5360\u4f4d\u7b26\uff0c\u5728\u8f93\u5165\u524d\u663e\u793a\u7684\u6587\u672c\u5185\u5bb9<\/li>\n<li>value\uff1a\u6587\u672c\u8f93\u5165\u6846\u7684\u9ed8\u8ba4\u503c<\/li>\n<li>placeholderTextColor\uff1a\u5360\u4f4d\u7b26\u6587\u672c\u7684\u989c\u8272<\/li>\n<li>password\uff1aboolean\u7c7b\u578b\uff0ctrue\u8868\u793a\u5bc6\u7801\u8f93\u5165\u663e\u793a*<\/li>\n<li>multiline\uff1a\u591a\u884c\u8f93\u5165<\/li>\n<li>editable\uff1a\u6587\u672c\u6846\u662f\u5426\u53ef\u8f93\u5165<\/li>\n<li>autoFocus\uff1a\u81ea\u52a8\u805a\u7126<\/li>\n<li>clearButtonMode\uff1a\u679a\u4e3e\u7c7b\u578b\uff0cnever while-editing unless-editing always \u7528\u4e8e\u663e\u793a\u6e05\u9664\u6309\u94ae<\/li>\n<li>maxLength\uff1a\u80fd\u591f\u8f93\u5165\u7684\u6700\u957f\u5b57\u7b26\u6570<\/li>\n<li>enablesReturnKeyAutomatically\uff1a\u5982\u679c\u4e3atrue \u8868\u793a\u6ca1\u6709\u6587\u672c\u65f6\u952e\u76d8\u662f\u4e0d\u80fd\u6709\u8fd4\u56de\u952e\u7684\uff0c\u5176\u9ed8\u8ba4\u503c\u4e3afalse<\/li>\n<li>returnKeyType\uff1a\u679a\u4e3e\u7c7b\u578b default go google join next route search send yahoo done emergency-call \u8868\u793a\u8f6f\u952e\u76d8\u8fd4\u56de\u952e\u663e\u793a\u7684\u5b57\u7b26\u4e32<\/li>\n<li>secureTextEntry\uff1a\u9690\u85cf\u8f93\u5165\u5185\u5bb9\uff0c\u9ed8\u8ba4\u503c\u4e3afalse<\/li>\n<\/ul>\n<h3>TextInput\u7684\u4e3b\u8981\u4e8b\u4ef6<\/h3>\n<ul>\n<li>onChangeText\uff1a\u5f53\u6587\u672c\u8f93\u5165\u6846\u7684\u5185\u5bb9\u53d8\u5316\u65f6\uff0c\u8c03\u7528\u6539\u51fd\u6570\uff1bonChangeText\u63a5\u6536\u4e00\u4e2a\u6587\u672c\u7684\u53c2\u6570\u5bf9\u8c61<\/li>\n<li>onChange\uff1a\u5f53\u6587\u672c\u53d8\u5316\u65f6\uff0c\u8c03\u7528\u8be5\u51fd\u6570<\/li>\n<li>onEndEditing\uff1a\u5f53\u7ed3\u675f\u7f16\u8f91\u65f6\uff0c\u8c03\u7528\u6539\u51fd\u6570<\/li>\n<li>onBlur\uff1a\u5931\u53bb\u7126\u70b9\u89e6\u53d1\u4e8b\u4ef6<\/li>\n<li>onFocus\uff1a\u83b7\u5f97\u7126\u70b9\u65f6\u89e6\u53d1\u4e8b\u4ef6<\/li>\n<li>onSubmitEditing\uff1a\u5f53\u7ed3\u675f\u7f16\u8f91\u540e\uff0c\u70b9\u51fb\u952e\u76d8\u7684\u63d0\u4ea4\u6309\u94ae\u89e6\u53d1\u8be5\u4e8b\u4ef6<\/li>\n<\/ul>\n<p>\u6848\u4f8b\uff1a\u641c\u7d22\u6846<\/p>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n *\/\n&#039;use strict&#039;;\n\nimport React, { Component } from &#039;react&#039;;\nimport {\n    AppRegistry,\n    StyleSheet,\n    Text,\n    TextInput,\n    View\n} from &#039;react-native&#039;;\n\nclass RNAPP extends Component {\n    render() {\n        return (\n            &lt;View style={[styles.flex,styles.topStatus]}&gt;\n                &lt;Search&gt;&lt;\/Search&gt;\n            &lt;\/View&gt;\n        );\n    }\n}\n\nclass Search extends Component {\n    render(){\n        return(\n            &lt;View style={[styles.flex,styles.flexDirection]}&gt;\n                &lt;View style={[styles.flex,styles.input]}&gt;\n                    &lt;TextInput returnKeyType=&quot;search&quot; \/&gt;\n                &lt;\/View&gt;\n\n                &lt;View style={styles.btn}&gt;\n                    &lt;Text style={styles.search}&gt;\u641c\u7d22&lt;\/Text&gt;\n                &lt;\/View&gt;\n            &lt;\/View&gt;\n        );\n    }\n}\n\nconst styles = StyleSheet.create({\n    flex:{\n        flex:1,\n    },\n    flexDirection:{\n        flexDirection:&#039;row&#039;,\n    },\n    topStatus:{\n        marginTop:25,\n    },\n    input:{\n        height:45,\n        borderColor:&#039;red&#039;,\n        borderWidth:1,\n        marginLeft:10,\n        paddingLeft:10,\n        borderRadius:5,\n\n    },\n    btn:{\n        width:45,\n        marginLeft:-5,\n        marginRight:5,\n        backgroundColor:&#039;#23BEFF&#039;,\n        height:45,\n        justifyContent:&#039;center&#039;,\n        alignItems:&#039;center&#039;,\n    },\n    search:{\n        color:&#039;#fff&#039;,\n        fontSize:15,\n        fontWeight:&#039;bold&#039;,\n    },\n});\n\nAppRegistry.registerComponent(&#039;RNAPP&#039;, () =&gt; RNAPP);<\/code><\/pre>\n<h2>TextInput\u81ea\u52a8\u63d0\u793a\u7684\u641c\u7d22\u6846<\/h2>\n<p>\u641c\u7d22\u6846\u52a0\u4e0a\u81ea\u52a8\u63d0\u793a\u7684\u529f\u80fd<\/p>\n<p>\u5f88\u591aapp\u7684\u641c\u7d22\u90fd\u662f\u8fd9\u6837\u7684\uff1a\u6211\u4eec\u8f93\u5165\u4e00\u4e2a\u5173\u952e\u8bcd\u7684\u65f6\u5019\uff0c\u4f1a\u5217\u51fa\u76f8\u5173\u7684\u641c\u7d22\u7ed3\u679c\u5217\u8868\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5b8c\u6210\u8be5\u529f\u80fd\u9700\u8981\u4e00\u4e2a\u641c\u7d22\u670d\u52a1\uff0c\u8fd4\u56deN\u6761\u7ed3\u679c\uff0c\u5e76\u5c06\u5176\u5c55\u793a\u51fa\u6765\uff0c\u8fd9\u91cc\u6211\u4eec\u7528\u9759\u6001\u6570\u636e\u6a21\u62df\u51fa\u6765\u3002<\/p>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n *\/\n&#039;use strict&#039;;\n\nimport React, { Component } from &#039;react&#039;;\nimport {\n    AppRegistry,\n    StyleSheet,\n    PixelRatio,\n    ScrollView,\n    Text,\n    TextInput,\n    View\n} from &#039;react-native&#039;;\n\nvar onePT = 1 \/ PixelRatio.get();\n\nclass RNAPP extends Component {\n    render() {\n        return (\n            &lt;View style={[styles.flex, styles.topStatus]}&gt;\n                &lt;Search&gt;&lt;\/Search&gt;\n            &lt;\/View&gt;\n        );\n    }\n}\n\nclass Search extends Component {\n    constructor(props) {\n        super(props);\n        this.state = {\n            show: false,\n            value: null,\n        };\n    }\n\n    hide(text) {\n        this.setState({\n            show: false,\n            value: text,\n        });\n    }\n\n    getValue(text) {\n        this.setState({\n            show: true,\n            value: text,\n        });\n    }\n\n    render() {\n        return(\n            &lt;View style={styles.flex}&gt;\n                &lt;View style={styles.flexDirection}&gt;\n                    &lt;View style={[styles.flex, styles.input]}&gt;\n                        &lt;TextInput\n                            keyboardType=&quot;web-search&quot;\n                            placeholder=&quot;\u8bf7\u8f93\u5165\u5173\u952e\u8bcd&quot;\n                            value={this.state.value}\n                            onChangeText={this.getValue.bind(this)}\n                            \/&gt;\n                    &lt;\/View&gt;\n                    &lt;View style={styles.btn}&gt;\n                        &lt;Text\n                            style={styles.search}\n                            onPress={this.hide.bind(this, this.state.value)}&gt;\n                            \u641c\u7d22\n                        &lt;\/Text&gt;\n                    &lt;\/View&gt;\n                &lt;\/View&gt;\n\n                {this.state.show?\n                    &lt;View style={styles.result}&gt;\n                        &lt;Text \n                            onPress={this.hide.bind(this, this.state.value + &#039; React Native&#039;)}\n                            style={styles.item}\n                            numberOfLines={1}&gt;\n                            {this.state.value} React Native\n                        &lt;\/Text&gt;\n                        &lt;Text\n                            onPress={this.hide.bind(this, this.state.value + &#039; \u4e2d\u56fd&#039;)}\n                            style={styles.item}\n                            numberOfLines={1}&gt;\n                            {this.state.value} \u4e2d\u56fd\n                        &lt;\/Text&gt;\n                        &lt;Text\n                            onPress={this.hide.bind(this, &#039;APP\u5f00\u53d1 &#039; + this.state.value + &#039; \u7efc\u5408\u5546\u5e97&#039;)}\n                            style={styles.item}\n                            numberOfLines={1}&gt;\n                            APP\u5f00\u53d1 {this.state.value} \u7efc\u5408\u5546\u5e97\n                        &lt;\/Text&gt;\n                        &lt;Text\n                            onPress={this.hide.bind(this, this.state.value + &#039; RNAPP.CC&#039;)}\n                            style={styles.item}\n                            numberOfLines={1}&gt;\n                            {this.state.value} RNAPP.CC\n                        &lt;\/Text&gt;\n                        &lt;Text\n                            onPress={this.hide.bind(this, &#039;RN\u5b66\u4e60 &#039; + this.state.value )}\n                            style={styles.item} \n                            numberOfLines={1}&gt;\n                            RN\u5b66\u4e60 {this.state.value}\n                        &lt;\/Text&gt;\n                    &lt;\/View&gt;\n                : null\n                }\n            &lt;\/View&gt;\n        );\n    }\n}\n\nconst styles = StyleSheet.create({\n    item:{\n        fontSize:16,\n        paddingTop:5,\n        paddingBottom:10,\n    },\n\n    result:{\n        marginTop:onePT,\n        marginLeft:18,\n        marginRight:5,\n        height:200,\n    },\n\n    flex:{\n        flex:1,\n    },\n\n    flexDirection:{\n        flexDirection:&#039;row&#039;,\n    },\n\n    topStatus:{\n        marginTop:25,\n    },\n\n    input:{\n        height:50,\n        borderColor:&#039;red&#039;,\n        borderWidth:1,\n        marginLeft:10,\n        paddingLeft:5,\n        borderRadius:5,\n    },\n\n    btn:{\n        width:45,\n        marginLeft:-5,\n        marginRight:5,\n        backgroundColor:&#039;#23BEFF&#039;,\n        height:50,\n        justifyContent:&#039;center&#039;,\n        alignItems:&#039;center&#039;,\n    },\n\n    search:{\n        color:&#039;#fff&#039;,\n        fontSize:15,\n        fontWeight:&#039;bold&#039;,\n    },\n});\n\nAppRegistry.registerComponent(&#039;RNAPP&#039;, () =&gt; RNAPP);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>TextInput\u7ec4\u4ef6 TextInput\uff1a\u6587\u672c\u8f93\u5165\u6846\uff0c\u6587\u672c\u7f16\u8f91\u6846 TextInput\u7684\u4e3b\u8981\u5c5e\u6027 autoCa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[],"class_list":["post-487","post","type-post","status-publish","format-standard","hentry","category-react-native"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=487"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/487\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}