Android Studio: encapsulates click events and initialization controls. androidstudio
Public class MainActivity extends Activity implements View. onClickListener {private TextView Txt; private Button btn; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); InitView ();} private void InitView () {Txt = (TextView) findViewById (R. id. textView); btn = (Button) findViewById (R. id. button1); btn. setOnClickLis Tener (this) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. button1: Txt. setText ("Hello! "); Break ;}}}
Note: add the code snippet implements View. onClickListener, press Alt + Enter on MainActivity, and click implement class. The override method onClick (View v) is automatically generated, and the click event is placed in InitView () of the initialization function, note that the parameter is this.