YYPOST群发软件 发表于 2025-2-14 09:33:55

Activity生命周期:管理应用界面的法则

<p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">在安卓开发中,<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>&nbsp;是一个非常重要的组件,主要用于以下几个方面:</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">用户界面展示</span>:每个&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>&nbsp;通常代表一个完整的屏幕或界面,用户与应用的交互主要通过&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>&nbsp;实现。例如,一个邮件应用可能包含一个显示邮件列表的&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>,以及另一个用于阅读邮件内容的&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">生命周期管理</span>:<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>&nbsp;有一个生命周期,即创建、启动、恢复、暂停、停止和销毁等状态。开发者可以通过重写&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>&nbsp;的生命周期方法,来管理资源、保存状态等,确保应用的流畅运行和良好的用户体验。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">组件交互</span>:<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>&nbsp;可以启动其他&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>,也可以向其他组件(如&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Service</code>、<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">BroadcastReceiver</code>)发送消息,实现组件间的交互。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">事件处理</span>:<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>&nbsp;可以响应用户的输入事件,比如触摸屏幕、点击按钮等,并作出相应的处理。</p></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">在你的代码示例中,<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">activity</code>&nbsp;类模拟了安卓的&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>,主要负责创建和显示用户界面、管理视图组件以及处理点击事件等。通过&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">onCreate</code>&nbsp;方法进行初始化,<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">setContentView</code>&nbsp;方法设置界面布局,<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">findViewById</code>&nbsp;方法查找特定的视图组件,并通过这些组件的&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">setOnClickListener</code>&nbsp;方法来处理用户点击操作。此外,代码还展示了如何在新线程中运行耗时操作,以及如何确保在 UI 线程中更新用户界面。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第一个例子说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().close()</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=a92dae8b5537d57e2a2eb2ad6cba4240" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
    var view = ac.inflate(
      `<span class="javascript" style="box-sizing: border-box;">&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">Button</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_width</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_height</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:id</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/button1"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"关闭"</span> /&gt;</span></span>
      &lt;/LinearLayout&gt;</span>`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>
    );

    ac.setContentView(view);
    var btn1 = ac.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
    btn1.setOnClickListener(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
      ac.close(); <span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 关闭活动的操作
    });
});

<span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 显示活动
ac.show();
</code></pre><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">创建活动对象</span>:<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">var ac = new activity();</code>&nbsp;创建一个新的活动对象。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">设置活动布局</span>:通过&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac.inflate</code>&nbsp;方法设置 XML 布局,XML 定义了一个按钮。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">设置按钮的点击事件</span>:找到按钮&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">button1</code>,并设置点击监听器。在监听器中调用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac.close()</code>&nbsp;方法来关闭活动。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">显示活动</span>:通过&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac.show()</code>&nbsp;方法显示活动。</p></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">请确保你的&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">activity</code>&nbsp;类定义了&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">close</code>&nbsp;和&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">show</code>&nbsp;方法,否则调用会失败。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第二个例子说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().findViewById(‘button1’)</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=9185ba147db9f620601534d076fc6021" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
    var view = ac.inflate(
      `<span class="javascript" style="box-sizing: border-box;">&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">Button</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_width</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_height</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:id</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/button1"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"关闭"</span> /&gt;</span></span>
      &lt;/LinearLayout&gt;</span>`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>
    );

    ac.setContentView(view);
    var btn1 = ac.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
    btn1.setOnClickListener(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
      ac.close();
    });
});

ac.show();
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">以上代码是在活动显示时创建一个按钮,点击按钮后关闭当前活动。<br style="box-sizing: border-box;">ac.close(); 是用于关闭或销毁活动的假设函数。<br style="box-sizing: border-box;">findViewById(‘button1’) 查找布局中定义的按钮并返回其引用,以便在代码中操控。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这些代码片段和概念是用于说明布局和事件处理如何基于活动对象进行操作的,具体实现和功能是需要在对应的框架中支持的。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第三个例子说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().findWebViewById(‘web1’)</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=b1e4d2dd9ece6ec2e6a8abff4995f20a" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
    var view = ac.inflate(
      `<span class="javascript" style="box-sizing: border-box;">&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">WebView</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_width</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_height</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:id</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/web1"</span> /&gt;</span></span>
      &lt;/LinearLayout&gt;</span>`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>
    );


    ac.setContentView(view);
    var webView = ac.findWebViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">'web1'</span>);

    <span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 在 WebView 中加载一个 URL
    webView.loadUrl(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"https://www.baidu.com"</span>);
});


ac.show();
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">说明</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">activity 类的实例化: 创建了一个 activity 对象。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">onCreate 方法: 设置界面的布局,包含一个 WebView。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">findWebViewById 方法: 用来获取布局中 ID 为 web1 的 WebView 实例。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">WebView 操作: 对找到的 WebView 执行操作,例如加载一个网页。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这种方法通常用于动态获取并操作 UI 控件,但请注意,具体细节和方法需要根据实际的 Android 或自定义 API 而定。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第四个例子说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().getActivity()</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=c50987c95c40ad698e43aaf3c2c8cdf6" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
    var view = ac.inflate(
      `<span class="javascript" style="box-sizing: border-box;">&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">Button</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_width</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_height</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:id</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/button1"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"开始"</span> /&gt;</span></span>
      &lt;/LinearLayout&gt;</span>`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>
    );


    ac.setContentView(view);
    var btn1 = ac.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
    btn1.setOnClickListener(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
      <span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 耗时代码必须在新线程中运行
      var t1 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> thread();
      t1.runJsCode(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
            <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"测试ui"</span>);
      }, <span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"线程1"</span>);
    });
});


ac.show();
</code></pre><ul style="margin-bottom: 1.5em; margin-left: 14px; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">activity()</span>:此类的实例化用于管理界面生命周期和视图。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">onCreate</span>:在活动创建时执行的回调函数,用于初始化视图。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">inflate</span>:加载 XML 布局并生成视图层次结构。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">setContentView</span>:将指定的视图作为活动的内容视图。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">findViewById</span>:获取布局中的特定视图,此例用于按钮。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">setOnClickListener</span>:设置按钮点击事件的监听器。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Thread</span>:创建新线程以避免阻塞主线程。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">runJsCode</span>:在新线程中执行指定的 JavaScript 代码。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">show</span>:显示活动。</li></ul><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">如果&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">activity</code>、<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">thread</code>&nbsp;以及其他对象未定义,确保相关的库或框架已经正确引入并配置。<br style="box-sizing: border-box;">第五个例子说明:</p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(97, 174, 238);">Activity</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.rootView = {
      <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟视图对象</span>
      <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">render</span>: <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
            <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Rendering the root view!"</span>);
      }
    };
}

Activity.prototype.getView = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</span> <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.rootView;
};

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 示例</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> Activity();
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> rootView = ac.getView();
rootView.render();<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 输出: Rendering the root view!</span>
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">说明</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">Activity 类: 我们定义了一个简单的Activity类,它内部包含一个rootView对象,用来模拟根视图。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">getView 方法: 此方法返回rootView,代表整个活动的视图。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">使用示例: 我们创建了Activity的一个实例,调用getView方法获取视图,然后调用render方法来模拟渲染该视图。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第六个例子说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().inflate(‘’,ViewGroup)</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=211694fafc3b4a1c288902f9f934ba4d" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();


ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
    var view = ac.inflate(
      `<span class="javascript" style="box-sizing: border-box;">&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">TextView</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_width</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_height</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Hello, World!AIWROK"</span> /&gt;</span></span>
      &lt;/LinearLayout&gt;</span>`,
      <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span> <span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 在这里传入父ViewGroup,如果有需要的话
    );


    ac.setContentView(view);
});


ac.show();
</code></pre><h3 id="h3--" style="margin-top: 1em; margin-bottom: 1.5em; box-sizing: border-box; position: relative; line-height: 1.75; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; font-size: 1.25em !important;"><a name="说明:" class="reference-link" style="color: rgb(65, 131, 196); transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;"></a>说明:</h3><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Activity 创建</span></p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">var ac = new activity();</code>:创建一个新的活动实例。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">onCreate 方法</span></p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac.onCreate(() =&gt; { ... })</code>&nbsp;定义当活动创建时的行为。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">视图填充和设置</span></p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac.inflate(...)</code>:通过XML字符串来定义布局视图。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac.setContentView(view);</code>:将填充的视图设置为活动的内容视图。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">按钮查找和监听器</span></p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">var btn1 = ac.findViewById("button1");</code>:找到布局中定义的按钮。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">btn1.setOnClickListener(() =&gt; { ... })</code>:为按钮设置点击监听器。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">线程</span></p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">var t1 = new thread();</code>:创建一个新的线程。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">t1.runJsCode(...);</code>:在线程中运行JavaScript代码。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">显示活动</span></p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac.show();</code>:显示活动的界面。</li></ul></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第七个例子说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().inflateSxml(‘’,ViewGroup)</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=c6c153aae65a8af0e06f0a242dede9f8" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();

ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {

    <span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 使用 XML 字符串创建视图
    var view = ac.inflateSxml(`<span class="javascript" style="box-sizing: border-box;">
      &lt;vertical&gt;
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">button</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">id</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第一个按钮"</span>/&gt;</span></span>
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">button</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第二个按钮"</span>/&gt;</span></span>
      &lt;/vertical&gt;
    </span>`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>);

    <span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 设置活动的内容视图
    ac.setContentView(view);

    <span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 寻找按钮并设置点击事件监听器
    var btn1 = ac.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
    btn1.setOnClickListener(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {

      <span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 事件处理代码需要运行在新线程中
      var t1 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> thread();
      t1.runJsCode(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
            println(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"测试UI"</span>);
      }, <span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"线程1"</span>);

    });

});

<span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 显示活动
ac.show();
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这个示例展示了如何使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">inflateSxml</code>&nbsp;方法将 XML 转换为视图对象。以下是代码的详细说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><span style="box-sizing: border-box; font-weight: 700;">创建活动实例</span></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs csharp" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
</code></pre><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">在&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">onCreate</code>&nbsp;方法中装载布局</span></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
    var view = ac.inflateSxml(`<span class="javascript" style="box-sizing: border-box;">
      &lt;vertical&gt;
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">button</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">id</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第一个按钮"</span>/&gt;</span></span>
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">button</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第二个按钮"</span>/&gt;</span></span>
      &lt;/vertical&gt;</span>`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>);
    ac.setContentView(view);
});
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">inflateSxml</code>:用于解析 XML 字符串并转换为视图对象。<br style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">setContentView</code>:设置当前活动的内容视图。</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">设置按钮点击事件</span></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var btn1 = ac.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
btn1.setOnClickListener(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
    <span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 耗时代码必须在新线程中运行
    var t1 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> thread();
    t1.runJsCode(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
      println(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"测试UI"</span>)
    }, <span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"线程1"</span>);
});
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">findViewById</code>:获取视图中 ID 为&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">button1</code>&nbsp;的按钮。<br style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">setOnClickListener</code>:设置按钮的点击事件。<br style="box-sizing: border-box;">使用新线程运行耗时操作,确保界面流畅。</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">显示活动</span></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs less" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">ac</span><span class="hljs-selector-class" style="box-sizing: border-box; color: rgb(209, 154, 102);">.show</span>();
</code></pre><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">show</code>:用于显示指定的活动。</p></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这个示例展示了如何使用 JavaScript 模拟 Android 活动中的基本操作,包括布局加载和事件处理。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第八个例子说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().loadLayoutFile(‘’)</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=d3186198fe8a2845229d62929422bab3" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs csharp" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 初始化一个activity页面</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 加载XML布局文件并显示UI</span>
ac.loadLayoutFile(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"/代码/layout.xml"</span>);

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 可以在加载完布局后进行其他UI控件的操作</span>
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 例如,找到一个按钮并设置点击事件</span>
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// var button = ac.findViewById("button_id");</span>
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// button.setOnClickListener(() =&gt; {</span>
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">//   printl("按钮被点击");</span>
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// });</span>

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 显示activity</span>
ac.show();
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">请根据实际需要替换”/代码/layout.xml”为你的XML文件的实际路径。上述代码示例需要在支持这一API的环境下执行。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第九个例子说明:</p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs xml" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">new activity().loadSXML(`    <span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">vertical</span>&gt;</span>
      <span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">button</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第一个按钮"</span>/&gt;</span>
      <span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">button</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第二个按钮"</span>/&gt;</span>
    <span class="hljs-tag" style="box-sizing: border-box;">&lt;/<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">vertical</span>&gt;</span>`)
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">loadSXML 方法:用于加载类 XML 布局代码。它支持较简单的 UI 布局。<br style="box-sizing: border-box;">参数 arg0:接受类 XML 布局代码的字符串。<br style="box-sizing: border-box;">示例中,创建了一个 vertical 布局,其中包含两个按钮,每个按钮显示不同的文本。<br style="box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=5f8bacb3ed535a18ad1dd89f88bff14d" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"><br style="box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=5342aa5f1a87ed60e437b0399cd1287e" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第十个例子说明:</p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs csharp" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 加载xml文件并显示UI</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
ac.loadLayoutFile(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"/代码/layout.xml"</span>);

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 加载简版xml</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
ac.loadSXML(`
    &lt;vertical&gt;
      &lt;button text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第一个按钮"</span>/&gt;
      &lt;button text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第二个按钮"</span>/&gt;
    &lt;/vertical&gt;
`);

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 加载xml</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
ac.loadXML(`
&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
    android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
    android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
    android:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
    android:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
    &lt;Button
      android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
      android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
      android:id=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/button1"</span>
      android:text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"开始"</span> /&gt;
&lt;/LinearLayout&gt;
`);
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这段代码展示了如何通过三种不同的方式加载和显示XML布局文件。以下是每种方式的说明:</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">加载XML文件并显示UI:</span></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs csharp" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
ac.loadLayoutFile(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"/代码/layout.xml"</span>);
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这段代码创建了一个新的活动<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">activity</code>,并通过<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">loadLayoutFile</code>方法加载位于路径<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">/代码/layout.xml</code>的布局文件,用于显示UI。</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">加载简版XML:</span></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs xml" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var ac = new activity();
ac.loadSXML(`
    <span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">vertical</span>&gt;</span>
      <span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">button</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第一个按钮"</span>/&gt;</span>
      <span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">button</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第二个按钮"</span>/&gt;</span>
    <span class="hljs-tag" style="box-sizing: border-box;">&lt;/<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">vertical</span>&gt;</span>
`);
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这段代码展示了如何在代码中直接定义XML布局字符串并加载显示。使用<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">loadSXML</code>方法可以加载简版的XML布局。在这个例子中,定义了一个垂直布局(<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">vertical</code>),其中包含两个按钮(<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">button</code>)。</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">加载XML:</span></p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs xml" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var ac = new activity();
ac.loadXML(`
<span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">LinearLayout</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">xmlns:android</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
    <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_width</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
    <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_height</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
    <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:orientation</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
    <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:padding</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;</span>
    <span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">Button</span>
      <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_width</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
      <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_height</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
      <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:id</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/button1"</span>
      <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"开始"</span> /&gt;</span>
<span class="hljs-tag" style="box-sizing: border-box;">&lt;/<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">LinearLayout</span>&gt;</span>
`);
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这段代码使用<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">loadXML</code>方法加载一段标准的Android XML布局定义。在这个布局中,使用了<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">LinearLayout</code>作为根布局,它是一个垂直的线性布局,包含一个按钮。按钮的宽度设为<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">match_parent</code>,高度为<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">wrap_content</code>,显示文字为“开始”,“id”用于唯一标识。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这种技术可以在自动化脚本和应用程序开发中快速创建UI布局。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=1a1e3e0bf4d1d6439b2105edbd0796b8" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第十一个例子说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().onCreate(Function)</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=2b4a734c34a3a9ed5fcae8b035bc0dcb" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();


ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {


    var view = ac.inflateSxml(`<span class="javascript" style="box-sizing: border-box;">
      &lt;vertical&gt;
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">button</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">id</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"点击我"</span>/&gt;</span></span>
      &lt;/vertical&gt;
    </span>`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>);


    ac.setContentView(view);


    var btn1 = ac.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
    btn1.setOnClickListener(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
      println(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"按钮被点击了"</span>);
    });


});


ac.show();
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这段代码创建了一个 Android 活动,并在活动中创建了一个简单的界面和事件监听器。以下是代码的详细说明:</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">创建活动实例</span>:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs csharp" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这里创建了一个新的&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">activity</code>&nbsp;对象实例,代表一个活动(Activity)。</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">定义活动创建时的行为</span>:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
    ...
});
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">onCreate</code>&nbsp;方法指定活动创建时要执行的代码。</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">加载布局</span>:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs xml" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var view = ac.inflateSxml(`
    <span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">vertical</span>&gt;</span>
      <span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">button</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">id</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"点击我"</span>/&gt;</span>
    <span class="hljs-tag" style="box-sizing: border-box;">&lt;/<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">vertical</span>&gt;</span>
`, null);
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">inflateSxml</code>&nbsp;方法以 XML 字符串的形式定义了一个简单的用户界面布局,其中包含一个垂直布局和一个按钮。</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">设置内容视图</span>:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs less" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">ac</span><span class="hljs-selector-class" style="box-sizing: border-box; color: rgb(209, 154, 102);">.setContentView</span>(view);
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">设置当前活动的内容视图为刚才创建的布局。</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">查找按钮并设置点击事件监听器</span>:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var btn1 = ac.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
btn1.setOnClickListener(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
    println(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"按钮被点击了"</span>);
});
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">查找布局中的按钮,并为其设置一个点击事件监听器,按钮点击时会在控制台输出 “按钮被点击了”。</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">显示活动</span>:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs less" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">ac</span><span class="hljs-selector-class" style="box-sizing: border-box; color: rgb(209, 154, 102);">.show</span>();
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">显示活动,使用户界面可见。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">此代码展示了如何在 Android 中创建简单的用户界面并响应用户的点击事件。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第十二个例子说明:</p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs kotlin" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">new activity().runOnUiThread(function <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-title" style="box-sizing: border-box; color: rgb(97, 174, 238);">fun</span><span class="hljs-params" style="box-sizing: border-box;">()</span></span>{
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">//这里写代码</span>
printl(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">'UI线程执行代码'</span>)

})
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=85563571b4e351fd27cf2741cb1cbe81" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();


ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
    var view = ac.inflateSxml(`<span class="javascript" style="box-sizing: border-box;">
      &lt;vertical&gt;
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">button</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">id</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span> <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"点击我"</span>/&gt;</span></span>
      &lt;/vertical&gt;
    </span>`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>);


    ac.setContentView(view);


    var btn1 = ac.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
    btn1.setOnClickListener(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
      <span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 点击按钮时在 UI 线程中运行代码
      ac.runOnUiThread(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
            printl(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"UI线程执行代码"</span>);
      });
    });
});


ac.show();
</code></pre><h3 id="h3-u8BF4u660E" style="margin-top: 1em; margin-bottom: 1.5em; box-sizing: border-box; position: relative; line-height: 1.75; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; font-size: 1.25em !important;"><a name="说明" class="reference-link" style="color: rgb(65, 131, 196); transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;"></a>说明</h3><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Activity 创建</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">创建一个新的&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">activity</code>&nbsp;实例,并在其&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">onCreate</code>&nbsp;方法中定义初始化代码。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">视图布局</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">inflateSxml</code>&nbsp;定义一个简单的布局,包括一个按钮。</li><li style="list-style: disc; box-sizing: border-box;">调用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">setContentView</code>&nbsp;方法将布局设置为当前活动的内容视图。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">按钮点击事件</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">findViewById</code>&nbsp;方法找到布局中的按钮。</li><li style="list-style: disc; box-sizing: border-box;">给按钮设置一个点击事件监听器。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">运行 UI 线程代码</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">在按钮点击时,调用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">runOnUiThread</code>&nbsp;方法,这将确保指定的代码在 UI 线程上执行。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">println</code>&nbsp;方法用于输出一条日志消息,表示代码在 UI 线程中执行。</li></ul></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第十三个例子说明:</p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs less" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">new</span> <span class="hljs-selector-tag" style="box-sizing: border-box; color: rgb(224, 108, 117);">activity</span>()<span class="hljs-selector-class" style="box-sizing: border-box; color: rgb(209, 154, 102);">.setContentView</span>(View)
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=9dc076677e30c8a45c63244006abf048" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(97, 174, 238);">Activity</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{}

Activity.prototype.onCreate = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">callback</span>) </span>{
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Activity Created"</span>);
    callback();
};

Activity.prototype.runOnUiThread = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">callback</span>) </span>{
    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟在UI线程中执行callback</span>
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">setTimeout</span>(callback, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>);
};

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 添加setContentView方法</span>
Activity.prototype.setContentView = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">view</span>) </span>{
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"视图对象已设置:"</span>, view);
};

<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 创建一个新的活动实例</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> myActivity = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> Activity();

myActivity.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"回调函数被调用"</span>);

    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 设置视图对象</span>
    myActivity.setContentView(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Main View"</span>);

    myActivity.runOnUiThread(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
      <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"UI线程中运行的代码"</span>);
    });
});
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这个代码定义了一个模拟的&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>&nbsp;类,并为其添加了&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">onCreate</code>&nbsp;和&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">runOnUiThread</code>&nbsp;方法。以下是代码的逐步解析:</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">定义Activity类</span>:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(97, 174, 238);">Activity</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{}
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这是一个构造函数,用于创建&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>&nbsp;的实例。</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">定义onCreate方法</span>:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">Activity.prototype.onCreate = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">callback</span>) </span>{
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Activity Created"</span>);
    callback();
};
</code></pre><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;">这个方法接受一个回调函数作为参数。当被调用时,它会输出 “Activity Created”,然后执行传入的回调函数。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">定义runOnUiThread方法</span>:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">Activity.prototype.runOnUiThread = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">callback</span>) </span>{
    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟在UI线程中执行callback</span>
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">setTimeout</span>(callback, <span class="hljs-number" style="box-sizing: border-box; color: rgb(209, 154, 102);">0</span>);
};
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">此方法也接受一个回调函数作为参数。借助&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">setTimeout</code>&nbsp;模拟在UI线程中异步执行回调。</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">创建实例并调用方法</span>:</p><pre style="overflow-wrap: normal; box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs javascript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> myActivity = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> Activity();

myActivity.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
    <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"回调函数被调用"</span>);
    myActivity.runOnUiThread(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
      <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">console</span>.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"UI线程中运行的代码"</span>);
    });
});
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">创建&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code>&nbsp;的实例&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">myActivity</code>。<br style="box-sizing: border-box;">调用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">onCreate</code>,传入一个回调函数,在回调中输出 “回调函数被调用”。<br style="box-sizing: border-box;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">runOnUiThread</code>&nbsp;运行另一个回调,模拟在UI线程中运行,并输出 “UI线程中运行的代码”。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这个代码展示了如何使用回调函数和异步执行来模拟Activity生命周期中的某些行为。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">第十四个例子说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().show()</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&amp;sign=ad8d068f0e9c00e0fc558ab2142470e0" alt="" style="box-sizing: border-box; border: 0px; max-width: 100%;"></p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs coffeescript" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">var ac = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
    var view = ac.inflate(
      `<span class="javascript" style="box-sizing: border-box;">&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
            <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android</span>:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
            <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;">&lt;<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">Button</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_width</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:layout_height</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:id</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/button1"</span>
                <span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">android:text</span>=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"开始"</span> /&gt;</span></span>
      &lt;/LinearLayout&gt;</span>`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>
    );


    ac.setContentView(view);
    var btn1 = ac.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
    btn1.setOnClickListener(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
      <span class="hljs-regexp" style="box-sizing: border-box; color: rgb(152, 195, 121);">//</span> 耗时代码必须在新线程中运行
      var t1 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> thread();
      t1.runJsCode(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="box-sizing: border-box;">()</span> =&gt;</span> {
            printl(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"测试ui"</span>);
      }, <span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"线程1"</span>);
    });
});


ac.show();
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这段代码定义了一个新的活动<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">activity</code>,并在活动创建时设置了一个简单的用户界面。用户界面包含一个按钮,点击按钮时,会在一个新线程中执行代码,输出“测试ui”。具体说明如下:</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">活动对象创建:</span>&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">var ac = new activity();</code>&nbsp;创建新的活动实例。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">onCreate 方法:</span>&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac.onCreate(() =&gt; {...})</code>&nbsp;定义活动的创建逻辑。在这里,你通过&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">inflate</code>&nbsp;方法设置了一个线性布局&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">LinearLayout</code>,包含一个按钮。</p></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">按钮点击监听:</span></p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">findViewById</code>&nbsp;找到按钮。</li><li style="list-style: disc; box-sizing: border-box;">使用&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">setOnClickListener</code>&nbsp;为按钮设置点击事件监听器。</li><li style="list-style: disc; box-sizing: border-box;">点击时,创建一个新线程&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">new thread()</code>,在其中执行&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">runJsCode</code>&nbsp;方法,这个方法将会输出文本“测试ui”。</li></ul></li><li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">显示活动:</span>&nbsp;<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac.show();</code>&nbsp;最后这行代码用于显示这个活动。</li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">总体来说,这段代码实现了一个简单的UI交互,当用户点击按钮时,会在后台线程中打印“测试ui”。请注意,确保<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; margin-right: 4px; margin-left: 5px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">activity</code>类及相关方法在你的环境中已正确定义,否则可能会出现错误。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">以下所有例子代码集合起来,让后面AI智能体抓取例子。</p><pre style="overflow-wrap: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 12px; line-height: 1.6; font-family: &quot;YaHei Consolas Hybrid&quot;, Consolas, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Helvetica, monospace, monospace; box-sizing: border-box; margin-bottom: 1.5em; padding: 1em; overflow: auto; background: rgb(56, 69, 72); border-radius: 4px; border: 1px solid rgb(221, 221, 221); position: relative; color: rgb(209, 210, 210);"><code class="hljs php" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;"><span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 定义 activity 类</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(97, 174, 238);">activity</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
    this.rootView = <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>;
}


activity.prototype.onCreate = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">callback</span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Activity Created"</span>);
    callback();
};


activity.prototype.setContentView = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">view</span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"视图对象已设置:"</span>, view);
    this.rootView = view;
};


activity.prototype.show = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"显示活动"</span>);
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">if</span> (this.rootView) {
      console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"活动视图:"</span>, this.rootView);
    } <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">else</span> {
      console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"没有设置视图对象"</span>);
    }
};


activity.prototype.close = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"关闭活动"</span>);
};


activity.prototype.inflate = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">layoutString, <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">parent</span></span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"加载布局:"</span>, layoutString);
    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 这里假设返回一个视图对象</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</span> layoutString;
};


activity.prototype.inflateSxml = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">layoutString, <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">parent</span></span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"加载简版布局:"</span>, layoutString);
    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 这里假设返回一个视图对象</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</span> layoutString;
};


activity.prototype.loadLayoutFile = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">path</span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"加载布局文件:"</span>, path);
    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 这里假设返回一个视图对象</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</span> path;
};


activity.prototype.loadSXML = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">layoutString</span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"加载简版布局文件:"</span>, layoutString);
    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 这里假设返回一个视图对象</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</span> layoutString;
};


activity.prototype.loadXML = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">layoutString</span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"加载标准布局文件:"</span>, layoutString);
    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 这里假设返回一个视图对象</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</span> layoutString;
};


activity.prototype.findViewById = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">id</span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"查找视图组件:"</span>, id);
    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 这里假设返回一个视图组件对象</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</span> {
      setOnClickListener: <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">callback</span>) </span>{
            console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"设置点击事件监听器"</span>);
            callback();
      }
    };
};


activity.prototype.findWebViewById = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">id</span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"查找 WebView 组件:"</span>, id);
    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 这里假设返回一个 WebView 组件对象</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</span> {
      loadUrl: <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">url</span>) </span>{
            console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"加载 URL:"</span>, url);
      }
    };
};


activity.prototype.runOnUiThread = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">callback</span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"在 UI 线程中运行代码"</span>);
    callback();
};


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 定义 thread 类</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(97, 174, 238);">thread</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{}


thread.prototype.runJsCode = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;">callback, threadName</span>) </span>{
    console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"运行线程:"</span>, threadName);
    callback();
};


<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 示例代码整合</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(97, 174, 238);">main</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第一个例子</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac1 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
    ac1.onCreate(() =&gt; {
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> view1 = ac1.inflate(
            `&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
                android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                android:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
                android:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
                &lt;Button
                  android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                  android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                  android:id=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/button1"</span>
                  android:text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"关闭"</span> /&gt;
            &lt;/LinearLayout&gt;`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>
      );


      ac1.setContentView(view1);
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> btn1 = ac1.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
      btn1.setOnClickListener(() =&gt; {
            ac1.close(); <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 关闭活动的操作</span>
      });
    });


    ac1.show();


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第二个例子</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac2 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
    ac2.onCreate(() =&gt; {
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> view2 = ac2.inflate(
            `&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
                android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                android:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
                android:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
                &lt;Button
                  android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                  android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                  android:id=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/button1"</span>
                  android:text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"关闭"</span> /&gt;
            &lt;/LinearLayout&gt;`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>
      );


      ac2.setContentView(view2);
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> btn1 = ac2.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
      btn1.setOnClickListener(() =&gt; {
            ac2.close();
      });
    });


    ac2.show();


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第三个例子</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac3 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
    ac3.onCreate(() =&gt; {
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> view3 = ac3.inflate(
            `&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
                android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                android:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
                android:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
                &lt;WebView
                  android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                  android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                  android:id=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/web1"</span> /&gt;
            &lt;/LinearLayout&gt;`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>
      );


      ac3.setContentView(view3);
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> webView = ac3.findWebViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"web1"</span>);


      <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 在 WebView 中加载一个 URL</span>
      webView.loadUrl(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"https://www.baidu.com"</span>);
    });


    ac3.show();


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第四个例子</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac4 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
    ac4.onCreate(() =&gt; {
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> view4 = ac4.inflate(
            `&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
                android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                android:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
                android:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
                &lt;Button
                  android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                  android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                  android:id=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/button1"</span>
                  android:text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"开始"</span> /&gt;
            &lt;/LinearLayout&gt;`, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>
      );


      ac4.setContentView(view4);
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> btn1 = ac4.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
      btn1.setOnClickListener(() =&gt; {
            <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 耗时代码必须在新线程中运行</span>
            <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> t1 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> thread();
            t1.runJsCode(() =&gt; {
                console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"测试ui"</span>);
            }, <span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"线程1"</span>);
      });
    });


    ac4.show();


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第五个例子</span>
    <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(97, 174, 238);">Activity</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
      this.rootView = {
            <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟视图对象</span>
            render: <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
                console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Rendering the root view!"</span>);
            }
      };
    }


    Activity.prototype.getView = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">function</span>(<span class="hljs-params" style="box-sizing: border-box;"></span>) </span>{
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</span> this.rootView;
    };


    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac5 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> Activity();
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> rootView = ac5.getView();
    rootView.render();<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 输出: Rendering the root view!</span>


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第六个例子</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac6 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();


    ac6.onCreate(() =&gt; {
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> view6 = ac6.inflate(
            `&lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
                android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                android:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
                android:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
                &lt;TextView
                  android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                  android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                  android:text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Hello, World!AIWROK"</span> /&gt;
            &lt;/LinearLayout&gt;`,
            <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span> <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 在这里传入父ViewGroup,如果有需要的话</span>
      );


      ac6.setContentView(view6);
    });


    ac6.show();


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第七个例子</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac7 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();


    ac7.onCreate(() =&gt; {
      <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 使用 XML 字符串创建视图</span>
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> view7 = ac7.inflateSxml(`
            &lt;vertical&gt;
                &lt;button id=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span> text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第一个按钮"</span>/&gt;
                &lt;button text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第二个按钮"</span>/&gt;
            &lt;/vertical&gt;
      `, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>);


      <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 设置活动的内容视图</span>
      ac7.setContentView(view7);


      <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 寻找按钮并设置点击事件监听器</span>
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> btn1 = ac7.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
      btn1.setOnClickListener(() =&gt; {
            <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 事件处理代码需要运行在新线程中</span>
            <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> t1 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> thread();
            t1.runJsCode(() =&gt; {
                console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"测试UI"</span>);
            }, <span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"线程1"</span>);
      });
    });


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 显示活动</span>
    ac7.show();


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第八个例子</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac8 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 加载XML布局文件并显示UI</span>
    ac8.loadLayoutFile(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"/代码/layout.xml"</span>);


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 显示activity</span>
    ac8.show();


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第九个例子</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac9 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
    ac9.loadSXML(`   
      &lt;vertical&gt;
            &lt;button text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第一个按钮"</span>/&gt;
            &lt;button text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第二个按钮"</span>/&gt;
      &lt;/vertical&gt;
    `);


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第十个例子</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac10a = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
    ac10a.loadLayoutFile(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"/代码/layout.xml"</span>);


    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac10b = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
    ac10b.loadSXML(`
      &lt;vertical&gt;
            &lt;button text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第一个按钮"</span>/&gt;
            &lt;button text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第二个按钮"</span>/&gt;
      &lt;/vertical&gt;
    `);


    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac10c = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();
    ac10c.loadXML(`
      &lt;LinearLayout xmlns:android=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"http://schemas.android.com/apk/res/android"</span>
            android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
            android:orientation=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"vertical"</span>
            android:padding=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"8dp"</span>&gt;
            &lt;Button
                android:layout_width=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"match_parent"</span>
                android:layout_height=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"wrap_content"</span>
                android:id=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/button1"</span>
                android:text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"开始"</span> /&gt;
      &lt;/LinearLayout&gt;
    `);


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第十一个例子</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac11 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();


    ac11.onCreate(() =&gt; {
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> view11 = ac11.inflateSxml(`
            &lt;vertical&gt;
                &lt;button id=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span> text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"点击我"</span>/&gt;
            &lt;/vertical&gt;
      `, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>);


      ac11.setContentView(view11);


      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> btn1 = ac11.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
      btn1.setOnClickListener(() =&gt; {
            console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"按钮被点击了"</span>);
      });
    });


    ac11.show();


    <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 第十二个例子</span>
    <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> ac12 = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> activity();


    ac12.onCreate(() =&gt; {
      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> view12 = ac12.inflateSxml(`
            &lt;vertical&gt;
                &lt;button id=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span> text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"点击我"</span>/&gt;
            &lt;/vertical&gt;
      `, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>);


      ac12.setContentView(view12);


      <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> btn1 = ac12.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
      btn1.setOnClickListener(() =&gt; {
            <span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 点击按钮时在 UI 线程中运行代码</span>
            ac12.runOnUiThread(() =&gt; {
                console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"UI线程执行代码"</span>);
            });
      });
    });


    ac12.show();
}


main();
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">集合代码意思,这段代码定义了一个简单的JavaScript模拟框架,用于模拟Android应用中的Activity和Thread的行为。主要功能包括:</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Activity类</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">onCreate(callback)</code>: 当活动创建时调用,传入一个回调函数。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">setContentView(view)</code>: 设置活动的根视图。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">show()</code>: 显示活动及其视图。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">close()</code>: 关闭活动。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">inflate(layoutString, parent)</code>: 从布局字符串创建视图。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">inflateSxml(layoutString, parent)</code>: 从简版布局字符串创建视图。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">loadLayoutFile(path)</code>: 从文件路径加载布局。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">loadSXML(layoutString)</code>: 加载简版布局文件。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">loadXML(layoutString)</code>: 加载标准布局文件。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">findViewById(id)</code>: 根据ID查找视图组件,并可以设置点击事件监听器。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">findWebViewById(id)</code>: 根据ID查找WebView组件,并可以加载URL。</li><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">runOnUiThread(callback)</code>: 在UI线程中运行代码。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Thread类</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">runJsCode(callback, threadName)</code>: 运行JavaScript代码在一个新线程中,传入回调函数和线程名。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">示例代码</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">创建多个<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">activity</code>对象,每个对象在<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">onCreate</code>方法中设置视图内容,包括按钮和WebView。</li><li style="list-style: disc; box-sizing: border-box;">通过<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">findViewById</code>和<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">findWebViewById</code>方法查找视图组件,并为按钮设置点击事件。</li><li style="list-style: disc; box-sizing: border-box;">在某些点击事件中,通过<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">runJsCode</code>方法在一个新线程中运行代码。</li><li style="list-style: disc; box-sizing: border-box;">使用<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">loadLayoutFile</code>、<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">loadSXML</code>和<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">loadXML</code>方法加载不同的布局文件。</li></ul></li><li style="box-sizing: border-box;"><p style="margin-top: 16px; margin-bottom: 1.5em; box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">输出</span>:</p><ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style: disc; box-sizing: border-box;">代码中包含大量的<code style="box-sizing: border-box; font-family: Consolas, Monaco, &quot;Lucida Console&quot;, &quot;Liberation Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Bitstream Vera Sans Mono&quot;, &quot;Courier New&quot;, monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">console.log</code>语句,用于输出各个方法的调用和执行过程,模拟了Android应用中Activity和Thread的生命周期和行为。</li></ul></li></ol><p style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Helvetica, &quot;Meiryo UI&quot;, &quot;Malgun Gothic&quot;, &quot;Segoe UI&quot;, &quot;Trebuchet MS&quot;, Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, &quot;Helvetica Neue&quot;, &quot;Droid Sans&quot;, &quot;wenquanyi micro hei&quot;, FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这个框架主要用于学习和测试目的,帮助开发者理解如何在JavaScript中模拟Android的活动和线程机制。</p><p></p>
页: [1]
查看完整版本: Activity生命周期:管理应用界面的法则