<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Library &#187; add</title>
	<atom:link href="http://www.ucosoft.com/tag/add/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ucosoft.com</link>
	<description>Free Source Code and Program Tips</description>
	<lastBuildDate>Mon, 19 Jul 2010 04:51:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Android View Add Listener Tips Sample</title>
		<link>http://www.ucosoft.com/add-listener-tips-android-view-2.html</link>
		<comments>http://www.ucosoft.com/add-listener-tips-android-view-2.html#comments</comments>
		<pubDate>Fri, 18 Dec 2009 03:29:40 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[listener]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/uncategorized/add-listener-tips-android-view-2.html</guid>
		<description><![CDATA[In the development for the control to add Listener is a very common work, the easiest way to add Listener can be: 1 findViewById &#40;R.id.myButton&#41;. setOnClickListener &#40;new View.OnClickListener &#40;&#41; &#40;public void onClick &#40;View v&#41; &#40;/ / Do stuff&#41;&#41;&#41;; Add a Listener using the above method has a drawback is that if control is too large,]]></description>
			<content:encoded><![CDATA[<p>  In the development for the control to add Listener is a very common work, the easiest way to add Listener can be: </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  findViewById <span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">myButton</span><span style="color: #009900;">&#41;</span>. <span style="color: #006633;">setOnClickListener</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">View</span>.<span style="color: #006633;">OnClickListener</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick <span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">/</span> <span style="color: #339933;">/</span> <span style="color: #000000; font-weight: bold;">Do</span> stuff<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>  Add a Listener using the above method has a drawback is that if control is too large, Listener will increase the number, so the following tips can be used to reduce the number of Listener: </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #003399;">View</span>.<span style="color: #006633;">OnClickListener</span> handler <span style="color: #339933;">=</span> <span style="color: #003399;">View</span>.<span style="color: #006633;">OnClickListener</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick <span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>v.<span style="color: #006633;">getId</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">Button01</span><span style="color: #339933;">:</span> <span style="color: #339933;">/</span> <span style="color: #339933;">/</span> doStuff <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">Button02</span><span style="color: #339933;">:</span> <span style="color: #339933;">/</span> <span style="color: #339933;">/</span> doStuff <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> findViewById <span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">myButton</span><span style="color: #009900;">&#41;</span>. <span style="color: #006633;">setOnClickListener</span> <span style="color: #009900;">&#40;</span>handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> findViewById <span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">myOtherButton</span><span style="color: #009900;">&#41;</span>. <span style="color: #006633;">setOnClickListener</span> <span style="color: #009900;">&#40;</span>handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>  The Android1.6 inside, add the Listener&#39;s work has become fairly simple (feeling more like doing web programming!), Concrete steps are as follows: </p>
<p>  1. First, the definition in the layout inside the Button and specify the response Listener </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;?</span> xml version <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span><span style="color: #cc66cc;">1.0</span><span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span> encoding <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>utf<span style="color: #339933;">-</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;?&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>LinearLayout xmlns<span style="color: #339933;">:</span> android <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//schemas.android.com/apk/res/android&amp;quot; android: orientation = &amp;quot;vertical&amp;quot; android: layout_width = &amp;quot;fill_parent&amp;quot; android: layout_height = &amp;quot;fill_parent&amp;quot;&amp;gt; &amp;lt;TextView android:layout_width=&amp;quot;fill_parent&amp;quot; android:layout_height=&amp;quot;wrap_content&amp;quot; android:text=&amp;quot;@string/hello&amp;quot; /&amp;gt; &amp;lt;Button android: text = &amp;quot;Button01&amp;quot; android : id = &amp;quot;@ + id/Button01&amp;quot; android: layout_width = &amp;quot;wrap_content&amp;quot; android: layout_height = &amp;quot;wrap_content&amp;quot; android: onClick = &amp;quot;myClickHandler01&amp;quot; /&amp;gt; &amp;lt;Button android: text = &amp;quot;Button02&amp;quot; android: id = &amp;quot;@ + id / Button02 &amp;quot;android: layout_width =&amp;quot; wrap_content &amp;quot;android: layout_height =&amp;quot; wrap_content &amp;quot;android: onClick =&amp;quot; myClickHandler02 &amp;quot;/&amp;gt; &amp;lt;TextView android: layout_width =&amp;quot; fill_parent &amp;quot;android: layout_height =&amp;quot; wrap_content &amp;quot;android: text =&amp;quot; @ string / hello &amp;quot;/&amp;gt; &amp;lt;/ LinearLayout&amp;gt;</span></pre></td></tr></table></div>

<p>  These two lines of which the following are new features: </p>
<p>  android: onClick = &quot;myClickHandler01&quot; </p>
<p>  android: onClick = &quot;myClickHandler02&quot; </p>
</p>
<p>  2. In the event inside the definition of public methods myClickHandler01, and myClickHandler02 (Note that these two methods must have a View of the shape parameter). </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ray.test</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestOnClickListener <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#40;</span>@ Override <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate <span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span> <span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> setContentView <span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> myClickHandler01 <span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>setTitle <span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>myClickHandler01<span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> myClickHandler02 <span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>setTitle <span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>myClickHandler02<span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>  Of course, you can also use this wording: </p>
<p>  The two buttons set to the same Listener </p>
<p>  android: onClick = &quot;myClickHandler&quot; </p>
<p>  android: onClick = &quot;myClickHandler&quot; </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ray.test</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestOnClickListener <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#40;</span>@ Override <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate <span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span> <span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> setContentView <span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> myClickHandler <span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>target.<span style="color: #006633;">getId</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">Button01</span><span style="color: #339933;">:</span> setTitle <span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>myClickHandler01<span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">Button02</span> <span style="color: #339933;">:</span> setTitle <span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>myClickHandler02<span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>  Refer to the article: &quot;UI framework changes in Android 1.6&quot; (take over the wall) </p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/add" title="add" rel="tag">add</a>, <a href="http://www.ucosoft.com/tag/android" title="android" rel="tag">android</a>, <a href="http://www.ucosoft.com/tag/listener" title="listener" rel="tag">listener</a>, <a href="http://www.ucosoft.com/tag/tips" title="Tips" rel="tag">Tips</a>, <a href="http://www.ucosoft.com/tag/view" title="view" rel="tag">view</a></strong><br />

	<ul class="st-related-posts">
	<li><a href="http://www.ucosoft.com/the-simplest-way-to-change-the-row-height-of-clistctrl.html" title="The Simplest way to change the row height of CListCtrl (January 14, 2008)">The Simplest way to change the row height of CListCtrl</a> (0)</li>
	<li><a href="http://www.ucosoft.com/speed-up-loading-of-pages.html" title="Speed up loading of pages (November 21, 2006)">Speed up loading of pages</a> (1)</li>
	<li><a href="http://www.ucosoft.com/some-useful-shortcut-of-visual-studio-6.html" title="Some useful shortcut of Visual Studio 6 (November 30, 2006)">Some useful shortcut of Visual Studio 6</a> (0)</li>
	<li><a href="http://www.ucosoft.com/visual-sourcesafe-microsofts-source-destruction-system.html" title="Some comment about Visual SourceSafe (November 21, 2006)">Some comment about Visual SourceSafe</a> (0)</li>
	<li><a href="http://www.ucosoft.com/solution-of-fatal-error-rc1004-unexpected-end-of-file-found.html" title="Solution of fatal error RC1004: unexpected end of file found (November 6, 2007)">Solution of fatal error RC1004: unexpected end of file found</a> (11)</li>
	<li><a href="http://www.ucosoft.com/setup-scim-in-ubuntu-610.html" title="Setup SCIM in Ubuntu 6.10 (December 11, 2006)">Setup SCIM in Ubuntu 6.10</a> (0)</li>
	<li><a href="http://www.ucosoft.com/65.html" title="MessageBox support Ctrl+C to copy content (January 1, 2007)">MessageBox support Ctrl+C to copy content</a> (0)</li>
	<li><a href="http://www.ucosoft.com/make-the-code-completion-working-again-in-vc-60.html" title="Make the Code Completion working again in VC++ 6.0 (December 6, 2006)">Make the Code Completion working again in VC++ 6.0</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-launch-an-application-with-admin-privileges-in-vista.html" title="How to launch an Application with Admin privileges in VISTA (March 8, 2007)">How to launch an Application with Admin privileges in VISTA</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-get-the-primary-domain-name.html" title="How to Get the Primary Domain name (January 3, 2007)">How to Get the Primary Domain name</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/add-listener-tips-android-view-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add a Listener Tips Android View Sample</title>
		<link>http://www.ucosoft.com/add-listener-tips-android-view.html</link>
		<comments>http://www.ucosoft.com/add-listener-tips-android-view.html#comments</comments>
		<pubDate>Sat, 17 Oct 2009 22:16:08 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[listener]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/uncategorized/add-listener-tips-android-view.html</guid>
		<description><![CDATA[In the development for the control to add Listener is a very common work, the easiest way to add Listener can be: 1 findViewById &#40;R.id.myButton&#41;. setOnClickListener &#40;new View.OnClickListener &#40;&#41; &#40;public void onClick &#40;View v&#41; &#40;/ / Do stuff&#41;&#41;&#41;; Add a Listener using the above method has a drawback is that if control is too large,]]></description>
			<content:encoded><![CDATA[<p>  In the development for the control to add Listener is a very common work, the easiest way to add Listener can be: </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  findViewById <span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">myButton</span><span style="color: #009900;">&#41;</span>. <span style="color: #006633;">setOnClickListener</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">View</span>.<span style="color: #006633;">OnClickListener</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick <span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">/</span> <span style="color: #339933;">/</span> <span style="color: #000000; font-weight: bold;">Do</span> stuff<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>  Add a Listener using the above method has a drawback is that if control is too large, Listener will increase the number, so the following tips can be used to reduce the number of Listener: </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #003399;">View</span>.<span style="color: #006633;">OnClickListener</span> handler <span style="color: #339933;">=</span> <span style="color: #003399;">View</span>.<span style="color: #006633;">OnClickListener</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick <span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>v.<span style="color: #006633;">getId</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">Button01</span><span style="color: #339933;">:</span> <span style="color: #339933;">/</span> <span style="color: #339933;">/</span> doStuff <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">Button02</span><span style="color: #339933;">:</span> <span style="color: #339933;">/</span> <span style="color: #339933;">/</span> doStuff <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> findViewById <span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">myButton</span><span style="color: #009900;">&#41;</span>. <span style="color: #006633;">setOnClickListener</span> <span style="color: #009900;">&#40;</span>handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> findViewById <span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">myOtherButton</span><span style="color: #009900;">&#41;</span>. <span style="color: #006633;">setOnClickListener</span> <span style="color: #009900;">&#40;</span>handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>  The Android1.6 inside, add the Listener&#39;s work has become fairly simple (feeling more like doing web programming!), Concrete steps are as follows: </p>
<p>  1. First, the definition in the layout inside the Button and specify the response Listener </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;?</span> xml version <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span><span style="color: #cc66cc;">1.0</span><span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span> encoding <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>utf<span style="color: #339933;">-</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;?&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>LinearLayout xmlns<span style="color: #339933;">:</span> android <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//schemas.android.com/apk/res/android&amp;quot; android: orientation = &amp;quot;vertical&amp;quot; android: layout_width = &amp;quot;fill_parent&amp;quot; android: layout_height = &amp;quot;fill_parent&amp;quot;&amp;gt; &amp;lt;TextView android:layout_width=&amp;quot;fill_parent&amp;quot; android:layout_height=&amp;quot;wrap_content&amp;quot; android:text=&amp;quot;@string/hello&amp;quot; /&amp;gt; &amp;lt;Button android: text = &amp;quot;Button01&amp;quot; android : id = &amp;quot;@ + id/Button01&amp;quot; android: layout_width = &amp;quot;wrap_content&amp;quot; android: layout_height = &amp;quot;wrap_content&amp;quot; android: onClick = &amp;quot;myClickHandler01&amp;quot; /&amp;gt; &amp;lt;Button android: text = &amp;quot;Button02&amp;quot; android: id = &amp;quot;@ + id / Button02 &amp;quot;android: layout_width =&amp;quot; wrap_content &amp;quot;android: layout_height =&amp;quot; wrap_content &amp;quot;android: onClick =&amp;quot; myClickHandler02 &amp;quot;/&amp;gt; &amp;lt;TextView android: layout_width =&amp;quot; fill_parent &amp;quot;android: layout_height =&amp;quot; wrap_content &amp;quot;android: text =&amp;quot; @ string / hello &amp;quot;/&amp;gt; &amp;lt;/ LinearLayout&amp;gt;</span></pre></td></tr></table></div>

<p>  These two lines of which the following are new features: </p>
<p>  android: onClick = &quot;myClickHandler01&quot; </p>
<p>  android: onClick = &quot;myClickHandler02&quot; </p>
</p>
<p>  2. In the event inside the definition of public methods myClickHandler01, and myClickHandler02 (Note that these two methods must have a View of the shape parameter). </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ray.test</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestOnClickListener <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#40;</span>@ Override <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate <span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span> <span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> setContentView <span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> myClickHandler01 <span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>setTitle <span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>myClickHandler01<span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> myClickHandler02 <span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>setTitle <span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>myClickHandler02<span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>  Of course, you can also use this wording: </p>
<p>  The two buttons set to the same Listener </p>
<p>  android: onClick = &quot;myClickHandler&quot; </p>
<p>  android: onClick = &quot;myClickHandler&quot; </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ray.test</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestOnClickListener <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#40;</span>@ Override <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate <span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span> <span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> setContentView <span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> myClickHandler <span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>target.<span style="color: #006633;">getId</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">Button01</span><span style="color: #339933;">:</span> setTitle <span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>myClickHandler01<span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">Button02</span> <span style="color: #339933;">:</span> setTitle <span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span>myClickHandler02<span style="color: #339933;">&amp;</span>quot<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>  Refer to the article: &quot;UI framework changes in Android 1.6&quot; (take over the wall) </p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/add" title="add" rel="tag">add</a>, <a href="http://www.ucosoft.com/tag/android" title="android" rel="tag">android</a>, <a href="http://www.ucosoft.com/tag/listener" title="listener" rel="tag">listener</a>, <a href="http://www.ucosoft.com/tag/tips" title="Tips" rel="tag">Tips</a>, <a href="http://www.ucosoft.com/tag/view" title="view" rel="tag">view</a></strong><br />

	<ul class="st-related-posts">
	<li><a href="http://www.ucosoft.com/the-simplest-way-to-change-the-row-height-of-clistctrl.html" title="The Simplest way to change the row height of CListCtrl (January 14, 2008)">The Simplest way to change the row height of CListCtrl</a> (0)</li>
	<li><a href="http://www.ucosoft.com/speed-up-loading-of-pages.html" title="Speed up loading of pages (November 21, 2006)">Speed up loading of pages</a> (1)</li>
	<li><a href="http://www.ucosoft.com/some-useful-shortcut-of-visual-studio-6.html" title="Some useful shortcut of Visual Studio 6 (November 30, 2006)">Some useful shortcut of Visual Studio 6</a> (0)</li>
	<li><a href="http://www.ucosoft.com/visual-sourcesafe-microsofts-source-destruction-system.html" title="Some comment about Visual SourceSafe (November 21, 2006)">Some comment about Visual SourceSafe</a> (0)</li>
	<li><a href="http://www.ucosoft.com/solution-of-fatal-error-rc1004-unexpected-end-of-file-found.html" title="Solution of fatal error RC1004: unexpected end of file found (November 6, 2007)">Solution of fatal error RC1004: unexpected end of file found</a> (11)</li>
	<li><a href="http://www.ucosoft.com/setup-scim-in-ubuntu-610.html" title="Setup SCIM in Ubuntu 6.10 (December 11, 2006)">Setup SCIM in Ubuntu 6.10</a> (0)</li>
	<li><a href="http://www.ucosoft.com/65.html" title="MessageBox support Ctrl+C to copy content (January 1, 2007)">MessageBox support Ctrl+C to copy content</a> (0)</li>
	<li><a href="http://www.ucosoft.com/make-the-code-completion-working-again-in-vc-60.html" title="Make the Code Completion working again in VC++ 6.0 (December 6, 2006)">Make the Code Completion working again in VC++ 6.0</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-launch-an-application-with-admin-privileges-in-vista.html" title="How to launch an Application with Admin privileges in VISTA (March 8, 2007)">How to launch an Application with Admin privileges in VISTA</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-get-the-primary-domain-name.html" title="How to Get the Primary Domain name (January 3, 2007)">How to Get the Primary Domain name</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/add-listener-tips-android-view.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
