Three methods for triggering onclick events in Activity

Source: Internet
Author: User

Java files

Package COM. wyw. demo; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. edittext; public class activity extends activity {/** called when the activity is first created. */edittext EV1; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); EV1 = (edittext) findviewbyid (R. id. edittext01); // The first method: button btn1 = (button) findviewbyid (R. id. button01); // obtain the button resource btn1.setonclicklistener (New button. onclicklistener () {// create a listener @ override public void onclick (view v) {string strtmp = "Click button01"; ev1.settext (strtmp );}}); // Method 2: button btn2 = (button) findviewbyid (R. id. button02); // get button resource btn2.setonclicklistener (listener); // set listener} button. onclicklistener listener = new button. onclicklistener () {// create a listener object @ override public void onclick (view v) {string strtmp = "Click button02"; ev1.settext (strtmp );}}; // method 3 (available in android1.6 and later versions) Public void btn3onclick (view) {string strtmp = "Click button03"; ev1.settext (strtmp );}}

XML file

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical" android:layout_width="fill_parent"      android:layout_height="fill_parent">      <Button android:text="Button01"               android:id="@+id/Button01"              android:layout_width="wrap_content"               android:layout_height="wrap_content">      </Button>            <Button android:text="Button02"               android:id="@+id/Button02"              android:layout_width="wrap_content"               android:layout_height="wrap_content">      </Button>            <Button android:text="Button03"               android:id="@+id/Button03"              android:layout_width="wrap_content"               android:layout_height="wrap_content"              android:onClick="Btn3OnClick">      </Button>        <EditText android:text="@+id/EditText01"               android:id="@+id/EditText01"               android:layout_width="wrap_content"               android:layout_height="wrap_content">      </EditText>  </LinearLayout>  

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.