这个和switch太相似了,用toast小试牛刀:
public class MainActivity extends AppCompatActivity {ToggleButton toggleButton;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);toggleButton=(ToggleButton)findViewById(R.id.toggleButton);toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) {if (b){Toast.makeText(getApplicationContext(),"on",Toast.LENGTH_SHORT).show();}else {Toast.makeText(getApplicationContext(),"off",Toast.LENGTH_SHORT).show();}}});} }