Listening-based Event response for Android development event response
Source: Internet
Author: User
<span id="Label3"></p>Listening-based Event response for Android development event response<p><p>This article describes how the Android operating system responds to events by Listening.</p></p><p><p></p></p> <ul> <ul> <li>Listening-based Event response for Android development event response <ul> <li> <ul> <li>Background introduction</li> <li>Android Development Event Response type</li> <li>Inner class</li> <li>Anonymous inner class</li> <li>External class</li> <li>Direct Binding Label</li> <li>Summarize</li> </ul></li> </ul></li> </ul> </ul><p><p></p></p>Background introduction<p><p>For any visual development, the response to the control will be Involved. We use the example: to implement a click on the button buttons to explain the corresponding method of the event in Android.</p></p>Android Development Event Response type<p><p>In Android development, there are two ways to respond to events, namely:<br>-listen-based Event Handling<br>-callback-based Event Handling</p></p><p><p>In this paper, we focus on the analysis based on monitoring-based event processing, There are five main ways to Implement:<br>-inner class<br>-internal Anonymous Class<br>-external class<br>-direct binding Label<br>-activity itself as an event listener tag</p></p><p><p>I personally have no sense of the fifth way, so I don't do a detailed introduction here.</p></p>Inner class<p><p>The inner class is the most common method we need to first use Setonclicklistener to bind the class that handles the response message:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-comment">// 内部类</span> button2.setOnClickListener(<span class="hljs-keyword">new</span> MyOnClickListener());</code></pre></pre><p><p>Then in the activity you designed, create an inner class:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword">public</span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyOnClickListener</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">View</span>.<span class="hljs-title">OnClickListener</span>{</span> <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onClick</span>(View v){ <span class="hljs-string">"内部类"</span>, Toast.LENGTH_SHORT).show(); } }</code></pre></pre><p><p>personally, the inner class is the most suggested method, because you can design an inner class that accepts all the messages that the button responds To.</p></p>Anonymous inner class<p><p>The following is an example of an anonymous inner class:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-comment">// 内部匿名类</span>button1.setOnClickListener(<span class="hljs-keyword">new</span> View.OnClickListener() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onClick</span>(View v) { <span class="hljs-string">"内部匿名类"</span>, Toast.LENGTH_SHORT).show(); }});</code></pre></pre>External class<p><p>The external class, in Layman's terms, is to create A. Java file to write the response to a button click event, here's an example, outerclass is the class I wrote to respond to the button event:</p></p><pre class="prettyprint"><code class=" hljs java"><code class="hljs java"><span class="hljs-keyword">import </span> android.app.Activity; <span class="hljs-keyword">import </span> android.view.View; <span class="hljs-keyword">import </span> android.widget.Toast; <span class="hljs-javadoc">/** * Created by zhi on 2015/4/24. */</span> <span class="hljs-keyword">public </span> <span class="hljs-class"><span class="hljs-keyword">class </span> <span class="hljs-title">outerclass </span> Span class= "hljs-keyword" >implements </span> <span class="hljs-title">view </span>. <span class="hljs-title">onclicklistener </span> { <span class="hljs-keyword">private </span> Activity act; Outerclass (activity Activity) {act = activity; } <span class="hljs-annotation"> @Override </span> <span class="hljs-keyword">public </span> <span class=" Hljs-keyword ">void </span> <span class=" hljs-title ">onclick </span> (View v) {toast.maketext (act,<span class=" hljs-st Ring ">" External class "</span>, toast.length_short). show (); }}</code></code></pre><p><p>Because the toast requires an activity parameter, you need to set this variable when building the class.<br>And when called, pass the arguments past:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-comment">// 外部类</span> button3.setOnClickListener(<span class="hljs-keyword">new</span> OuterClass(<span class="hljs-keyword">this</span>));</code></pre></pre>Direct Binding Label<p><p>The direct binding tag refers to the settings in the XML file, as in the following example:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs applescript"> <Button android:layout_width=<span class="hljs-string">"wrap_content"</span> android:layout_height=<span class="hljs-string">"wrap_content"</span> android:<span class="hljs-type">text</span>=<span class="hljs-string">"Button4"</span> android:<span class="hljs-property">id</span>=<span class="hljs-string">"@+id/button4"</span> android:layout_below=<span class="hljs-string">"@+id/button3"</span> android:layout_alignStart=<span class="hljs-string">"@+id/button3"</span> android:onClick=<span class="hljs-string">"clickButton"</span>/></code></pre></pre><p><p>In the layout file, there is an onclick tag that formulates the function of the button binding.</p></p> <blockquote> <blockquote> <p><strong>tip:</strong> Here "clickbutton" is the function name to process the message, no parentheses are required</p> </blockquote> </blockquote><p><p>In The. Java file, write the Clickbutton method:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-comment">// 直接绑定</span><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">clickButton</span>(View source){ Toast.makeText(getApplicationContext(),<span class="hljs-string">"绑定标签"</span>, Toast.LENGTH_SHORT).show(); }</code></pre></pre> <blockquote> <blockquote> <p><strong>tip:</strong> It is important to note that the name of the function can be arbitrarily named, but the <strong>parameter</strong> must conform to this form (View Source) or the program will crash directly.</p> </blockquote> </blockquote>Summarize<p><p>This article is mainly about how the Android operating system to respond to events, this article is mainly to explain through the way of Listening.</p></p> <p><p>Listening-based Event response for Android development event response</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service