Use Case of Switch in Android learning notes
(1) Layout File Code
(2) controlled classes
Package com. example. android_switch; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. switch; import android. widget. toast; import android. widget. toggleButton; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void onToggleClicked (View view) {/** strongly converted to Switch type */boolean isChecked = (Switch) view ). isChecked (); if (isChecked = true) {Toast. makeText (MainActivity. this, "open", 1 ). show ();} else {Toast. makeText (MainActivity. this, "close", 1 ). show () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}