{"id":519,"date":"2023-02-25T15:36:48","date_gmt":"2023-02-25T07:36:48","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=519"},"modified":"2023-04-29T20:44:21","modified_gmt":"2023-04-29T12:44:21","slug":"react-native-learning-rn-calling-native-ui-component-methods","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/react-native-learning-rn-calling-native-ui-component-methods\/","title":{"rendered":"React Native\u5b66\u4e60\u4e4bRN\u8c03\u7528\u539f\u751fUI\u7ec4\u4ef6\u65b9\u6cd5"},"content":{"rendered":"<p>\u5b9e\u4f8b1\uff1a\u539f\u751f\u63a7\u4ef6\uff0c\u901a\u8fc7\u5c5e\u6027\u8c03\u7528\u539f\u751f\u63a7\u4ef6\u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u8dd1\u9a6c\u706f\u6548\u679c<\/p>\n<p>\u5b9e\u4f8b2\uff1a\u7ec4\u5408\u63a7\u4ef6\uff0c\u901a\u8fc7\u5c5e\u6027\u8c03\u7528\u7ec4\u5408\u63a7\u4ef6\u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u9009\u4e2d\u72b6\u6001\u6548\u679c<\/p>\n<blockquote>\n<p>\u6ce8\uff1aReact Native\u4e2d\u6ca1\u6709CheckBox\u7ec4\u4ef6<\/p>\n<\/blockquote>\n<p><!-- more --><\/p>\n<p>FocusedTextViewManager.java<\/p>\n<pre><code class=\"language-java\">public class FocusedTextViewManager extends SimpleViewManager&lt;FocusedTextView&gt; {\n\n    private ThemedReactContext context;\n\n    @Override\n    public String getName() {\n        return &quot;FocusedTextView&quot;;\n    }\n\n    @Override\n    protected FocusedTextView createViewInstance(ThemedReactContext reactContext) {\n        context = reactContext;\n        FocusedTextView focusedTextView = new FocusedTextView(context);\n        focusedTextView.setText(&quot;\u6b22\u8fce\u89c2\u4e34 React Native APP \u5f00\u53d1\u793e\u533a\uff0c\u4e00\u4e2a\u9760\u8c31\u7684\u8de8\u5e73\u53f0\u79fb\u52a8\u5f00\u53d1\u6280\u672f\u4ea4\u6d41\u5e73\u53f0&quot;);\n        focusedTextView.setTextColor(Color.BLUE);\n        focusedTextView.setSingleLine(true);\n        focusedTextView.setTextSize(20);\n        focusedTextView.setEllipsize(TextUtils.TruncateAt.MARQUEE);\n        return focusedTextView;\n    }\n\n    \/\/\u66b4\u9732\u5c5e\u6027\n    @ReactProp(name = &quot;text&quot;)\n    public void setText(FocusedTextView view, String text){\n        view.setText(text);\n    }\n}<\/code><\/pre>\n<p>SettingItemViewManager.java<\/p>\n<pre><code class=\"language-java\">public class SettingItemViewManager extends SimpleViewManager&lt;SettingItemView&gt; {\n\n    private ThemedReactContext context;\n\n    @Override\n    public String getName() {\n        return &quot;SettingItemView&quot;;\n    }\n\n    @Override\n    protected SettingItemView createViewInstance(ThemedReactContext reactContext) {\n        context = reactContext;\n        SettingItemView itemView = new SettingItemView(context);\n        itemView.setDesc(&quot;\u81ea\u52a8\u66f4\u65b0\u5df2\u7ecf\u5173\u95ed&quot;);\n        itemView.setTitle(&quot;\u8bbe\u7f6e\u662f\u5426\u81ea\u52a8\u66f4\u65b0&quot;);\n        return itemView;\n    }\n\n    @ReactProp(name = &quot;desc&quot;)\n    public void setDesc(SettingItemView view, String desc){\n        view.setDesc(desc);\n    }\n\n    @ReactProp(name = &quot;title&quot;)\n    public void setTitle(SettingItemView view, String title){\n        view.setTitle(title);\n    }\n\n    @ReactProp(name = &quot;isChecked&quot;)\n    public void setChecked(SettingItemView view, boolean isChecked){\n        view.setChecked(isChecked);\n    }\n}<\/code><\/pre>\n<p>MyNativeModule.java<\/p>\n<pre><code class=\"language-java\">public class MyNativeModule extends ReactContextBaseJavaModule implements ActivityEventListener, LifecycleEventListener {\n\n    private ReactApplicationContext context;\n    public static final int REQUEST_CODE = 1;\n\n    public MyNativeModule(ReactApplicationContext reactContext) {\n        super(reactContext);\n        context = reactContext;\n    }\n\n    @Override\n    public String getName() {\n        \/\/\u4e00\u5b9a\u8981\u8fd4\u56de\u4e00\u4e2a\u540d\u5b57\uff0c\u5728RN\u4ee3\u7801\u91cc\u9762\u9700\u8981\u4f7f\u7528\u8fd9\u4e2a\u540d\u5b57\u6765\u8c03\u7528\u8be5\u7c7b\u7684\u65b9\u6cd5\n        return &quot;MyNativeModule&quot;;\n    }\n\n    \/\/\u51fd\u6570\u4e0d\u80fd\u6709\u8fd4\u56de\u503c\uff0c\u56e0\u4e3a\u88ab\u8c03\u7528\u7684\u539f\u751f\u4ee3\u7801\u662f\u5f02\u6b65\u7684\uff0c\u539f\u751f\u4ee3\u7801\u6267\u884c\u7ed3\u675f\u4e4b\u540e\u53ea\u80fd\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u6216\u8005\u53d1\u9001\u6d88\u606f\u7ed9RN\n    \/\/\u5fc5\u987b\u58f0\u660eReactMethod\u6ce8\u89e3\n    @ReactMethod\n    public void showNativeMsg(String msg) {\n        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();\n    }\n\n    @ReactMethod\n    public void startMyActivity(){\n        Intent intent = new Intent(context, MyActivity.class);\n        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);\n        context.startActivity(intent);\n    }\n}<\/code><\/pre>\n<p>MyReactPackage.java<\/p>\n<pre><code class=\"language-java\">public class MyReactPackage implements ReactPackage {\n\n    @Override\n    public List&lt;NativeModule&gt; createNativeModules(ReactApplicationContext reactContext) {\n        return Arrays.&lt;NativeModule&gt;asList(\n            new MyNativeModule(reactContext)\n        );\n    }\n\n    @Override\n    public List&lt;Class&lt;? extends JavaScriptModule&gt;&gt; createJSModules() {\n        return Collections.emptyList();\n    }\n\n    @Override\n    public List&lt;ViewManager&gt; createViewManagers(ReactApplicationContext reactContext) {\n        return Arrays.&lt;ViewManager&gt;asList(\n            new FocusedTextViewManager(),\n            new SettingItemViewManager()\n        );\n    }\n}<\/code><\/pre>\n<p>MainApplication.java<\/p>\n<pre><code>@Override\nprotected List&lt;ReactPackage&gt; getPackages() {\n    return Arrays.&lt;ReactPackage&gt;asList(\n        new MainReactPackage(), new MyReactPackage()\n    );\n}<\/code><\/pre>\n<p>FocusedTextView.js<\/p>\n<pre><code class=\"language-javascript\">import React, { Component, PropTypes } from &#039;react&#039;;\nimport {\n  requireNativeComponent,\n  Text,\n  View\n} from &#039;react-native&#039;;\n\nlet iface = {\n    name:&#039;FocusedTextView&#039;,\n    propTypes:{\n        text:PropTypes.string,\n        ...View.propTypes\n    },\n};\n\nmodule.exports = requireNativeComponent(&#039;FocusedTextView&#039;, iface);<\/code><\/pre>\n<p>SettingItemView.js<\/p>\n<pre><code class=\"language-javascript\">import React, { Component, PropTypes } from &#039;react&#039;;\nimport {\n  requireNativeComponent,\n  Text,\n  View\n} from &#039;react-native&#039;;\n\nlet iface = {\n    name:&#039;SettingItemView&#039;,\n    propTypes:{\n        desc:PropTypes.string,\n        title:PropTypes.string,\n        isChecked:PropTypes.bool,\n        ...View.propTypes\n    },\n};\n\nmodule.exports = requireNativeComponent(&#039;SettingItemView&#039;, iface);<\/code><\/pre>\n<p>index.android.js<\/p>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n * @flow\n *\/\n\nimport React, { Component } from &#039;react&#039;;\nimport {\n    AppRegistry,\n    StyleSheet,\n    Text,\n    View,\n    NativeModules,\n    TouchableOpacity\n} from &#039;react-native&#039;;\n\nimport FocusedTextView from &#039;.\/FocusedTextView.js&#039;;\nimport SettingItemView from &#039;.\/SettingItemView.js&#039;;\nimport Dimensions from &#039;Dimensions&#039;;\n\nlet width = Dimensions.get(&#039;window&#039;).width;\n\nclass RNAPP extends Component {\n    constructor(props) {\n        super(props);\n        this.state = {\n            isChecked:false,\n            desc:&#039;\u81ea\u52a8\u66f4\u65b0\u5df2\u7ecf\u5173\u95ed&#039;,\n            title:&#039;\u8bbe\u7f6e\u662f\u5426\u81ea\u52a8\u66f4\u65b0&#039;,\n        };\n    }\n\n    render() {\n        return (\n            &lt;View style={styles.container}&gt;\n                &lt;Text style={styles.welcome}&gt;\n                    Welcome to React Native!\n                &lt;\/Text&gt;\n                &lt;Text style={styles.welcome}&gt;\n                    React Native\u6df7\u5408\u539f\u751f\u5f00\u53d1\n                &lt;\/Text&gt;\n                &lt;Text style={styles.instructions}&gt;\n                    Powered by rnapp.cc\n                &lt;\/Text&gt;\n                &lt;Text onPress={this.startMyActivity.bind(this)} style={styles.btn}&gt;\u542f\u52a8\u539f\u751f\u754c\u9762&lt;\/Text&gt;\n                &lt;FocusedTextView\n                    style={{ width: width - 40, height: 30, marginTop: 10 }}\n                    text=&#039;\u6b22\u8fce\u89c2\u4e34 React Native APP \u5f00\u53d1\u793e\u533a\uff0c\u4e00\u4e2a\u9760\u8c31\u7684\u8de8\u5e73\u53f0\u79fb\u52a8\u5f00\u53d1\u6280\u672f\u4ea4\u6d41\u5e73\u53f0&#039;\n                    \/&gt;\n                &lt;TouchableOpacity onPress={this.setAutoUpdate}&gt;\n                    &lt;SettingItemView\n                        style={{ width: width - 20, height: 68, marginTop: 10 }}\n                        title={this.state.title}\n                        desc={this.state.desc}\n                        isChecked={this.state.isChecked}\n                        \/&gt;\n                &lt;\/TouchableOpacity&gt;\n            &lt;\/View&gt;\n        );\n    }\n\n    startMyActivity = () =&gt; {\n        NativeModules.MyNativeModule.startMyActivity();\n    }\n\n    setAutoUpdate = () =&gt; {\n        if(this.state.isChecked){\n            this.setState({\n                isChecked:false,\n                desc:&#039;\u81ea\u52a8\u66f4\u65b0\u5df2\u7ecf\u5173\u95ed&#039;,\n            });\n        } else {\n            this.setState({\n                isChecked:true,\n                desc:&#039;\u81ea\u52a8\u66f4\u65b0\u5f00\u542f\u5566&#039;,\n            });\n        }\n    }\n}\n\nconst styles = StyleSheet.create({\n    container: {\n        flex: 1,\n        justifyContent: &#039;center&#039;,\n        alignItems: &#039;center&#039;,\n        backgroundColor: &#039;#F5FCFF&#039;,\n    },\n    welcome: {\n        fontSize: 20,\n        textAlign: &#039;center&#039;,\n        margin: 10,\n    },\n    instructions: {\n        textAlign: &#039;center&#039;,\n        color: &#039;#333333&#039;,\n        marginBottom: 5,\n    },\n    btn: {\n        marginTop: 20,\n        marginLeft: 10,\n        marginRight: 10,\n        width: 260,\n        height: 35,\n        backgroundColor: &#039;#3BC1FF&#039;,\n        color: &#039;#fff&#039;,\n        lineHeight: 24,\n        fontWeight: &#039;bold&#039;,\n        textAlign: &#039;center&#039;,\n        textAlignVertical:&#039;center&#039;\n    },\n});\n\nAppRegistry.registerComponent(&#039;RNAPP&#039;, () =&gt; RNAPP);<\/code><\/pre>\n<p>MyActivity.java<\/p>\n<pre><code class=\"language-java\">public class MyActivity extends AppCompatActivity {\n    private SettingItemView siv_update;\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_my);\n\n        siv_update = (SettingItemView) this.findViewById(R.id.siv_update);\n        siv_update.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View view) {\n                if (siv_update.isChecked()) {\n                    siv_update.setChecked(false);\n                    siv_update.setDesc(&quot;\u81ea\u52a8\u66f4\u65b0\u5df2\u7ecf\u5173\u95ed&quot;);\n                } else {\n                    siv_update.setChecked(true);\n                    siv_update.setDesc(&quot;\u81ea\u52a8\u66f4\u65b0\u5f00\u542f\u5566\uff01&quot;);\n                }\n            }\n        });\n    }\n\n    public void onBack(View v) {\n        finish();\n    }\n}<\/code><\/pre>\n<p>SettingItemView.java<\/p>\n<pre><code class=\"language-java\">public class SettingItemView extends RelativeLayout {\n\n    private TextView tv_desc;\n    private TextView tv_title;\n    private CheckBox cb_status;\n\n    public SettingItemView(Context context) {\n        super(context);\n        iniView(context);\n    }\n\n    public SettingItemView(Context context, AttributeSet attrs) {\n        super(context, attrs);\n        iniView(context);\n    }\n\n    public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n        iniView(context);\n    }\n\n    private void iniView(Context context) {\n        View.inflate(context, R.layout.setting_item_view, this);\n        cb_status = (CheckBox) this.findViewById(R.id.cb_status);\n        tv_desc = (TextView) this.findViewById(R.id.tv_desc);\n        tv_title = (TextView) this.findViewById(R.id.tv_title);\n    }\n\n    \/**\n     * \u6821\u9a8c\u7ec4\u5408\u63a7\u4ef6\u662f\u5426\u9009\u4e2d\n     * @return\n     *\/\n    public boolean isChecked() {\n        return cb_status.isChecked();\n    }\n\n    \/**\n     * \u8bbe\u7f6e\u7ec4\u5408\u63a7\u4ef6\u7684\u72b6\u6001\n     * @param checked\n     *\/\n    public void setChecked(boolean checked) {\n        cb_status.setChecked(checked);\n    }\n\n    \/**\n     * \u8bbe\u7f6e\u7ec4\u5408\u63a7\u4ef6\u7684\u63cf\u8ff0\u4fe1\u606f\n     * @param text\n     *\/\n    public void setDesc(String text) {\n        tv_desc.setText(text);\n    }\n\n    \/**\n     * \u8bbe\u7f6e\u7ec4\u5408\u63a7\u4ef6\u7684\u6807\u9898\n     * @param title\n     *\/\n    public void setTitle(String title){\n        tv_title.setText(title);\n    }\n}<\/code><\/pre>\n<p>activity_my.xml<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;LinearLayout\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;wrap_content&quot;\n    android:orientation=&quot;vertical&quot;\n    xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    &gt;\n    &lt;TextView\n        android:layout_width=&quot;wrap_content&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:layout_marginTop=&quot;200dp&quot;\n        android:text=&quot;Powered by RNAPP.CC&quot;\n        android:paddingLeft=&quot;10dp&quot;\n        android:paddingRight=&quot;10dp&quot;\n        android:textSize=&quot;20sp&quot;\n        android:layout_gravity=&quot;center_horizontal&quot;\/&gt;\n    &lt;Button\n        android:onClick=&quot;onBack&quot;\n        android:layout_width=&quot;wrap_content&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:layout_gravity=&quot;center_horizontal&quot;\n        android:layout_marginTop=&quot;20dp&quot;\n        android:background=&quot;#3BC1FF&quot;\n        android:paddingLeft=&quot;10dp&quot;\n        android:paddingRight=&quot;10dp&quot;\n        android:text=&quot;\u539f\u751f\u6309\u94ae - \u70b9\u51fb\u9000\u51fa&quot;\n        android:textSize=&quot;16sp&quot;\n        android:textColor=&quot;#FFFFFF&quot;\n        \/&gt;\n    &lt;cc.rnapp.view.FocusedTextView\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:layout_marginTop=&quot;20dp&quot;\n        android:layout_marginLeft=&quot;10dp&quot;\n        android:layout_marginRight=&quot;10dp&quot;\n        android:text=&quot;\u6b22\u8fce\u89c2\u4e34 React Native APP \u5f00\u53d1\u793e\u533a\uff0c\u4e00\u4e2a\u9760\u8c31\u7684\u8de8\u5e73\u53f0\u79fb\u52a8\u5f00\u53d1\u6280\u672f\u4ea4\u6d41\u5e73\u53f0&quot;\n        android:textSize=&quot;18sp&quot;\n        android:textColor=&quot;#FF0000&quot;\n        android:ellipsize=&quot;marquee&quot;\n        android:singleLine=&quot;true&quot;\n        \/&gt;\n    &lt;cc.rnapp.view.SettingItemView\n        android:id=&quot;@+id\/siv_update&quot;\n        android:layout_width=&quot;wrap_content&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:layout_marginTop=&quot;20dp&quot;\n        \/&gt;\n&lt;\/LinearLayout&gt;<\/code><\/pre>\n<p>setting_item_view.xml<\/p>\n<pre><code class=\"language-xml\">&lt;RelativeLayout\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;60dp&quot;\n    xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    &gt;\n    &lt;TextView\n        android:id=&quot;@+id\/tv_title&quot;\n        android:layout_width=&quot;wrap_content&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:layout_marginLeft=&quot;10dp&quot;\n        android:layout_marginRight=&quot;10dp&quot;\n        android:text=&quot;\u8bbe\u7f6e\u662f\u5426\u81ea\u52a8\u66f4\u65b0&quot;\n        android:textColor=&quot;#000000&quot;\n        android:textSize=&quot;20sp&quot;\/&gt;\n    &lt;TextView\n        android:id=&quot;@+id\/tv_desc&quot;\n        android:layout_width=&quot;wrap_content&quot;\n        android:layout_height=&quot;wrap_content&quot; \n        android:layout_below=&quot;@+id\/tv_title&quot;\n        android:layout_marginLeft=&quot;10dp&quot;\n        android:text=&quot;\u81ea\u52a8\u66f4\u65b0\u5df2\u7ecf\u5173\u95ed&quot;\n        android:textColor=&quot;#88000000&quot;\n        android:textSize=&quot;18sp&quot;\n        \/&gt;\n    &lt;CheckBox\n        android:id=&quot;@+id\/cb_status&quot;\n        android:layout_width=&quot;wrap_content&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:layout_alignParentRight=&quot;true&quot;\n        android:layout_marginRight=&quot;10dp&quot;\n        android:layout_centerVertical=&quot;true&quot;\n        android:clickable=&quot;false&quot;\n        android:focusable=&quot;false&quot;\n        \/&gt;\n    &lt;View\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;0.2dp&quot;\n        android:layout_marginLeft=&quot;5dp&quot;\n        android:layout_marginRight=&quot;5dp&quot;\n        android:layout_alignParentBottom=&quot;true&quot;\n        android:background=&quot;#000000&quot;\n        \/&gt;\n&lt;\/RelativeLayout&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u4f8b1\uff1a\u539f\u751f\u63a7\u4ef6\uff0c\u901a\u8fc7\u5c5e\u6027\u8c03\u7528\u539f\u751f\u63a7\u4ef6\u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u8dd1\u9a6c\u706f\u6548\u679c \u5b9e\u4f8b2\uff1a\u7ec4\u5408\u63a7\u4ef6\uff0c\u901a\u8fc7\u5c5e\u6027\u8c03\u7528\u7ec4\u5408\u63a7\u4ef6\u7684\u65b9\u6cd5\uff0c\u5b9e [&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-519","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\/519","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=519"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/519\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}