Android Learning: Click event Onclicklistener

Source: Internet
Author: User

One: Look at the program




Two: summary
Click event Onclicklistener three ways:
(1) dynamic setting mode
Button1.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Toast.maketext (Mainactivity.this, "I am a dynamic button",
Toast.length_short). Show ();
}
});


(2) configuration method
android:onclick= "Xmlclickfuc"
Public void Xmlclickfuc (View v) {
Move right 10 at the point of each button click
V.setx (V.getx () + 10);
}


(3) Reusable mode
Button1.setonclicklistener (MyListener);
Button2.setonclicklistener (MyListener);


Setonclicklistener and Onclicklistener Solution coupling
Private View.onclicklistener MyListener = new Onclicklistener () {
@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.button1:
Toast.maketext (Mainactivity.this, "I am Dynamic button1", Toast.length_short). Show ();
Break
Case R.ID.BUTTON3:
Toast.maketext (Mainactivity.this, "I am dynamic Button3", Toast.length_short). Show ();
Break
Default
Toast.maketext (Mainactivity.this, "I am Dynamic button1", Toast.length_short). Show ();
Break

}

}
};


Android Learning: Click event Onclicklistener

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.