Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Click event

Shulou Source: shulou.com Published: 2022-06-03 07:59:21 09月11日 Update

1. Click events in several ways:

> > 1, anonymous inner class

Public class MainActivity extends Activity {

Private Button btn

Private TextView tv_show

@ Override

Protected void onCreate (Bundle savedInstanceState) {

Super.onCreate (savedInstanceState)

SetContentView (R.layout.activity_main)

Btn = (Button) findViewById (R.id.btn)

Tv_show = (TextView) findViewById (R.id.tv_show)

/ / the first anonymous inner class

Btn.setOnClickListener (new OnClickListener () {

@ Override

Public void onClick (View v) {

Tv_show.setText ("OK, I'll be right there!")

}

});

}

}

> > 2, the method implemented by the class

Private void clickTwo () {

Btn.setOnClickListener (this)

}

@ Override

Public void onClick (View v) {

Tv_show.setText ("OK, I'll be right there!")

}

> > 3PowerXML layout onClick

/ * *

* the third method: the first modification uses public, and the second method contains View.

* @ param v

, /

Public void clickThree (View v) {

Tv_show.setText ("attention, everyone: Mr. Cang came to Huayu Class 6 to inspect this afternoon!")

}

> > 4, the fourth method

Public void onClick (View v) {

Switch (v.getId ()) {

Case R.id.btn:

Tv_show.setText ("OK")

Break

Case R.id.btn2:] [

Tv_show.setText ("OK, I'll be right there!")

Break

Case R.id.btn3:

Tv_show.setText ("all right, I'll be right there! see you at Huayu Grove tonight!")

Break

Default:

Break

}

}

Tags: Method immediately event old man classmate layout way forest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology OPPO Reno MySQL vpn