Android-view-construction.xhtml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Android view construction</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Building-for-Android.xhtml" title="Building for Android"/>
  10. <link rel="next" href="System-inquiry.xhtml" title="System inquiry"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Android view construction" epub:type="subchapter" id="Android-view-construction">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Android view construction</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>An Android user interface is constructed from <code class="literal">View</code> objects.
  23. The following is an example that illustrates some features of
  24. Kawa to help write views hierarchies,
  25. The example is self-contained, and can be built and run
  26. as described in <a class="link" href="Building-for-Android.xhtml" title="Building for Android">Building for Android</a>.
  27. </p>
  28. <pre class="screen">(require 'android-defs)
  29. (activity hello
  30. (on-create-view
  31. (define counter ::integer 0)
  32. (define counter-view
  33. (TextView text: "Not clicked yet."))
  34. (LinearLayout orientation: LinearLayout:VERTICAL
  35. (TextView text: "Hello, Android from Kawa Scheme!")
  36. (Button
  37. text: "Click here!"
  38. on-click-listener: (lambda (e)
  39. (set! counter (+ counter 1))
  40. (counter-view:setText
  41. (format "Clicked ~d times." counter))))
  42. counter-view)))
  43. </pre>
  44. <p>The first <code class="literal">import</code> form imports various useful definitions
  45. from the Kawa Android library. Using these is not required for
  46. writing a Kawa application, but makes it more convenient.
  47. </p>
  48. <p>The names <code class="literal">LinearLayout</code>, <code class="literal">TextView</code>, and <code class="literal">Button</code>
  49. are just aliases for standard Android <code class="literal">View</code> sub-classes.
  50. A few are prefined by <code class="literal">(require 'android-defs)</code>, or you
  51. can define them yourself using <code class="literal">define-alias</code>.
  52. </p>
  53. <p>An Android application consists of one or more <em class="firstterm">activities</em>,
  54. each of which is an instance of the <code class="literal">android.app.Activity</code> class.
  55. You can use the <code class="literal">activity</code> macro to define your <code class="literal">Activity</code> class.
  56. The first macro argument (in this case <code class="literal">hello</code>) is the class name,
  57. and the others are members of the class, in the syntax of
  58. a <a class="link" href="Defining-new-classes.xhtml#meta-field-or-method-decl"><em class="replaceable"><code>field-or-method-decl</code></em></a>. The sub-form <code class="literal">on-create-view</code>
  59. is an abbreviation for declaring an <code class="literal">onCreate</code> method
  60. (which is called when the <code class="literal">Activity</code> starts up
  61. followed by a <code class="literal">setContentView</code>:
  62. The body of the <code class="literal">on-create-view</code> is evaluated.
  63. The result should be a <code class="literal">View</code> expression,
  64. which is passed to <code class="literal">setContentView</code>.
  65. </p>
  66. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667869606784" class="indexterm"/> <code class="function">current-activity</code> [<em class="replaceable"><code>new-value</code></em>]</p>
  67. <div class="blockquote">
  68. <blockquote class="blockquote">
  69. <p>With no arguments, returns the current <code class="literal">Activity</code>.
  70. If a <em class="replaceable"><code>new-value</code></em> argument is given, sets the current activity.
  71. It is set automatically by the <code class="literal">on-create</code> and <code class="literal">on-create-view</code>
  72. methods of the <code class="literal">activity</code> macro.
  73. </p>
  74. <p>Since <code class="literal">current-activity</code> is a <a class="link" href="Parameter-objects.xhtml" title="Parameter objects">parameter object</a>,
  75. you can
  76. locally change the value using <a class="link" href="Parameter-objects.xhtml#parameterize-syntax"><code class="literal">parameterize</code></a>.
  77. </p>
  78. </blockquote>
  79. </div>
  80. <section class="sect2" title="View object allocation" epub:type="division" id="idm139667869598656">
  81. <div class="titlepage">
  82. <div>
  83. <div>
  84. <h3 class="title">View object allocation</h3>
  85. </div>
  86. </div>
  87. </div>
  88. <p>To create an instance of a <code class="literal">View</code> class you “call” the
  89. class as if it were a function,
  90. as described in <a class="link" href="Allocating-objects.xhtml" title="Allocating objects">Allocating objects</a>.
  91. For example:
  92. </p>
  93. <pre class="screen">(TextView (this) text: "Hello, Android from Kawa Scheme!")
  94. </pre>
  95. <p>If you <code class="literal">(require 'android-defs)</code> that defines
  96. some special handling for <code class="literal">View</code> classes.
  97. You can leave out the <code class="literal">(this)</code> argument,
  98. which refers to the enclosing <code class="literal">Activity</code>:
  99. </p>
  100. <pre class="screen">(TextView text: "Hello, Android from Kawa Scheme!")
  101. </pre>
  102. </section>
  103. <section class="sect2" title="Event handlers" epub:type="division" id="idm139667869592752">
  104. <div class="titlepage">
  105. <div>
  106. <div>
  107. <h3 class="title">Event handlers</h3>
  108. </div>
  109. </div>
  110. </div>
  111. <p>You can register event listeners on Android <code class="literal">View</code> objects
  112. using methods typically named <code class="literal">setOn<em class="replaceable"><code>EVENT</code></em>Listener</code>.
  113. For example <code class="literal">setOnClickListener</code>. When allocating
  114. an object you can leave out the <code class="literal">set</code>, and you can optionally
  115. use Scheme-style names: <code class="literal">on-click-listener</code>. The argument
  116. is an object of a special nested listener class,
  117. for example <code class="literal">View$OnClickListener</code>. These are
  118. single-method classes, so you can use a lambda expression
  119. and <a class="link" href="Anonymous-classes.xhtml#SAM-conversion">SAM-conversion</a> will automatically create the needed
  120. listener class.
  121. </p>
  122. </section>
  123. </section>
  124. <footer>
  125. <div class="navfooter">
  126. <ul>
  127. <li>
  128. <b class="toc">
  129. <a href="Android-view-construction.xhtml#idm139667869598656">View object allocation</a>
  130. </b>
  131. </li>
  132. <li>
  133. <b class="toc">
  134. <a href="Android-view-construction.xhtml#idm139667869592752">Event handlers</a>
  135. </b>
  136. </li>
  137. </ul>
  138. <p>
  139. Up: <a accesskey="u" href="Miscellaneous.xhtml">Miscellaneous topics</a></p>
  140. <p>
  141. Previous: <a accesskey="p" href="Building-for-Android.xhtml">Building for Android</a></p>
  142. <p>
  143. Next: <a accesskey="n" href="System-inquiry.xhtml">System inquiry</a></p>
  144. </div>
  145. </footer>
  146. </body>
  147. </html>