4 ways to implement the OnClick event for Android controls

Source: Internet
Author: User

1.
<span style= "White-space:pre" ></span>textview TV = (TextView) Findviewbyid (r.id.tv); Tv.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method stub Do something ...});
2.
<span style= "White-space:pre" ></span>textview TV = (TextView) Findviewbyid (r.id.tv); Onclicklistener Monclicklistener = new Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method stub//do Something.}}; Tv.setonclicklistener (Monclicklistener);
3.
Package Com.yqy.yqy_click;import Android.app.activity;import Android.os.bundle;import android.view.View;import Android.view.view.onclicklistener;import Android.widget.textview;public class Mainactivity extends Activity Implements Onclicklistener {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); TextView TV = (TextView) Findviewbyid (r.id.tv); Tv.setonclicklistener (this);} @Overridepublic void OnClick (view view) {//TODO auto-generated method Stubswitch (View.getid ()) {case R.id.tv://do Someth Ing...break;default:break;}}}
4.
<!--android:clickable= "true" setting clickable permissions (Button,imagebutton, etc. control without setting the event itself with a click)--    <textview        Android:id= "@+id/tv"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content        " android:onclick= "OnClick"        android:clickable= "true"        android:text= "@string/hello_world"/>
<span style= "White-space:pre" ></span>public void OnClick (view view) {//do something ...}




4 ways to implement the OnClick event for Android controls

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.