安卓窗体[activity]第一集合
<p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=0d3c662ae9069d292c559e810b4d6ac6" 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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>.isOpen = <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">false</span>;
}
Activity.prototype.open = <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);">if</span> (!<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.isOpen) {
<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 opened."</span>);
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.isOpen = <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">true</span>;
} <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">else</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 is already open."</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>{
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">if</span> (<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.isOpen) {
<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 closed."</span>);
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.isOpen = <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">false</span>;
} <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">else</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 is not open."</span>);
}
};
<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> activity = <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">new</span> Activity();
activity.open();
activity.close();
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这个代码定义了一个名为 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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><h3 id="h3--code-activity-code-" 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; font-size: 1.25em !important;"><a name="构造函数 <code>Activity</code>" class="reference-link" style="color: rgb(65, 131, 196); transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;"></a>构造函数 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; font-size: 14px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code></h3><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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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>.isOpen = <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">false</span>;
}
</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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> 是一个构造函数,用于创建活动的实例。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;">每个活动实例都有一个 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">isOpen</code> 属性,表示活动是否打开,初始值为 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">false</code>。</li></ul><h3 id="h3--code-open-code-" 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; font-size: 1.25em !important;"><a name="方法 <code>open</code>" class="reference-link" style="color: rgb(65, 131, 196); transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;"></a>方法 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; font-size: 14px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">open</code></h3><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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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.open = <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);">if</span> (!<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.isOpen) {
<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 opened."</span>);
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.isOpen = <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">true</span>;
} <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">else</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 is already open."</span>);
}
};
</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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">open</code> 方法用于打开活动。<ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;">如果活动没有打开(<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">isOpen</code> 为 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">false</code>),则输出 “Activity opened.”,并将 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">isOpen</code> 设置为 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">true</code>。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;">如果活动已经打开,则输出 “Activity is already open.”。</li></ul></li></ul><h3 id="h3--code-close-code-" 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; font-size: 1.25em !important;"><a name="方法 <code>close</code>" class="reference-link" style="color: rgb(65, 131, 196); transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;"></a>方法 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; font-size: 14px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">close</code></h3><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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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.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>{
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">if</span> (<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.isOpen) {
<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 closed."</span>);
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.isOpen = <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">false</span>;
} <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">else</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 is not open."</span>);
}
};
</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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> 方法用于关闭活动。<ul style="margin-left: 14px; padding-left: 2em; box-sizing: border-box;"><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;">如果活动已经打开(<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">isOpen</code> 为 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">true</code>),则输出 “Activity closed.”,并将 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">isOpen</code> 设置为 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">false</code>。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;">如果活动没有打开,则输出 “Activity is not open.”。</li></ul></li></ul><h3 id="h3--code-activity-code-" 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; font-size: 1.25em !important;"><a name="创建和使用 <code>Activity</code>" class="reference-link" style="color: rgb(65, 131, 196); transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;"></a>创建和使用 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; font-size: 14px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity</code></h3><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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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 go" style="overflow-wrap: normal; box-sizing: border-box; background: 0px 0px transparent; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> activity = <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">new</span> Activity();
activity.open();
activity.<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">close</span>();
</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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;">使用 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">new Activity()</code> 创建了一个活动实例。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;">通过调用 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">open()</code> 方法打开活动。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;">通过调用 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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></ul><h3 id="h3--code-new-activity-close-code-" 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif; font-size: 1.25em !important;"><a name="<code>new Activity().close()</code>" class="reference-link" style="color: rgb(65, 131, 196); transition: color 0.08s ease-out 0s; box-sizing: border-box; background: 0px 0px;"></a><code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; font-size: 14px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">new Activity().close()</code></h3><ul style="margin-bottom: 1.5em; margin-left: 14px; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;">用 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">new Activity()</code> 创建一个活动实例,并立即调用 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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-position: initial; list-style-image: initial; box-sizing: border-box;">因为活动初始时是关闭的,所以会输出 “Activity is not open.”。</li></ul><h3 id="h3-u5E94u7528u573Au666F" 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().findViewById(‘button1’)</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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; border-radius: 3px; border: 0px; word-break: normal; display: block; max-width: none; overflow-x: auto; overflow-y: initial; line-height: inherit;">; font-style: italic;">//doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=214f4a5c1735713a12ff9127aa8f31dd)</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>{
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.layout = <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>;
}
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;">layout</span>) </span>{
<span class="hljs-built_in" style="box-sizing: border-box; color: rgb(230, 192, 123);">this</span>.layout = layout;
<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);">"Layout loaded:"</span>, layout);
};
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>{
<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);">"Finding view by ID:"</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> {
<span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">setOnClickListener</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;">listener</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);">"Setting click listener for"</span>, id);
listener();
}
};
};
<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();
ac.loadSXML(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">'<vertical><button id="button1" text="第一个按钮"/></vertical>'</span>);
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> btn = ac.findViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"button1"</span>);
btn.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;"></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);">"Button clicked!"</span>);
});
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这个代码是一个模拟的 JavaScript 类,用于处理活动(Activity)和视图(View)的简单例子。下面是代码的基本工作原理和说明:</p><ol style="margin-bottom: 1.5em; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">layout</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;">loadSXML方法:</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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">layout</code> 属性中。</li><li style="list-style: disc; box-sizing: border-box;">它接受一个字符串参数 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">layout</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;">findViewById方法:</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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> 方法用于模拟根据 ID 查找视图。</li><li style="list-style: disc; box-sizing: border-box;">它接收一个 ID 字符串并输出正在查找的 ID。</li><li style="list-style: disc; box-sizing: border-box;">返回一个模拟的视图对象,该对象包含 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> 方法。</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;">setOnClickListener方法:</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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> 方法用于为视图设置点击监听器。</li><li style="list-style: disc; box-sizing: border-box;">接受一个回调函数 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">listener</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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac</code>。</li><li style="list-style: disc; box-sizing: border-box;">使用 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> 方法加载布局。</li><li style="list-style: disc; box-sizing: border-box;">使用 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> 方法获取按钮视图。</li><li style="list-style: disc; box-sizing: border-box;">设置按钮的点击监听器,点击时会输出 “Button clicked!”。</li></ul></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=07ac7de55340bd8caec62f8f3bb1b2c8" 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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>{
this.layout = <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>;
}
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;">layout</span>) </span>{
this.layout = layout;
console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Layout loaded:"</span>, layout);
};
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);">"Finding webview by ID:"</span>, id);
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟获取JsWebView对象</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</span> {
url: <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;">page</span>) </span>{
console.log(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"Loading page:"</span>, page);
}
};
};
<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();
ac.loadXML(`
<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);">"0dp"</span>>
<WebView
android:id=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"@+id/web"</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);">"0dp"</span>
android:layout_weight=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"1"</span> />
</LinearLayout>
`);
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> web = ac.findWebViewById(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"web"</span>);
web.url(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"/代码/h5.html"</span>);
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">如何在一个模拟的安卓活动(Activity)中加载布局,并通过控件的ID获取WebView控件。以下是详细说明:</p><h3 id="h3-u4EE3u7801u7ED3u6784" 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">layout</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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity.prototype.loadSXML</code>方法用于加载XML布局,将布局信息存储在<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">layout</code>属性中,并打印出“Layout loaded”信息。</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;">查找WebView</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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">Activity.prototype.findWebViewById</code>方法用于根据ID查找WebView。此方法打印“Finding view by ID”信息,并返回一个模拟的WebView对象。</li><li style="list-style: disc; box-sizing: border-box;">这个模拟的WebView对象具有<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">url</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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac</code>。</li><li style="list-style: disc; box-sizing: border-box;">使用<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac.loadSXML</code>方法加载布局。</li><li style="list-style: disc; box-sizing: border-box;">使用<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">ac.findWebViewById("web")</code>方法获取WebView对象。</li><li style="list-style: disc; box-sizing: border-box;">调用<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">web.url("/代码/h5.html")</code>来模拟加载一个网页。</li></ul></li></ol><h3 id="h3-u53EFu80FDu7684u5E94u7528u573Au666F" 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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><ul style="margin-bottom: 1.5em; margin-left: 14px; padding-left: 2em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;">这种结构可以用于测试或模拟安卓应用程序中的视图查找和事件处理。</li><li style="list-style-position: initial; list-style-image: initial; box-sizing: border-box;">通过这种方式,你可以在JavaScript环境中模拟安卓控件的行为。</li></ul><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().getActivity()</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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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是一个类,定义了getActivity方法</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>{
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// Activity初始化过程</span>
}
Activity.prototype.getActivity = <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);">"获取当前Activity对象"</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>;
};
<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> currentActivity = ac.getActivity();
<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);">if</span> (currentActivity === ac) {
<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);">"Successfully retrieved the current Activity object."</span>);
} <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">else</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);">"Failed to retrieve the current Activity object."</span>);
}
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这个<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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);">getActivity()</code>方法看起来是从某个<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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>对象中获取当前的活动实例,不过你贴出的代码示例似乎没有实现这个方法。通常情况下,在Android开发中,<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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);">getActivity()</code>用于从Fragment中获取宿主Activity。</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">以下是一个简单的模拟实例,以帮助你理解如何在ES5中实现这个功能:</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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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>.name = <span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"MainActivity"</span>;
}
Activity.prototype.getActivity = <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);">"Getting activity instance"</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>;<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;">// 示例</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> currentActivity = ac.getActivity();
<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);">"Current Activity is:"</span>, currentActivity.name);
</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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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>: 创建一个简单的<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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);">name</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;">getActivity方法</span>: 实现的这个方法是返回当前的实例(<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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);">this</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;">创建一个<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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>实例。</li><li style="list-style: disc; box-sizing: border-box;">调用<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; padding: 3px; background: rgb(249, 249, 249); border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(64, 158, 255);">getActivity()</code>方法获取当前活动实例,并输出其名称。</li></ul></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">new activity().inflate(‘’,ViewGroup)<br style="box-sizing: border-box;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=b6a5b9253bd2aafc876c78ee03615354" 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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>{}
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>{
callback();
};
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;">xmlString, viewGroup</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);">"Parsing XML layout..."</span>);
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟解析 XML 并返回一个 View 对象</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</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);">"Setting content view..."</span>);
};
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>{
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟通过 ID 找到视图</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);">`Finding View by ID: <span class="hljs-subst" style="box-sizing: border-box; color: rgb(224, 108, 117);">${id}</span>`</span>);
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">return</span> {
<span class="hljs-attr" style="box-sizing: border-box; color: rgb(209, 154, 102);">setOnClickListener</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;">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);">"Button clicked"</span>);
callback();
}
};
};
<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();
ac.onCreate(<span class="hljs-function" style="box-sizing: border-box;">() =></span> {
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> view = ac.inflate(<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="button1"
android:text="开始" />
</LinearLayout>
`</span>, <span class="hljs-literal" style="box-sizing: border-box; color: rgb(86, 182, 194);">null</span>);
ac.setContentView(view);
<span class="hljs-keyword" style="box-sizing: border-box; color: rgb(198, 120, 221);">var</span> 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> {
<span class="hljs-comment" style="box-sizing: border-box; color: rgb(170, 170, 170); font-style: italic;">// 模拟线程执行</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>);
});
});
</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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><li style="list-style-position: initial; list-style-image: initial; 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;">inflate(String xml, ViewGroup root):</span> 这个方法用于将 XML 布局字符串转换为一个 View 对象。<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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);">xml</code> 是 XML 布局的字符串,<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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);">root</code> 是父 ViewGroup,可以传递 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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);">null</code>。</p></li><li style="list-style-position: initial; list-style-image: initial; 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> 这是在 Activity 创建时调用的方法,在这里面设置初始的 UI 布局。</p></li><li style="list-style-position: initial; list-style-image: initial; 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;">setContentView(View view):</span> 设置当前 Activity 的内容视图为指定的 View 对象。</p></li><li style="list-style-position: initial; list-style-image: initial; 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;">findViewById(String id):</span> 根据 ID 查找视图。</p></li><li style="list-style-position: initial; list-style-image: initial; 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;">setOnClickListener():</span> 为按钮添加点击事件监听器。</p></li><li style="list-style-position: initial; list-style-image: initial; 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></li></ul><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> =></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;">
<vertical>
<span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;"><<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>/></span></span>
<span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;"><<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>/></span></span>
</vertical>
</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> =></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> =></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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这个示例展示了如何使用 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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 转换为视图对象。以下是代码的详细说明:</p><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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;">在 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> 方法中装载布局</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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> =></span> {
var view = ac.inflateSxml(`<span class="javascript" style="box-sizing: border-box;">
<vertical>
<span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;"><<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>/></span></span>
<span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box;"><<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>/></span></span>
</vertical></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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> =></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> =></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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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 为 <code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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> 的按钮。<br style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;"><img src="http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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(() => {</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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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;"><<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">vertical</span>></span>
<span class="hljs-tag" style="box-sizing: border-box;"><<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>/></span>
<span class="hljs-tag" style="box-sizing: border-box;"><<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>/></span>
<span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">vertical</span>></span>`)
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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&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&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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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(`
<vertical>
<button text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第一个按钮"</span>/>
<button text=<span class="hljs-string" style="box-sizing: border-box; color: rgb(152, 195, 121);">"第二个按钮"</span>/>
</vertical>
`);
<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(`
<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>>
<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> />
</LinearLayout>
`);
</code></pre><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这段代码创建了一个新的活动<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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;"><<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">vertical</span>></span>
<span class="hljs-tag" style="box-sizing: border-box;"><<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>/></span>
<span class="hljs-tag" style="box-sizing: border-box;"><<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>/></span>
<span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">vertical</span>></span>
`);
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这段代码展示了如何在代码中直接定义XML布局字符串并加载显示。使用<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", 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: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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;"><<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>></span>
<span class="hljs-tag" style="box-sizing: border-box;"><<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> /></span>
<span class="hljs-tag" style="box-sizing: border-box;"></<span class="hljs-name" style="box-sizing: border-box; color: rgb(224, 108, 117);">LinearLayout</span>></span>
`);
</code></pre></li></ol><p style="margin-bottom: 1.5em; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这段代码使用<code style="box-sizing: border-box; font-family: Consolas, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", 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: "Microsoft YaHei", Helvetica, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Monaco, monospace, Tahoma, STXihei, 华文细黑, STHeiti, "Helvetica Neue", "Droid Sans", "wenquanyi micro hei", FreeSans, Arimo, Arial, SimSun, 宋体, Heiti, 黑体, sans-serif;">这种技术可以在自动化脚本和应用程序开发中快速创建UI布局。</p><div><br></div><p></p>
页:
[1]