123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <!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">
- <head>
- <title>Android view construction</title>
- <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
- <link rel="stylesheet" type="text/css" href="kawa.css"/>
- <script src="kawa-ebook.js" type="text/javascript"/>
- <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
- <link rel="prev" href="Building-for-Android.xhtml" title="Building for Android"/>
- <link rel="next" href="System-inquiry.xhtml" title="System inquiry"/>
- </head>
- <body>
- <header/>
- <section class="sect1" title="Android view construction" epub:type="subchapter" id="Android-view-construction">
- <div class="titlepage">
- <div>
- <div>
- <h2 class="title" style="clear: both">Android view construction</h2>
- </div>
- </div>
- </div>
- <p>An Android user interface is constructed from <code class="literal">View</code> objects.
- The following is an example that illustrates some features of
- Kawa to help write views hierarchies,
- The example is self-contained, and can be built and run
- as described in <a class="link" href="Building-for-Android.xhtml" title="Building for Android">Building for Android</a>.
- </p>
- <pre class="screen">(require 'android-defs)
- (activity hello
- (on-create-view
- (define counter ::integer 0)
- (define counter-view
- (TextView text: "Not clicked yet."))
- (LinearLayout orientation: LinearLayout:VERTICAL
- (TextView text: "Hello, Android from Kawa Scheme!")
- (Button
- text: "Click here!"
- on-click-listener: (lambda (e)
- (set! counter (+ counter 1))
- (counter-view:setText
- (format "Clicked ~d times." counter))))
- counter-view)))
- </pre>
- <p>The first <code class="literal">import</code> form imports various useful definitions
- from the Kawa Android library. Using these is not required for
- writing a Kawa application, but makes it more convenient.
- </p>
- <p>The names <code class="literal">LinearLayout</code>, <code class="literal">TextView</code>, and <code class="literal">Button</code>
- are just aliases for standard Android <code class="literal">View</code> sub-classes.
- A few are prefined by <code class="literal">(require 'android-defs)</code>, or you
- can define them yourself using <code class="literal">define-alias</code>.
- </p>
- <p>An Android application consists of one or more <em class="firstterm">activities</em>,
- each of which is an instance of the <code class="literal">android.app.Activity</code> class.
- You can use the <code class="literal">activity</code> macro to define your <code class="literal">Activity</code> class.
- The first macro argument (in this case <code class="literal">hello</code>) is the class name,
- and the others are members of the class, in the syntax of
- 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>
- is an abbreviation for declaring an <code class="literal">onCreate</code> method
- (which is called when the <code class="literal">Activity</code> starts up
- followed by a <code class="literal">setContentView</code>:
- The body of the <code class="literal">on-create-view</code> is evaluated.
- The result should be a <code class="literal">View</code> expression,
- which is passed to <code class="literal">setContentView</code>.
- </p>
- <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>
- <div class="blockquote">
- <blockquote class="blockquote">
- <p>With no arguments, returns the current <code class="literal">Activity</code>.
- If a <em class="replaceable"><code>new-value</code></em> argument is given, sets the current activity.
- It is set automatically by the <code class="literal">on-create</code> and <code class="literal">on-create-view</code>
- methods of the <code class="literal">activity</code> macro.
- </p>
- <p>Since <code class="literal">current-activity</code> is a <a class="link" href="Parameter-objects.xhtml" title="Parameter objects">parameter object</a>,
- you can
- locally change the value using <a class="link" href="Parameter-objects.xhtml#parameterize-syntax"><code class="literal">parameterize</code></a>.
- </p>
- </blockquote>
- </div>
- <section class="sect2" title="View object allocation" epub:type="division" id="idm139667869598656">
- <div class="titlepage">
- <div>
- <div>
- <h3 class="title">View object allocation</h3>
- </div>
- </div>
- </div>
- <p>To create an instance of a <code class="literal">View</code> class you “call” the
- class as if it were a function,
- as described in <a class="link" href="Allocating-objects.xhtml" title="Allocating objects">Allocating objects</a>.
- For example:
- </p>
- <pre class="screen">(TextView (this) text: "Hello, Android from Kawa Scheme!")
- </pre>
- <p>If you <code class="literal">(require 'android-defs)</code> that defines
- some special handling for <code class="literal">View</code> classes.
- You can leave out the <code class="literal">(this)</code> argument,
- which refers to the enclosing <code class="literal">Activity</code>:
- </p>
- <pre class="screen">(TextView text: "Hello, Android from Kawa Scheme!")
- </pre>
- </section>
- <section class="sect2" title="Event handlers" epub:type="division" id="idm139667869592752">
- <div class="titlepage">
- <div>
- <div>
- <h3 class="title">Event handlers</h3>
- </div>
- </div>
- </div>
- <p>You can register event listeners on Android <code class="literal">View</code> objects
- using methods typically named <code class="literal">setOn<em class="replaceable"><code>EVENT</code></em>Listener</code>.
- For example <code class="literal">setOnClickListener</code>. When allocating
- an object you can leave out the <code class="literal">set</code>, and you can optionally
- use Scheme-style names: <code class="literal">on-click-listener</code>. The argument
- is an object of a special nested listener class,
- for example <code class="literal">View$OnClickListener</code>. These are
- single-method classes, so you can use a lambda expression
- and <a class="link" href="Anonymous-classes.xhtml#SAM-conversion">SAM-conversion</a> will automatically create the needed
- listener class.
- </p>
- </section>
- </section>
- <footer>
- <div class="navfooter">
- <ul>
- <li>
- <b class="toc">
- <a href="Android-view-construction.xhtml#idm139667869598656">View object allocation</a>
- </b>
- </li>
- <li>
- <b class="toc">
- <a href="Android-view-construction.xhtml#idm139667869592752">Event handlers</a>
- </b>
- </li>
- </ul>
- <p>
- Up: <a accesskey="u" href="Miscellaneous.xhtml">Miscellaneous topics</a></p>
- <p>
- Previous: <a accesskey="p" href="Building-for-Android.xhtml">Building for Android</a></p>
- <p>
- Next: <a accesskey="n" href="System-inquiry.xhtml">System inquiry</a></p>
- </div>
- </footer>
- </body>
- </html>
|