Android - Event Handling

<blockquote > <p ><b >Events are a useful way to collect data about a user&#39;s interaction with interactive components of Applications.</b>Like button presses or screen touch etc. The Android framework maintains an event queue as first-in, first-out (FIFO) basis. You can capture these events in your program and take appropriate action as per requirements.</p> </blockquote> <p >There are following three concepts related to Android Event Management &minus;</p> <ul class="list" > <li > <p ><b >Event Listeners</b>&minus; An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI.</p> </li> <li > <p ><b >Event Listeners Registration</b>&minus; Event Registration is the process by which an Event Handler gets registered with an Event Listener so that the handler is called when the Event Listener fires the event.</p> </li> <li > <p ><b >Event Handlers</b>&minus; When an event happens and we have registered an event listener for the event, the event listener calls the Event Handlers, which is the method that actually handles the event.</p> </li> </ul> <h2 >Event Listeners &amp; Event Handlers</h2> <table class="table table-bordered" > <tbody > <tr > <th >Event Handler</th> <th >Event Listener &amp; Description</th> </tr> <tr > <td >onClick()</td> <td ><b >OnClickListener()</b> <p >This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. You will use onClick() event handler to handle such event.</p> </td> </tr> <tr > <td >onLongClick()</td> <td ><b >OnLongClickListener()</b> <p >This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. for one or more seconds. You will use onLongClick() event handler to handle such event.</p> </td> </tr> <tr > <td >onFocusChange()</td> <td ><b >OnFocusChangeListener()</b> <p >This is called when the widget looses its focus ie. user goes away from the view item. You will use onFocusChange() event handler to handle such event.</p> </td> </tr> <tr > <td >onKey()</td> <td ><b >OnFocusChangeListener()</b> <p >This is called when the user is focused on the item and presses or releases a hardware key on the device. You will use onKey() event handler to handle such event.</p> </td> </tr> <tr > <td >onTouch()</td> <td ><b >OnTouchListener()</b> <p >This is called when the user presses the key, releases the key, or any movement gesture on the screen. You will use onTouch() event handler to handle such event.</p> </td> </tr> <tr > <td >onMenuItemClick()</td> <td ><b >OnMenuItemClickListener()</b> <p >This is called when the user selects a menu item. You will use onMenuItemClick() event handler to handle such event.</p> </td> </tr> <tr > <td >onCreateContextMenu()</td> <td ><b >onCreateContextMenuItemListener()</b> <p >This is called when the context menu is being built(as the result of a sustained &quot;long click)</p> </td> </tr> </tbody> </table> <p >There are many more event listeners available as a part of<b >View</b>class like OnHoverListener, OnDragListener etc which may be needed for your application. So I recommend to refer official documentation for Android application development in case you are going to develop a sophisticated apps.</p> <h2 >Event Listeners Registration</h2> <p >Event Registration is the process by which an Event Handler gets registered with an Event Listener so that the handler is called when the Event Listener fires the event. Though there are several tricky ways to register your event listener for any event, but I&#39;m going to list down only top 3 ways, out of which you can use any of them based on the situation.</p> <ul class="list" > <li > <p >Using an Anonymous Inner Class</p> </li> <li > <p >Activity class implements the Listener interface.</p> </li> <li > <p >Using Layout file activity_main.xml to specify event handler directly.</p> </li> </ul> <p >Below section will provide you detailed examples on all the three scenarios &minus;</p> <h2 >Touch Mode</h2> <p >Users can interact with their devices by using hardware keys or buttons or touching the screen.Touching the screen puts the device into touch mode. The user can then interact with it by touching the on-screen virtual buttons, images, etc.You can check if the device is in touch mode by calling the View class&rsquo;s isInTouchMode() method.</p> <p><img alt="Event" src="http://www.tutorialspoint.com/android/images/event.png" /></p> <h4 align="center" >TOUCH EVENT</h4> <h2 >Focus</h2> <p >A view or widget is usually highlighted or displays a flashing cursor when it&rsquo;s in focus. This indicates that it&rsquo;s ready to accept input from the user.</p> <ul class="list" > <li > <p ><b >isFocusable()</b>&ndash; it returns true or false</p> </li> <li > <p ><b >isFocusableInTouchMode()</b>&ndash; checks to see if the view is focusable in touch mode. (A view may be focusable when using a hardware key but not when the device is in touch mode)</p> </li> </ul> <pre class="prettyprint notranslate prettyprinted" > <span class="pln" >android</span><span class="pun" >:</span><span class="pln" >foucsUp</span><span class="pun" >=</span><span class="str" >&quot;@=id/button_l&quot;</span></pre> <h2 >onTouchEvent()</h2> <pre class="prettyprint prettyprinted" > <span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onTouchEvent</span><span class="pun" >(</span><span class="pln" >motionEvent </span><span class="kwd" >event</span><span class="pun" >){</span><span class="pln" > </span><span class="kwd" >switch</span><span class="pun" >(</span><span class="kwd" >event</span><span class="pun" >.</span><span class="pln" >getAction</span><span class="pun" >()){</span><span class="pln" > </span><span class="kwd" >case</span><span class="pln" > TOUCH_DOWN</span><span class="pun" >:</span><span class="pln" > </span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="str" >&quot;you have clicked down Touch button&quot;</span><span class="pun" >,</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENTH_LONG</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" > </span><span class="kwd" >break</span><span class="pun" >();</span><span class="pln" > </span><span class="kwd" >case</span><span class="pln" > TOUCH_UP</span><span class="pun" >:</span><span class="pln" > </span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="str" >&quot;you have clicked up touch button&quot;</span><span class="pun" >,</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENTH_LONG</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" > </span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >case</span><span class="pln" > TOUCH_MOVE</span><span class="pun" >:</span><span class="pln" > </span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="str" >&quot;you have clicked move touch button&quot;</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENTH_LONG</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" > </span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onTouchEvent</span><span class="pun" >(</span><span class="kwd" >event</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span></pre> <h2 >Event Handling Examples</h2> <h3 >Event Listeners Registration Using an Anonymous Inner Class</h3> <p >Here you will create an anonymous implementation of the listener and will be useful if each class is applied to a single control only and you have advantage to pass arguments to event handler. In this approach event handler methods can access private data of Activity. No reference is needed to call to Activity.</p> <p >But if you applied the handler to more than one control, you would have to cut and paste the code for the handler and if the code for the handler is long, it makes the code harder to maintain.</p> <p >Following are the simple steps to show how we will make use of separate Listener class to register and capture click event. Similar way you can implement your listener for any other required event type.</p> <table class="table table-bordered" > <tbody > <tr > <th class="fivepct" >Step</th> <th >Description</th> </tr> <tr > <td >1</td> <td >You will use Android studio IDE to create an Android application and name it as<i >myapplication</i>under a package<i >com.example.myapplication</i>as explained in the<i >Hello World Example</i>chapter.</td> </tr> <tr > <td >2</td> <td >Modify<i >src/MainActivity.java</i>file to add click event listeners and handlers for the two buttons defined.</td> </tr> <tr > <td >3</td> <td >Modify the detault content of<i >res/layout/activity_main.xml</i>file to include Android UI controls.</td> </tr> <tr > <td >4</td> <td >No need to declare default string constants.Android studio takes care default constants.</td> </tr> <tr > <td >5</td> <td >Run the application to launch Android emulator and verify the result of the changes done in the aplication.</td> </tr> </tbody> </table> <p >Following is the content of the modified main activity file<b >src/com.example.myapplication/MainActivity.java</b>. This file can include each of the fundamental lifecycle methods.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="pln" >myapplication</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MainActivity</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >ActionBarActivity</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >private</span><span class="pln" > </span><span class="typ" >ProgressDialog</span><span class="pln" > progress</span><span class="pun" >;</span><span class="pln" > </span><span class="typ" >Button</span><span class="pln" > b1</span><span class="pun" >,</span><span class="pln" >b2</span><span class="pun" >;</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCreate</span><span class="pun" >(</span><span class="typ" >Bundle</span><span class="pln" > savedInstanceState</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onCreate</span><span class="pun" >(</span><span class="pln" >savedInstanceState</span><span class="pun" >);</span><span class="pln" > setContentView</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >layout</span><span class="pun" >.</span><span class="pln" >activity_main</span><span class="pun" >);</span><span class="pln" > progress </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >ProgressDialog</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >);</span><span class="pln" > b1</span><span class="pun" >=(</span><span class="typ" >Button</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >button</span><span class="pun" >);</span><span class="pln" > b2</span><span class="pun" >=(</span><span class="typ" >Button</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >button2</span><span class="pun" >);</span><span class="pln" > b1</span><span class="pun" >.</span><span class="pln" >setOnClickListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnClickListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClick</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > v</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >TextView</span><span class="pln" > txtView </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >TextView</span><span class="pun" >)</span><span class="pln" > findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >textView</span><span class="pun" >);</span><span class="pln" > txtView</span><span class="pun" >.</span><span class="pln" >setTextSize</span><span class="pun" >(</span><span class="lit" >25</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >});</span><span class="pln" > b2</span><span class="pun" >.</span><span class="pln" >setOnClickListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnClickListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClick</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > v</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >TextView</span><span class="pln" > txtView </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >TextView</span><span class="pun" >)</span><span class="pln" > findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >textView</span><span class="pun" >);</span><span class="pln" > txtView</span><span class="pun" >.</span><span class="pln" >setTextSize</span><span class="pun" >(</span><span class="lit" >55</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >});</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onCreateOptionsMenu</span><span class="pun" >(</span><span class="typ" >Menu</span><span class="pln" > menu</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// Inflate the menu; this adds items to the action bar if it is present.</span><span class="pln" > getMenuInflater</span><span class="pun" >().</span><span class="pln" >inflate</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >menu</span><span class="pun" >.</span><span class="pln" >menu_main</span><span class="pun" >,</span><span class="pln" > menu</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onOptionsItemSelected</span><span class="pun" >(</span><span class="typ" >MenuItem</span><span class="pln" > item</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// Handle action bar item clicks here. The action bar will</span><span class="pln" > </span><span class="com" >// automatically handle clicks on the Home/Up button, so long</span><span class="pln" > </span><span class="com" >// as you specify a parent activity in AndroidManifest.xml.</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > id </span><span class="pun" >=</span><span class="pln" > item</span><span class="pun" >.</span><span class="pln" >getItemId</span><span class="pun" >();</span><span class="pln" > </span><span class="com" >//noinspection SimplifiableIfStatement</span><span class="pln" > </span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >id </span><span class="pun" >==</span><span class="pln" > R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >action_settings</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onOptionsItemSelected</span><span class="pun" >(</span><span class="pln" >item</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span></pre> <p >Following will be the content of<b >res/layout/activity_main.xml</b>file &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="tag" >&lt;RelativeLayout</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >xmlns:tools</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/tools&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:paddingBottom</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingLeft</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingRight</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingTop</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >tools:context</span><span class="pun" >=</span><span class="atv" >&quot;.MainActivity&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView1&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Event Handling &quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignParentTop</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;30dp&quot;</span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Tutorials point &quot;</span><span class="pln" > </span><span class="atn" >android:textColor</span><span class="pun" >=</span><span class="atv" >&quot;#ff87ff09&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;30dp&quot;</span><span class="pln" > </span><span class="atn" >android:layout_above</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageButton&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_marginBottom</span><span class="pun" >=</span><span class="atv" >&quot;40dp&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;ImageButton</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageButton&quot;</span><span class="pln" > </span><span class="atn" >android:src</span><span class="pun" >=</span><span class="atv" >&quot;@drawable/abc&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerVertical</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;Button</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Small font&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageButton&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;Button</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Large Font&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Hello World!&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;25dp&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/RelativeLayout&gt;</span></pre> <p >Following will be the content of<b >res/values/strings.xml</b>to define two new constants &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="pun" >&lt;?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >&quot;1.0&quot;</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >&quot;utf-8&quot;</span><span class="pun" >?&gt;</span><span class="pln" > </span><span class="tag" >&lt;resources&gt;</span><span class="pln" > </span><span class="tag" >&lt;string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >&quot;app_name&quot;</span><span class="tag" >&gt;</span><span class="pln" >myapplication</span><span class="tag" >&lt;/string&gt;</span><span class="pln" > </span><span class="tag" >&lt;string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >&quot;action_settings&quot;</span><span class="tag" >&gt;</span><span class="pln" >Settings</span><span class="tag" >&lt;/string&gt;</span><span class="pln" > </span><span class="tag" >&lt;/resources&gt;</span></pre> <p >Following is the default content of<b >AndroidManifest.xml</b>&minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="pun" >&lt;?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >&quot;1.0&quot;</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >&quot;utf-8&quot;</span><span class="pun" >?&gt;</span><span class="pln" > </span><span class="tag" >&lt;manifest</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >package</span><span class="pun" >=</span><span class="atv" >&quot;com.example.myapplication&quot;</span><span class="pln" > </span><span class="atn" >android:versionCode</span><span class="pun" >=</span><span class="atv" >&quot;1&quot;</span><span class="pln" > </span><span class="atn" >android:versionName</span><span class="pun" >=</span><span class="atv" >&quot;1.0&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;uses-sdk</span><span class="pln" > </span><span class="atn" >android:minSdkVersion</span><span class="pun" >=</span><span class="atv" >&quot;8&quot;</span><span class="pln" > </span><span class="atn" >android:targetSdkVersion</span><span class="pun" >=</span><span class="atv" >&quot;22&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;application</span><span class="pln" > </span><span class="atn" >android:allowBackup</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >&quot;@drawable/ic_launcher&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >&quot;@style/AppTheme&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;activity</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;com.example.myapplication.MainActivity&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.action.MAIN&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;category</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.category.LAUNCHER&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;/activity&gt;</span><span class="pln" > </span><span class="tag" >&lt;/application&gt;</span><span class="pln" > </span><span class="tag" >&lt;/manifest&gt;</span></pre> <p >Let&#39;s try to run your<b >myapplication</b>application. I assume you had created your<b >AVD</b>while doing environment setup. To run the app from Android Studio, open one of your project&#39;s activity files and click Run<img alt="Eclipse Run Icon" class="inline" src="http://www.tutorialspoint.com/android/images/eclipse_run.png" />icon from the toolbar. Android Studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window &minus;</p> <p><img alt="Android Event Handling" src="http://www.tutorialspoint.com/android/images/event1.png" /></p> <p >Now you try to click on two buttons, one by one and you will see that font of the<b >Hello World</b>text will change, which happens because registered click event handler method is being called against each click event.</p> <h2 >Exercise</h2> <p >I will recommend to try writing different event handlers for different event types and understand exact difference in different event types and their handling. Events related to menu, spinner, pickers widgets are little different but they are also based on the same concepts as explained above.</p>
RangeTime:0.010309s
RangeMem:239.54 KB
返回顶部 留言