java中:

 1 public class MainActivity extends Activity implements OnCheckedChangeListener{
 2 
 3     private Switch aSwitch;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         aSwitch = (Switch)findViewById(R.id.switch1);
10         aSwitch.setOnCheckedChangeListener(this);
11     }
12     @Override
13     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
14         if(isChecked){
15             Log.e("", "checked");
16         }
17         if(!isChecked){
18             Log.e("", "not checked");
19         }
20 
21     }

xml中:

1             <Switch
2                 android:layout_width="wrap_content"
3                 android:layout_height="wrap_content"
4                 android:id="@+id/switch1"
5                 android:checked="true" />

注意记住要再onCreate时.setOnCheckedChangeListener(this)

相关文章:

  • 2021-12-27
  • 2021-04-17
  • 2021-12-27
  • 2022-12-23
  • 2021-12-27
  • 2021-12-27
  • 2021-12-25
  • 2021-12-27
猜你喜欢
  • 2021-12-27
  • 2021-08-15
  • 2021-06-28
  • 2021-07-09
  • 2022-12-23
  • 2021-12-23
  • 2022-12-23
相关资源
相似解决方案