Four Ways to read button click events in Android

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong><span style="font-size:18px">title, There are four types of button click events in android, such AS.</span></strong></p></p><p><p><strong><span style="font-size:18px">Interface for four button+ a textview+ a ImageView</span></strong></p></p><p><p><br></p></p><p><p><strong><span style="font-size:18px">The Activity_main layout file is as Follows:</span></strong></p></p><p><p></p></p><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height = "match_parent" android:orientation= "vertical" android:padding= "5dp" tools:context= ". Mainactivity "> <button android:id=" @+id/btn_click1 "android:layout_width=" match_parent "android oid:layout_height= "wrap_content" android:text= "@string/click1"/> <button android:id= "@+id/btn_clic K2 "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:text=" @string/cl Ick2 "/> <button android:id=" @+id/btn_click3 "android:layout_width=" match_parent "android:la yout_height= "wrap_content" android:text= "@string/click3"/> <button android:layout_width= "match_par ENT "android:layout_height=" wrap_content "android: onclick= "click4" android:text= "@string/click4"/> <textview android:id= "@+id/tv_text" Andro Id:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textcolor= "@android: color/holo_r Ed_light "android:textsize=" 18sp "/> <imageview android:id=" @+id/iv_yc "android:layout_width= "wrap_content" android:layout_height= "wrap_content"/></linearlayout></pre><strong><strong><span style="font-size:18px">the main interface implements the following Code:</span></strong></strong><p><p></p></p><p><p></p></p><pre name="code" class="java">Package Clickevent.ithema.com.clickevent;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.imageview;import android.widget.TextView; public class Mainactivity extends Activity implements View.onclicklistener {private TextView tv_text; Private Button btn_click1,btn_click2,btn_click3; Private ImageView iv_yc; @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Tv_text = (TextView) Findviewbyid (r.id.tv_text); iv_yc= (ImageView) Findviewbyid (r.id.iv_yc); Btn_click1 = (Button) Findviewbyid (r.id.btn_click1); Btn_click2 = (Button) Findviewbyid (r.id.btn_click2); Btn_click3 = (Button) Findviewbyid (r.id.btn_click3); Btn_click2.setonclicklistener (this);//this is the way to implement the interface click Btn_click3.setonclicklistener (new mylistener ());// This is the way the custom class implements the interface click Btn_click1.setoncliCklistener (new view.onclicklistener () {//implement Anonymous inner class click Method @Override public void OnClick (View V) { Tv_text.settext ("this is an anonymous internal class click method, applicable to only one click method"); Iv_yc.setimageresource (r.drawable.click1); } }); } @Override public void OnClick (View V) {tv_text.settext ("this is the way to implement Interface clicks"); Iv_yc.setimageresource (r.drawable.click2); } class MyListener implements View.onclicklistener {@Override public void OnClick (View V) {t V_text.settext ("this is the way the custom class implements the interface click"); Iv_yc.setimageresource (r.drawable.click3); }} public void Click4 (View V) {tv_text.settext ("this is the method of defining attributes in an XML file"); Iv_yc.setimageresource (r.drawable.click4); }}</span><span style= "font-size:18px;font-weight:bold;" ></span></pre><br><strong>the <strong><span style="font-size:24px; color:#ff0000">Following is a separate code after splitting:</span></strong></strong><p><p></p></p><p><p><strong><span style="font-size:18px">Method One: Anonymous Inner class method</span></strong></p></p><p><p></p></p><pre name="code" class="java">Package Clickevent.ithema.com.clickevent;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.imageview;import android.widget.TextView;/** * Created by Administrator on 2016/7/16. */public class Click1 extends activity{private TextView tv_text;//text box private ImageView iv_yc;//picture box private Butt On Btn_click1;//button @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstanc eState); Setcontentview (r.layout.activity_main); tv_text= (TextView) Findviewbyid (r.id.tv_text);//find The ID of the text box iv_yc= (ImageView) Findviewbyid (r.id.iv_yc);//find The ID of the picture box btn_click1= (button) Findviewbyid (r.id.btn_click1);//find The ID of the button btn_click1.setonclicklistener (new view.onclick Listener () {//set listening @Override//button Click to unlock the method public void OnClick (View V) {//set The text to be displayed Tv_text.settext ("this is an anonymous internal class click method, applicable to only one click method"); Set the picture to display Iv_yc.setimageresource (r.drawable.click1); } }); }}</pre><strong><strong><span style="font-size:18px">method Two: Implement Interface</span></strong></strong><br><p><p></p></p><p><p></p></p><pre name="code" class="java">Package Clickevent.ithema.com.clickevent;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.imageview;import android.widget.TextView;/** * Created by Administrator on 2016/7/16. */public class Click2 extends Activity implements View.onclicklistener {//implement Listener interface private TextView tv_text;//text box Priv Ate ImageView iv_yc;//picture Box private button Btn_click2;//button @Override protected void onCreate (Bundle Savedinstancest Ate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); tv_text= (TextView) Findviewbyid (r.id.tv_text);//find The ID of the text box iv_yc= (ImageView) Findviewbyid (r.id.iv_yc);//find The ID of the picture box btn_click2= (Button) Findviewbyid (r.id.btn_click2); Find the ID of the button btn_click2.setonclicklistener (this);//set the button to listen} @Override//button Click to unlock the public void OnClick (View V {//set the text to be displayed Tv_text.settext ("this is the way to implement the interface click"); Set the picture to display Iv_yc.sEtimageresource (r.drawable.click2); }}</pre><strong><strong><span style="font-size:18px">Method Three: custom class implementation interface</span></strong></strong><p><p></p></p><p><p></p></p><pre name="code" class="java">Package Clickevent.ithema.com.clickevent;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.imageview;import android.widget.TextView;/** * Created by Administrator on 2016/7/16. */public class Click3 extends Activity {private TextView tv_text;//text box private ImageView iv_yc;//picture box private but ton Btn_click3;//button @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstan cestate); Setcontentview (r.layout.activity_main); Tv_text = (TextView) Findviewbyid (r.id.tv_text);//find The ID of the text box iv_yc= (ImageView) Findviewbyid (r.id.iv_yc);//find The ID of the picture box Btn_click3 = (button) Findviewbyid (r.id.btn_click3);//find The ID of the button btn_click3.setonclicklistener (new mylistener ());//set Listener for Button class MyListener implements View.onclicklistener {//custom MyListener class implements Onclicklistener interface @Overri De public void OnClick (View V) {//set The text to be displayed Tv_text.settext ("this is the way the custom class implements the interface click"); Set the picture to display Iv_yc.setimageresource (r.drawable.click3); } }}</pre><strong><strong><span style="font-size:18px">method Four: Define the OnClick property in the XML file</span></strong></strong><br>1. Define the onclick attribute in the XML file:<p><p></p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:o nclick= "click4" android:text= "@string/click4"/></pre></pre>2. How to write a click in Java code:<p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">Package Clickevent.ithema.com.clickevent;import Android.app.activity;import Android.os.bundle;import Android.view.view;import android.widget.imageview;import android.widget.textview;/** * Created by Administrator on 2016/7/16. */public class Click4 extends Activity { private TextView tv_text;//text box private ImageView iv_yc;//picture Box @ Override protected void onCreate (Bundle Savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Tv_text = (TextView) Findviewbyid (r.id.tv_text);//find The ID of the text box iv_yc= (ImageView) Findviewbyid (r.id.iv_yc);// Find the ID of the picture frame } public void Click4 (View v) { //set The text to be displayed Tv_text.settext ("this is the method of defining attributes in an XML file"); Set the picture to display Iv_yc.setimageresource (r.drawable.click4);} }</pre></pre><br><span style="font-size:18px">the <span style="font-size:18px"><strong>above is my study in the summary of the buttons of the four kinds of click events, if there are shortcomings, please give criticism, thank you! </strong></span></span><br><br><br><p><p></p></p><p><p>Four Ways to read button click events in Android</p></p></span>

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.