Android 多状态按钮 ToggleButton

ToggleButton
     选中状态,未选中状态并且需要为不同的状态设置不同的显示文本。
     属性:
          checked="true"
          textOff=""
          textOn=""
 
private ToggleButton tb ;
private ImageView iv ;
     tb=(ToggleButton) findViewById(R.id.toggleButton1);
      iv=(ImageView) findViewById(R.id. imageView1);
      //给togglebutton 设置监听器
      tb.setOnCheckedChangeListener( new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
           /*
            *  当TB被点击的时候,当前的方法会被执行
            *  buttonView  代表被点击的控件的本事(对象)
            *  isChecked  代表被点击的控件的状态(On/Off)
            */
             iv.setImageResource(isChecked?R.drawable. on:R.drawable.ic_launcher);
         }
      });
     
 
 
 
 <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textOn="@string/textOn"
        android:textOff="@string/textOff"
        android:hint="@string/ToggleButton" />
 
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/ic_launcher" />
stareblankly.cn
原文地址:https://www.cnblogs.com/stareblankly/p/4829249.html