ÔÚ°²×¿¿ª·¢ÖУ¬Activity ÊÇÒ»¸ö·Ç³£ÖØÒªµÄ×é¼þ£¬Ö÷ÒªÓÃÓÚÒÔϼ¸¸ö·½Ã棺 Óû§½çÃæչʾ£ºÃ¿¸ö Activity ͨ³£´ú±íÒ»¸öÍêÕûµÄÆÁÄ»»ò½çÃ棬Óû§ÓëÓ¦ÓõĽ»»¥Ö÷Ҫͨ¹ý Activity ʵÏÖ¡£ÀýÈ磬һ¸öÓʼþÓ¦ÓÿÉÄÜ°üº¬Ò»¸öÏÔʾÓʼþÁбíµÄ Activity £¬ÒÔ¼°ÁíÒ»¸öÓÃÓÚÔĶÁÓʼþÄÚÈÝµÄ Activity ¡£ ÉúÃüÖÜÆÚ¹ÜÀí£ºActivity ÓÐÒ»¸öÉúÃüÖÜÆÚ£¬¼´´´½¨¡¢Æô¶¯¡¢»Ö¸´¡¢ÔÝÍ£¡¢Í£Ö¹ºÍÏú»ÙµÈ״̬¡£¿ª·¢Õß¿ÉÒÔͨ¹ýÖØд Activity µÄÉúÃüÖÜÆÚ·½·¨£¬À´¹ÜÀí×ÊÔ´¡¢±£´æ״̬µÈ£¬È·±£Ó¦ÓõÄÁ÷³©ÔËÐкÍÁ¼ºÃµÄÓû§ÌåÑé¡£ ×é¼þ½»»¥£ºActivity ¿ÉÒÔÆô¶¯ÆäËû Activity £¬Ò²¿ÉÒÔÏòÆäËû×é¼þ£¨Èç Service ¡¢BroadcastReceiver £©·¢ËÍÏûÏ¢£¬ÊµÏÖ×é¼þ¼äµÄ½»»¥¡£ ʼþ´¦Àí£ºActivity ¿ÉÒÔÏìÓ¦Óû§µÄÊäÈëʼþ£¬±ÈÈç´¥ÃþÆÁÄ»¡¢µã»÷°´Å¥µÈ£¬²¢×÷³öÏàÓ¦µÄ´¦Àí¡£
ÔÚÄãµÄ´úÂëʾÀýÖУ¬activity ÀàÄ£ÄâÁË°²×¿µÄ Activity £¬Ö÷Òª¸ºÔð´´½¨ºÍÏÔʾÓû§½çÃæ¡¢¹ÜÀíÊÓͼ×é¼þÒÔ¼°´¦Àíµã»÷ʼþµÈ¡£Í¨¹ý onCreate ·½·¨½øÐгõʼ»¯£¬setContentView ·½·¨ÉèÖýçÃæ²¼¾Ö£¬findViewById ·½·¨²éÕÒÌض¨µÄÊÓͼ×é¼þ£¬²¢Í¨¹ýÕâЩ×é¼þµÄ setOnClickListener ·½·¨À´´¦ÀíÓû§µã»÷²Ù×÷¡£´ËÍ⣬´úÂ뻹չʾÁËÈçºÎÔÚÐÂÏß³ÌÖÐÔËÐкÄʱ²Ù×÷£¬ÒÔ¼°ÈçºÎÈ·±£ÔÚ UI Ïß³ÌÖиüÐÂÓû§½çÃæ¡£ µÚÒ»¸öÀý×Ó˵Ã÷£º new activity().close() ![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=a92dae8b5537d57e2a2eb2ad6cba4240)
var ac = new activity();
ac.onCreate(() => {
var view = ac.inflate(
`<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="@+id/button1"
android:text="¹Ø±Õ" />
</LinearLayout>`, null
);
ac.setContentView(view);
var btn1 = ac.findViewById("button1");
btn1.setOnClickListener(() => {
ac.close(); // ¹Ø±Õ»î¶¯µÄ²Ù×÷
});
});
// ÏÔʾ»î¶¯
ac.show();
´´½¨»î¶¯¶ÔÏó£ºvar ac = new activity(); ´´½¨Ò»¸öеĻ¶ÔÏó¡£ ÉèÖû²¼¾Ö£ºÍ¨¹ý ac.inflate ·½·¨ÉèÖà XML ²¼¾Ö£¬XML ¶¨ÒåÁËÒ»¸ö°´Å¥¡£ ÉèÖð´Å¥µÄµã»÷ʼþ£ºÕÒµ½°´Å¥ button1 £¬²¢ÉèÖõã»÷¼àÌýÆ÷¡£ÔÚ¼àÌýÆ÷Öе÷Óà ac.close() ·½·¨À´¹Ø±Õ»î¶¯¡£ ÏÔʾ»î¶¯£ºÍ¨¹ý ac.show() ·½·¨ÏÔʾ»î¶¯¡£
ÇëÈ·±£ÄãµÄ activity ÀඨÒåÁË close ºÍ show ·½·¨£¬·ñÔòµ÷Óûáʧ°Ü¡£ µÚ¶þ¸öÀý×Ó˵Ã÷£º new activity().findViewById(¡®button1¡¯) ![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=9185ba147db9f620601534d076fc6021)
var ac = new activity();
ac.onCreate(() => {
var view = ac.inflate(
`<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="@+id/button1"
android:text="¹Ø±Õ" />
</LinearLayout>`, null
);
ac.setContentView(view);
var btn1 = ac.findViewById("button1");
btn1.setOnClickListener(() => {
ac.close();
});
});
ac.show();
ÒÔÉÏ´úÂëÊÇÔڻÏÔʾʱ´´½¨Ò»¸ö°´Å¥£¬µã»÷°´Å¥ºó¹Ø±Õµ±Ç°»î¶¯¡£ ac.close(); ÊÇÓÃÓڹرջòÏú»Ù»î¶¯µÄ¼ÙÉ躯Êý¡£ findViewById(¡®button1¡¯) ²éÕÒ²¼¾ÖÖж¨ÒåµÄ°´Å¥²¢·µ»ØÆäÒýÓã¬ÒÔ±ãÔÚ´úÂëÖвٿء£ ÕâЩ´úÂëƬ¶ÎºÍ¸ÅÄîÊÇÓÃÓÚ˵Ã÷²¼¾ÖºÍʼþ´¦ÀíÈçºÎ»ùÓڻ¶ÔÏó½øÐвÙ×÷µÄ£¬¾ßÌåʵÏֺ͹¦ÄÜÊÇÐèÒªÔÚ¶ÔÓ¦µÄ¿ò¼ÜÖÐÖ§³ÖµÄ¡£ µÚÈý¸öÀý×Ó˵Ã÷£º new activity().findWebViewById(¡®web1¡¯) ![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=b1e4d2dd9ece6ec2e6a8abff4995f20a)
var ac = new activity();
ac.onCreate(() => {
var view = ac.inflate(
`<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">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/web1" />
</LinearLayout>`, null
);
ac.setContentView(view);
var webView = ac.findWebViewById('web1');
// ÔÚ WebView ÖмÓÔØÒ»¸ö URL
webView.loadUrl("https://www.baidu.com");
});
ac.show();
˵Ã÷ activity ÀàµÄʵÀý»¯£º ´´½¨ÁËÒ»¸ö activity ¶ÔÏó¡£ onCreate ·½·¨£º ÉèÖýçÃæµÄ²¼¾Ö£¬°üº¬Ò»¸ö WebView¡£ findWebViewById ·½·¨£º ÓÃÀ´»ñÈ¡²¼¾ÖÖÐ ID Ϊ web1 µÄ WebView ʵÀý¡£ WebView ²Ù×÷£º ¶ÔÕÒµ½µÄ WebView Ö´ÐвÙ×÷£¬ÀýÈç¼ÓÔØÒ»¸öÍøÒ³¡£ ÕâÖÖ·½·¨Í¨³£ÓÃÓÚ¶¯Ì¬»ñÈ¡²¢²Ù×÷ UI ¿Ø¼þ£¬µ«Çë×¢Ò⣬¾ßÌåϸ½ÚºÍ·½·¨ÐèÒª¸ù¾Ýʵ¼ÊµÄ Android »ò×Ô¶¨Òå API ¶ø¶¨¡£ µÚËĸöÀý×Ó˵Ã÷£º new activity().getActivity() ![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=c50987c95c40ad698e43aaf3c2c8cdf6)
var ac = new activity();
ac.onCreate(() => {
var view = ac.inflate(
`<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="@+id/button1"
android:text="¿ªÊ¼" />
</LinearLayout>`, null
);
ac.setContentView(view);
var btn1 = ac.findViewById("button1");
btn1.setOnClickListener(() => {
// ºÄʱ´úÂë±ØÐëÔÚÐÂÏß³ÌÖÐÔËÐÐ
var t1 = new thread();
t1.runJsCode(() => {
console.log("²âÊÔui");
}, "Ïß³Ì1");
});
});
ac.show();
- activity()£º´ËÀàµÄʵÀý»¯ÓÃÓÚ¹ÜÀí½çÃæÉúÃüÖÜÆÚºÍÊÓͼ¡£
- onCreate£ºÔڻ´´½¨Ê±Ö´ÐеĻص÷º¯Êý£¬ÓÃÓÚ³õʼ»¯ÊÓͼ¡£
- inflate£º¼ÓÔØ XML ²¼¾Ö²¢Éú³ÉÊÓͼ²ã´Î½á¹¹¡£
- setContentView£º½«Ö¸¶¨µÄÊÓͼ×÷Ϊ»î¶¯µÄÄÚÈÝÊÓͼ¡£
- findViewById£º»ñÈ¡²¼¾ÖÖеÄÌض¨ÊÓͼ£¬´ËÀýÓÃÓÚ°´Å¥¡£
- setOnClickListener£ºÉèÖð´Å¥µã»÷ʼþµÄ¼àÌýÆ÷¡£
- Thread£º´´½¨ÐÂÏß³ÌÒÔ±ÜÃâ×èÈûÖ÷Ï̡߳£
- runJsCode£ºÔÚÐÂÏß³ÌÖÐÖ´ÐÐÖ¸¶¨µÄ JavaScript ´úÂë¡£
- show£ºÏÔʾ»î¶¯¡£
Èç¹û activity ¡¢thread ÒÔ¼°ÆäËû¶ÔÏó䶨Ò壬ȷ±£Ïà¹ØµÄ¿â»ò¿ò¼ÜÒѾÕýÈ·ÒýÈë²¢ÅäÖᣠµÚÎå¸öÀý×Ó˵Ã÷£º function Activity() {
this.rootView = {
render: function() {
console.log("Rendering the root view!");
}
};
}
Activity.prototype.getView = function() {
return this.rootView;
};
var ac = new Activity();
var rootView = ac.getView();
rootView.render();
˵Ã÷ Activity Àà: ÎÒÃǶ¨ÒåÁËÒ»¸ö¼òµ¥µÄActivityÀ࣬ËüÄÚ²¿°üº¬Ò»¸örootView¶ÔÏó£¬ÓÃÀ´Ä£Äâ¸ùÊÓͼ¡£ getView ·½·¨: ´Ë·½·¨·µ»ØrootView£¬´ú±íÕû¸ö»î¶¯µÄÊÓͼ¡£ ʹÓÃʾÀý: ÎÒÃÇ´´½¨ÁËActivityµÄÒ»¸öʵÀý£¬µ÷ÓÃgetView·½·¨»ñÈ¡ÊÓͼ£¬È»ºóµ÷ÓÃrender·½·¨À´Ä£ÄâäÖȾ¸ÃÊÓͼ¡£ µÚÁù¸öÀý×Ó˵Ã÷£º new activity().inflate(¡®¡¯,ViewGroup) ![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=211694fafc3b4a1c288902f9f934ba4d)
var ac = new activity();
ac.onCreate(() => {
var view = ac.inflate(
`<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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World! AIWROK" />
</LinearLayout>`,
null // ÔÚÕâÀï´«È븸ViewGroup£¬Èç¹ûÓÐÐèÒªµÄ»°
);
ac.setContentView(view);
});
ac.show();
˵Ã÷£ºActivity ´´½¨ var ac = new activity(); £º´´½¨Ò»¸öеĻʵÀý¡£
onCreate ·½·¨ - ʹÓÃ
ac.onCreate(() => { ... }) ¶¨Òåµ±»î¶¯´´½¨Ê±µÄÐÐΪ¡£
ÊÓͼÌî³äºÍÉèÖà ac.inflate(...) £ºÍ¨¹ýXML×Ö·û´®À´¶¨Òå²¼¾ÖÊÓͼ¡£ac.setContentView(view); £º½«Ìî³äµÄÊÓͼÉèÖÃΪ»î¶¯µÄÄÚÈÝÊÓͼ¡£
°´Å¥²éÕҺͼàÌýÆ÷ var btn1 = ac.findViewById("button1"); £ºÕÒµ½²¼¾ÖÖж¨ÒåµÄ°´Å¥¡£btn1.setOnClickListener(() => { ... }) £ºÎª°´Å¥ÉèÖõã»÷¼àÌýÆ÷¡£
Ïß³Ì var t1 = new thread(); £º´´½¨Ò»¸öеÄÏ̡߳£t1.runJsCode(...); £ºÔÚÏß³ÌÖÐÔËÐÐJavaScript´úÂë¡£
ÏÔʾ»î¶¯ ac.show(); £ºÏÔʾ»î¶¯µÄ½çÃæ¡£
µÚÆ߸öÀý×Ó˵Ã÷£º new activity().inflateSxml(¡®¡¯,ViewGroup) ![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=c6c153aae65a8af0e06f0a242dede9f8)
var ac = new activity();
ac.onCreate(() => {
// ʹÓà XML ×Ö·û´®´´½¨ÊÓͼ
var view = ac.inflateSxml(`
<vertical>
<button id="button1" text="µÚÒ»¸ö°´Å¥"/>
<button text="µÚ¶þ¸ö°´Å¥"/>
</vertical>
`, null);
// ÉèÖûµÄÄÚÈÝÊÓͼ
ac.setContentView(view);
// Ñ°ÕÒ°´Å¥²¢ÉèÖõã»÷ʼþ¼àÌýÆ÷
var btn1 = ac.findViewById("button1");
btn1.setOnClickListener(() => {
// ʼþ´¦Àí´úÂëÐèÒªÔËÐÐÔÚÐÂÏß³ÌÖÐ
var t1 = new thread();
t1.runJsCode(() => {
println("²âÊÔUI");
}, "Ïß³Ì1");
});
});
// ÏÔʾ»î¶¯
ac.show();
Õâ¸öʾÀýչʾÁËÈçºÎʹÓà inflateSxml ·½·¨½« XML ת»»ÎªÊÓͼ¶ÔÏó¡£ÒÔÏÂÊÇ´úÂëµÄÏêϸ˵Ã÷£º ´´½¨»î¶¯ÊµÀý var ac = new activity();
ÔÚ onCreate ·½·¨ÖÐ×°Ôز¼¾Ö ac.onCreate(() => {
var view = ac.inflateSxml(`
<vertical>
<button id="button1" text="µÚÒ»¸ö°´Å¥"/>
<button text="µÚ¶þ¸ö°´Å¥"/>
</vertical>`, null);
ac.setContentView(view);
});
inflateSxml £ºÓÃÓÚ½âÎö XML ×Ö·û´®²¢×ª»»ÎªÊÓͼ¶ÔÏó¡£
setContentView £ºÉèÖõ±Ç°»î¶¯µÄÄÚÈÝÊÓͼ¡£
ÉèÖð´Å¥µã»÷ʼþ var btn1 = ac.findViewById("button1");
btn1.setOnClickListener(() => {
// ºÄʱ´úÂë±ØÐëÔÚÐÂÏß³ÌÖÐÔËÐÐ
var t1 = new thread();
t1.runJsCode(() => {
println("²âÊÔUI")
}, "Ïß³Ì1");
});
findViewById £º»ñÈ¡ÊÓͼÖÐ ID Ϊ button1 µÄ°´Å¥¡£
setOnClickListener £ºÉèÖð´Å¥µÄµã»÷ʼþ¡£ ʹÓÃÐÂÏß³ÌÔËÐкÄʱ²Ù×÷£¬È·±£½çÃæÁ÷³©¡£
ÏÔʾ»î¶¯ ac.show();
show £ºÓÃÓÚÏÔʾָ¶¨µÄ»î¶¯¡£
Õâ¸öʾÀýչʾÁËÈçºÎʹÓà JavaScript Ä£Äâ Android »î¶¯ÖеĻù±¾²Ù×÷£¬°üÀ¨²¼¾Ö¼ÓÔغÍʼþ´¦Àí¡£ µÚ°Ë¸öÀý×Ó˵Ã÷£º new activity().loadLayoutFile(¡®¡¯) ![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=d3186198fe8a2845229d62929422bab3)
var ac = new activity();
ac.loadLayoutFile("/´úÂë/layout.xml");
ac.show();
Çë¸ù¾Ýʵ¼ÊÐèÒªÌæ»»¡±/´úÂë/layout.xml¡±ÎªÄãµÄXMLÎļþµÄʵ¼Ê·¾¶¡£ÉÏÊö´úÂëʾÀýÐèÒªÔÚÖ§³ÖÕâÒ»APIµÄ»·¾³ÏÂÖ´ÐС£ µÚ¾Å¸öÀý×Ó˵Ã÷£º new activity().loadSXML(` <vertical>
<button text="µÚÒ»¸ö°´Å¥"/>
<button text="µÚ¶þ¸ö°´Å¥"/>
</vertical>`)
loadSXML ·½·¨£ºÓÃÓÚ¼ÓÔØÀà XML ²¼¾Ö´úÂë¡£ËüÖ§³Ö½Ï¼òµ¥µÄ UI ²¼¾Ö¡£ ²ÎÊý arg0£º½ÓÊÜÀà XML ²¼¾Ö´úÂëµÄ×Ö·û´®¡£ ʾÀýÖУ¬´´½¨ÁËÒ»¸ö vertical ²¼¾Ö£¬ÆäÖаüº¬Á½¸ö°´Å¥£¬Ã¿¸ö°´Å¥ÏÔʾ²»Í¬µÄÎı¾¡£
![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=5f8bacb3ed535a18ad1dd89f88bff14d)
![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=5342aa5f1a87ed60e437b0399cd1287e) µÚÊ®¸öÀý×Ó˵Ã÷£º
var ac = new activity();
ac.loadLayoutFile("/´úÂë/layout.xml");
var ac = new activity();
ac.loadSXML(`
<vertical>
<button text="µÚÒ»¸ö°´Å¥"/>
<button text="µÚ¶þ¸ö°´Å¥"/>
</vertical>
`);
var ac = new activity();
ac.loadXML(`
<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="@+id/button1"
android:text="¿ªÊ¼" />
</LinearLayout>
`);
Õâ¶Î´úÂëչʾÁËÈçºÎͨ¹ýÈýÖÖ²»Í¬µÄ·½Ê½¼ÓÔغÍÏÔʾXML²¼¾ÖÎļþ¡£ÒÔÏÂÊÇÿÖÖ·½Ê½µÄ˵Ã÷£º ¼ÓÔØXMLÎļþ²¢ÏÔʾUI£º var ac = new activity();
ac.loadLayoutFile("/´úÂë/layout.xml");
Õâ¶Î´úÂë´´½¨ÁËÒ»¸öеĻactivity £¬²¢Í¨¹ýloadLayoutFile ·½·¨¼ÓÔØλÓÚ·¾¶/´úÂë/layout.xml µÄ²¼¾ÖÎļþ£¬ÓÃÓÚÏÔʾUI¡£ ¼ÓÔؼò°æXML£º var ac = new activity();
ac.loadSXML(`
<vertical>
<button text="µÚÒ»¸ö°´Å¥"/>
<button text="µÚ¶þ¸ö°´Å¥"/>
</vertical>
`);
Õâ¶Î´úÂëչʾÁËÈçºÎÔÚ´úÂëÖÐÖ±½Ó¶¨ÒåXML²¼¾Ö×Ö·û´®²¢¼ÓÔØÏÔʾ¡£Ê¹ÓÃloadSXML ·½·¨¿ÉÒÔ¼ÓÔؼò°æµÄXML²¼¾Ö¡£ÔÚÕâ¸öÀý×ÓÖУ¬¶¨ÒåÁËÒ»¸ö´¹Ö±²¼¾Ö£¨vertical £©£¬ÆäÖаüº¬Á½¸ö°´Å¥£¨button £©¡£ ¼ÓÔØXML£º var ac = new activity();
ac.loadXML(`
<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="@+id/button1"
android:text="¿ªÊ¼" />
</LinearLayout>
`);
Õâ¶Î´úÂëʹÓÃloadXML ·½·¨¼ÓÔØÒ»¶Î±ê×¼µÄAndroid XML²¼¾Ö¶¨Òå¡£ÔÚÕâ¸ö²¼¾ÖÖУ¬Ê¹ÓÃÁËLinearLayout ×÷Ϊ¸ù²¼¾Ö£¬ËüÊÇÒ»¸ö´¹Ö±µÄÏßÐÔ²¼¾Ö£¬°üº¬Ò»¸ö°´Å¥¡£°´Å¥µÄ¿í¶ÈÉèΪmatch_parent £¬¸ß¶ÈΪwrap_content £¬ÏÔʾÎÄ×ÖΪ¡°¿ªÊ¼¡±£¬¡°id¡±ÓÃÓÚΨһ±êʶ¡£ ÕâÖÖ¼¼Êõ¿ÉÒÔÔÚ×Ô¶¯»¯½Å±¾ºÍÓ¦ÓóÌÐò¿ª·¢ÖпìËÙ´´½¨UI²¼¾Ö¡£ ![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=1a1e3e0bf4d1d6439b2105edbd0796b8)
µÚʮһ¸öÀý×Ó˵Ã÷£º new activity().onCreate(Function) ![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=2b4a734c34a3a9ed5fcae8b035bc0dcb)
var ac = new activity();
ac.onCreate(() => {
var view = ac.inflateSxml(`
<vertical>
<button id="button1" text="µã»÷ÎÒ"/>
</vertical>
`, null);
ac.setContentView(view);
var btn1 = ac.findViewById("button1");
btn1.setOnClickListener(() => {
println("°´Å¥±»µã»÷ÁË");
});
});
ac.show();
Õâ¶Î´úÂë´´½¨ÁËÒ»¸ö Android »î¶¯£¬²¢ÔڻÖд´½¨ÁËÒ»¸ö¼òµ¥µÄ½çÃæºÍʼþ¼àÌýÆ÷¡£ÒÔÏÂÊÇ´úÂëµÄÏêϸ˵Ã÷£º ´´½¨»î¶¯ÊµÀý£º var ac = new activity();
ÕâÀï´´½¨ÁËÒ»¸öÐ嵀 activity ¶ÔÏóʵÀý£¬´ú±íÒ»¸ö»î¶¯£¨Activity£©¡£ ¶¨Òå»î¶¯´´½¨Ê±µÄÐÐΪ£º ac.onCreate(() => {
...
});
ʹÓà onCreate ·½·¨Ö¸¶¨»î¶¯´´½¨Ê±ÒªÖ´ÐеĴúÂë¡£ ¼ÓÔز¼¾Ö£º var view = ac.inflateSxml(`
<vertical>
<button id="button1" text="µã»÷ÎÒ"/>
</vertical>
`, null);
ʹÓà inflateSxml ·½·¨ÒÔ XML ×Ö·û´®µÄÐÎʽ¶¨ÒåÁËÒ»¸ö¼òµ¥µÄÓû§½çÃæ²¼¾Ö£¬ÆäÖаüº¬Ò»¸ö´¹Ö±²¼¾ÖºÍÒ»¸ö°´Å¥¡£ ÉèÖÃÄÚÈÝÊÓͼ£º ac.setContentView(view);
ÉèÖõ±Ç°»î¶¯µÄÄÚÈÝÊÓͼΪ¸Õ²Å´´½¨µÄ²¼¾Ö¡£ ²éÕÒ°´Å¥²¢ÉèÖõã»÷ʼþ¼àÌýÆ÷£º var btn1 = ac.findViewById("button1");
btn1.setOnClickListener(() => {
println("°´Å¥±»µã»÷ÁË");
});
²éÕÒ²¼¾ÖÖеİ´Å¥£¬²¢ÎªÆäÉèÖÃÒ»¸öµã»÷ʼþ¼àÌýÆ÷£¬°´Å¥µã»÷ʱ»áÔÚ¿ØÖÆ̨Êä³ö ¡°°´Å¥±»µã»÷ÁË¡±¡£ ÏÔʾ»î¶¯£º ac.show();
ÏÔʾ»î¶¯£¬Ê¹Óû§½çÃæ¿É¼û¡£ ´Ë´úÂëչʾÁËÈçºÎÔÚ Android Öд´½¨¼òµ¥µÄÓû§½çÃæ²¢ÏìÓ¦Óû§µÄµã»÷ʼþ¡£ µÚÊ®¶þ¸öÀý×Ó˵Ã÷£º new activity().runOnUiThread(function fun(){
printl('UIÏß³ÌÖ´ÐдúÂë')
})
![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=85563571b4e351fd27cf2741cb1cbe81)
var ac = new activity();
ac.onCreate(() => {
var view = ac.inflateSxml(`
<vertical>
<button id="button1" text="µã»÷ÎÒ"/>
</vertical>
`, null);
ac.setContentView(view);
var btn1 = ac.findViewById("button1");
btn1.setOnClickListener(() => {
// µã»÷°´Å¥Ê±ÔÚ UI Ïß³ÌÖÐÔËÐдúÂë
ac.runOnUiThread(() => {
printl("UIÏß³ÌÖ´ÐдúÂë");
});
});
});
ac.show();
˵Ã÷Activity ´´½¨£º - ´´½¨Ò»¸öеÄ
activity ʵÀý£¬²¢ÔÚÆä onCreate ·½·¨Öж¨Òå³õʼ»¯´úÂë¡£
ÊÓͼ²¼¾Ö£º - ʹÓÃ
inflateSxml ¶¨ÒåÒ»¸ö¼òµ¥µÄ²¼¾Ö£¬°üÀ¨Ò»¸ö°´Å¥¡£ - µ÷ÓÃ
setContentView ·½·¨½«²¼¾ÖÉèÖÃΪµ±Ç°»î¶¯µÄÄÚÈÝÊÓͼ¡£
°´Å¥µã»÷ʼþ£º - ʹÓÃ
findViewById ·½·¨ÕÒµ½²¼¾ÖÖеİ´Å¥¡£ - ¸ø°´Å¥ÉèÖÃÒ»¸öµã»÷ʼþ¼àÌýÆ÷¡£
ÔËÐÐ UI Ï̴߳úÂ룺 - ÔÚ°´Å¥µã»÷ʱ£¬µ÷ÓÃ
runOnUiThread ·½·¨£¬Õ⽫ȷ±£Ö¸¶¨µÄ´úÂëÔÚ UI Ïß³ÌÉÏÖ´ÐС£ println ·½·¨ÓÃÓÚÊä³öÒ»ÌõÈÕÖ¾ÏûÏ¢£¬±íʾ´úÂëÔÚ UI Ïß³ÌÖÐÖ´ÐС£
µÚÊ®Èý¸öÀý×Ó˵Ã÷£º new activity().setContentView(View)
![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=9dc076677e30c8a45c63244006abf048)
function Activity() {}
Activity.prototype.onCreate = function(callback) {
console.log("Activity Created");
callback();
};
Activity.prototype.runOnUiThread = function(callback) {
setTimeout(callback, 0);
};
Activity.prototype.setContentView = function(view) {
console.log("ÊÓͼ¶ÔÏóÒÑÉèÖÃ:", view);
};
var myActivity = new Activity();
myActivity.onCreate(function() {
console.log("»Øµ÷º¯Êý±»µ÷ÓÃ");
myActivity.setContentView("Main View");
myActivity.runOnUiThread(function() {
console.log("UIÏß³ÌÖÐÔËÐеĴúÂë");
});
});
Õâ¸ö´úÂ붨ÒåÁËÒ»¸öÄ£ÄâµÄ Activity À࣬²¢ÎªÆäÌí¼ÓÁË onCreate ºÍ runOnUiThread ·½·¨¡£ÒÔÏÂÊÇ´úÂëµÄÖð²½½âÎö£º ¶¨ÒåActivityÀࣺ function Activity() {}
ÕâÊÇÒ»¸ö¹¹Ô캯Êý£¬ÓÃÓÚ´´½¨ Activity µÄʵÀý¡£ ¶¨ÒåonCreate·½·¨£º Activity.prototype.onCreate = function(callback) {
console.log("Activity Created");
callback();
};
Õâ¸ö·½·¨½ÓÊÜÒ»¸ö»Øµ÷º¯Êý×÷Ϊ²ÎÊý¡£µ±±»µ÷ÓÃʱ£¬Ëü»áÊä³ö ¡°Activity Created¡±£¬È»ºóÖ´Ðд«ÈëµÄ»Øµ÷º¯Êý¡£ ¶¨ÒårunOnUiThread·½·¨£º Activity.prototype.runOnUiThread = function(callback) {
setTimeout(callback, 0);
};
´Ë·½·¨Ò²½ÓÊÜÒ»¸ö»Øµ÷º¯Êý×÷Ϊ²ÎÊý¡£½èÖú setTimeout Ä£ÄâÔÚUIÏß³ÌÖÐÒì²½Ö´Ðлص÷¡£ ´´½¨ÊµÀý²¢µ÷Ó÷½·¨£º var myActivity = new Activity();
myActivity.onCreate(function() {
console.log("»Øµ÷º¯Êý±»µ÷ÓÃ");
myActivity.runOnUiThread(function() {
console.log("UIÏß³ÌÖÐÔËÐеĴúÂë");
});
});
´´½¨ Activity µÄʵÀý myActivity ¡£ µ÷Óà onCreate £¬´«ÈëÒ»¸ö»Øµ÷º¯Êý£¬Ôڻص÷ÖÐÊä³ö ¡°»Øµ÷º¯Êý±»µ÷Óᱡ£ ʹÓà runOnUiThread ÔËÐÐÁíÒ»¸ö»Øµ÷£¬Ä£ÄâÔÚUIÏß³ÌÖÐÔËÐУ¬²¢Êä³ö ¡°UIÏß³ÌÖÐÔËÐеĴúÂ롱¡£ Õâ¸ö´úÂëչʾÁËÈçºÎʹÓûص÷º¯ÊýºÍÒì²½Ö´ÐÐÀ´Ä£ÄâActivityÉúÃüÖÜÆÚÖеÄijЩÐÐΪ¡£ µÚÊ®ËĸöÀý×Ó˵Ã÷£º new activity().show() ![](http://doc.aiwork24.com:91/server/index.php?s=/api/attachment/visitFile&sign=ad8d068f0e9c00e0fc558ab2142470e0)
var ac = new activity();
ac.onCreate(() => {
var view = ac.inflate(
`<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="@+id/button1"
android:text="¿ªÊ¼" />
</LinearLayout>`, null
);
ac.setContentView(view);
var btn1 = ac.findViewById("button1");
btn1.setOnClickListener(() => {
// ºÄʱ´úÂë±ØÐëÔÚÐÂÏß³ÌÖÐÔËÐÐ
var t1 = new thread();
t1.runJsCode(() => {
printl("²âÊÔui");
}, "Ïß³Ì1");
});
});
ac.show();
Õâ¶Î´úÂ붨ÒåÁËÒ»¸öеĻactivity £¬²¢Ôڻ´´½¨Ê±ÉèÖÃÁËÒ»¸ö¼òµ¥µÄÓû§½çÃæ¡£Óû§½çÃæ°üº¬Ò»¸ö°´Å¥£¬µã»÷°´Å¥Ê±£¬»áÔÚÒ»¸öÐÂÏß³ÌÖÐÖ´ÐдúÂ룬Êä³ö¡°²âÊÔui¡±¡£¾ßÌå˵Ã÷ÈçÏ£º »î¶¯¶ÔÏó´´½¨: var ac = new activity(); ´´½¨ÐµĻʵÀý¡£ onCreate ·½·¨: ac.onCreate(() => {...}) ¶¨Òå»î¶¯µÄ´´½¨Âß¼¡£ÔÚÕâÀÄãͨ¹ý inflate ·½·¨ÉèÖÃÁËÒ»¸öÏßÐÔ²¼¾Ö LinearLayout £¬°üº¬Ò»¸ö°´Å¥¡£ °´Å¥µã»÷¼àÌý: - ʹÓÃ
findViewById ÕÒµ½°´Å¥¡£ - ʹÓÃ
setOnClickListener Ϊ°´Å¥ÉèÖõã»÷ʼþ¼àÌýÆ÷¡£ - µã»÷ʱ£¬´´½¨Ò»¸öÐÂÏß³Ì
new thread() £¬ÔÚÆäÖÐÖ´ÐÐ runJsCode ·½·¨£¬Õâ¸ö·½·¨½«»áÊä³öÎı¾¡°²âÊÔui¡±¡£
- ÏÔʾ»î¶¯:
ac.show(); ×îºóÕâÐдúÂëÓÃÓÚÏÔʾÕâ¸ö»î¶¯¡£
×ÜÌåÀ´Ëµ£¬Õâ¶Î´úÂëʵÏÖÁËÒ»¸ö¼òµ¥µÄUI½»»¥£¬µ±Óû§µã»÷°´Å¥Ê±£¬»áÔÚºǫ́Ïß³ÌÖдòÓ¡¡°²âÊÔui¡±¡£Çë×¢Ò⣬ȷ±£activity À༰Ïà¹Ø·½·¨ÔÚÄãµÄ»·¾³ÖÐÒÑÕýÈ·¶¨Ò壬·ñÔò¿ÉÄÜ»á³öÏÖ´íÎó¡£ ÒÔÏÂËùÓÐÀý×Ó´úÂ뼯ºÏÆðÀ´£¬ÈúóÃæAIÖÇÄÜÌåץȡÀý×Ó¡£
function activity() {
this.rootView = null;
}
activity.prototype.onCreate = function(callback) {
console.log("Activity Created");
callback();
};
activity.prototype.setContentView = function(view) {
console.log("ÊÓͼ¶ÔÏóÒÑÉèÖÃ:", view);
this.rootView = view;
};
activity.prototype.show = function() {
console.log("ÏÔʾ»î¶¯");
if (this.rootView) {
console.log("»î¶¯ÊÓͼ:", this.rootView);
} else {
console.log("ûÓÐÉèÖÃÊÓͼ¶ÔÏó");
}
};
activity.prototype.close = function() {
console.log("¹Ø±Õ»î¶¯");
};
activity.prototype.inflate = function(layoutString, parent) {
console.log("¼ÓÔز¼¾Ö:", layoutString);
return layoutString;
};
activity.prototype.inflateSxml = function(layoutString, parent) {
console.log("¼ÓÔؼò°æ²¼¾Ö:", layoutString);
return layoutString;
};
activity.prototype.loadLayoutFile = function(path) {
console.log("¼ÓÔز¼¾ÖÎļþ:", path);
return path;
};
activity.prototype.loadSXML = function(layoutString) {
console.log("¼ÓÔؼò°æ²¼¾ÖÎļþ:", layoutString);
return layoutString;
};
activity.prototype.loadXML = function(layoutString) {
console.log("¼ÓÔرê×¼²¼¾ÖÎļþ:", layoutString);
return layoutString;
};
activity.prototype.findViewById = function(id) {
console.log("²éÕÒÊÓͼ×é¼þ:", id);
return {
setOnClickListener: function(callback) {
console.log("ÉèÖõã»÷ʼþ¼àÌýÆ÷");
callback();
}
};
};
activity.prototype.findWebViewById = function(id) {
console.log("²éÕÒ WebView ×é¼þ:", id);
return {
loadUrl: function(url) {
console.log("¼ÓÔØ URL:", url);
}
};
};
activity.prototype.runOnUiThread = function(callback) {
console.log("ÔÚ UI Ïß³ÌÖÐÔËÐдúÂë");
callback();
};
function thread() {}
thread.prototype.runJsCode = function(callback, threadName) {
console.log("ÔËÐÐÏß³Ì:", threadName);
callback();
};
function main() {
var ac1 = new activity();
ac1.onCreate(() => {
var view1 = ac1.inflate(
`<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="@+id/button1"
android:text="¹Ø±Õ" />
</LinearLayout>`, null
);
ac1.setContentView(view1);
var btn1 = ac1.findViewById("button1");
btn1.setOnClickListener(() => {
ac1.close();
});
});
ac1.show();
var ac2 = new activity();
ac2.onCreate(() => {
var view2 = ac2.inflate(
`<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="@+id/button1"
android:text="¹Ø±Õ" />
</LinearLayout>`, null
);
ac2.setContentView(view2);
var btn1 = ac2.findViewById("button1");
btn1.setOnClickListener(() => {
ac2.close();
});
});
ac2.show();
var ac3 = new activity();
ac3.onCreate(() => {
var view3 = ac3.inflate(
`<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">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/web1" />
</LinearLayout>`, null
);
ac3.setContentView(view3);
var webView = ac3.findWebViewById("web1");
webView.loadUrl("https://www.baidu.com");
});
ac3.show();
var ac4 = new activity();
ac4.onCreate(() => {
var view4 = ac4.inflate(
`<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="@+id/button1"
android:text="¿ªÊ¼" />
</LinearLayout>`, null
);
ac4.setContentView(view4);
var btn1 = ac4.findViewById("button1");
btn1.setOnClickListener(() => {
var t1 = new thread();
t1.runJsCode(() => {
console.log("²âÊÔui");
}, "Ïß³Ì1");
});
});
ac4.show();
function Activity() {
this.rootView = {
render: function() {
console.log("Rendering the root view!");
}
};
}
Activity.prototype.getView = function() {
return this.rootView;
};
var ac5 = new Activity();
var rootView = ac5.getView();
rootView.render();
var ac6 = new activity();
ac6.onCreate(() => {
var view6 = ac6.inflate(
`<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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World! AIWROK" />
</LinearLayout>`,
null
);
ac6.setContentView(view6);
});
ac6.show();
var ac7 = new activity();
ac7.onCreate(() => {
var view7 = ac7.inflateSxml(`
<vertical>
<button id="button1" text="µÚÒ»¸ö°´Å¥"/>
<button text="µÚ¶þ¸ö°´Å¥"/>
</vertical>
`, null);
ac7.setContentView(view7);
var btn1 = ac7.findViewById("button1");
btn1.setOnClickListener(() => {
var t1 = new thread();
t1.runJsCode(() => {
console.log("²âÊÔUI");
}, "Ïß³Ì1");
});
});
ac7.show();
var ac8 = new activity();
ac8.loadLayoutFile("/´úÂë/layout.xml");
ac8.show();
var ac9 = new activity();
ac9.loadSXML(`
<vertical>
<button text="µÚÒ»¸ö°´Å¥"/>
<button text="µÚ¶þ¸ö°´Å¥"/>
</vertical>
`);
var ac10a = new activity();
ac10a.loadLayoutFile("/´úÂë/layout.xml");
var ac10b = new activity();
ac10b.loadSXML(`
<vertical>
<button text="µÚÒ»¸ö°´Å¥"/>
<button text="µÚ¶þ¸ö°´Å¥"/>
</vertical>
`);
var ac10c = new activity();
ac10c.loadXML(`
<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="@+id/button1"
android:text="¿ªÊ¼" />
</LinearLayout>
`);
var ac11 = new activity();
ac11.onCreate(() => {
var view11 = ac11.inflateSxml(`
<vertical>
<button id="button1" text="µã»÷ÎÒ"/>
</vertical>
`, null);
ac11.setContentView(view11);
var btn1 = ac11.findViewById("button1");
btn1.setOnClickListener(() => {
console.log("°´Å¥±»µã»÷ÁË");
});
});
ac11.show();
var ac12 = new activity();
ac12.onCreate(() => {
var view12 = ac12.inflateSxml(`
<vertical>
<button id="button1" text="µã»÷ÎÒ"/>
</vertical>
`, null);
ac12.setContentView(view12);
var btn1 = ac12.findViewById("button1");
btn1.setOnClickListener(() => {
ac12.runOnUiThread(() => {
console.log("UIÏß³ÌÖ´ÐдúÂë");
});
});
});
ac12.show();
}
main();
¼¯ºÏ´úÂëÒâ˼£¬Õâ¶Î´úÂ붨ÒåÁËÒ»¸ö¼òµ¥µÄJavaScriptÄ£Äâ¿ò¼Ü£¬ÓÃÓÚÄ£ÄâAndroidÓ¦ÓÃÖеÄActivityºÍThreadµÄÐÐΪ¡£Ö÷Òª¹¦ÄÜ°üÀ¨£º ActivityÀࣺ onCreate(callback) : µ±»î¶¯´´½¨Ê±µ÷Ó㬴«ÈëÒ»¸ö»Øµ÷º¯Êý¡£setContentView(view) : ÉèÖûµÄ¸ùÊÓͼ¡£show() : ÏÔʾ»î¶¯¼°ÆäÊÓͼ¡£close() : ¹Ø±Õ»î¶¯¡£inflate(layoutString, parent) : ´Ó²¼¾Ö×Ö·û´®´´½¨ÊÓͼ¡£inflateSxml(layoutString, parent) : ´Ó¼ò°æ²¼¾Ö×Ö·û´®´´½¨ÊÓͼ¡£loadLayoutFile(path) : ´ÓÎļþ·¾¶¼ÓÔز¼¾Ö¡£loadSXML(layoutString) : ¼ÓÔؼò°æ²¼¾ÖÎļþ¡£loadXML(layoutString) : ¼ÓÔرê×¼²¼¾ÖÎļþ¡£findViewById(id) : ¸ù¾ÝID²éÕÒÊÓͼ×é¼þ£¬²¢¿ÉÒÔÉèÖõã»÷ʼþ¼àÌýÆ÷¡£findWebViewById(id) : ¸ù¾ÝID²éÕÒWebView×é¼þ£¬²¢¿ÉÒÔ¼ÓÔØURL¡£runOnUiThread(callback) : ÔÚUIÏß³ÌÖÐÔËÐдúÂë¡£
ThreadÀࣺ runJsCode(callback, threadName) : ÔËÐÐJavaScript´úÂëÔÚÒ»¸öÐÂÏß³ÌÖУ¬´«Èë»Øµ÷º¯ÊýºÍÏß³ÌÃû¡£
ʾÀý´úÂ룺 - ´´½¨¶à¸ö
activity ¶ÔÏó£¬Ã¿¸ö¶ÔÏóÔÚonCreate ·½·¨ÖÐÉèÖÃÊÓͼÄÚÈÝ£¬°üÀ¨°´Å¥ºÍWebView¡£ - ͨ¹ý
findViewById ºÍfindWebViewById ·½·¨²éÕÒÊÓͼ×é¼þ£¬²¢Îª°´Å¥ÉèÖõã»÷ʼþ¡£ - ÔÚijЩµã»÷ʼþÖУ¬Í¨¹ý
runJsCode ·½·¨ÔÚÒ»¸öÐÂÏß³ÌÖÐÔËÐдúÂë¡£ - ʹÓÃ
loadLayoutFile ¡¢loadSXML ºÍloadXML ·½·¨¼ÓÔز»Í¬µÄ²¼¾ÖÎļþ¡£
Êä³ö£º - ´úÂëÖаüº¬´óÁ¿µÄ
console.log Óï¾ä£¬ÓÃÓÚÊä³ö¸÷¸ö·½·¨µÄµ÷ÓúÍÖ´Ðйý³Ì£¬Ä£ÄâÁËAndroidÓ¦ÓÃÖÐActivityºÍThreadµÄÉúÃüÖÜÆÚºÍÐÐΪ¡£
Õâ¸ö¿ò¼ÜÖ÷ÒªÓÃÓÚѧϰºÍ²âÊÔÄ¿µÄ£¬°ïÖú¿ª·¢ÕßÀí½âÈçºÎÔÚJavaScriptÖÐÄ£ÄâAndroidµÄ»î¶¯ºÍÏ̻߳úÖÆ¡£ |