XML宣告
<togglebutton android:id="@+id/tb_wifi" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="ToggleButton"> </togglebutton> <togglebutton android:id="@+id/tb_bt" android:layout_aligntop="@+id/tv_bt" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="ToggleButton">
IMPORT
import android.widget.ToggleButton; import android.widget.CompoundButton;
CODE
private ToggleButton wifi_tb = (ToggleButton) findViewById(R.id.tb_wifi); private ToggleButton bt_tb = (ToggleButton) findViewById(R.id.tb_bt); wifi_tb.setOnCheckedChangeListener(listener); bt_tb.setOnCheckedChangeListener(listener);
判斷togglebutton開關狀態
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { if (isChecked) //ToggleButton on { switch (buttonView.getId()) //判斷哪個ToggleButton被選取 { case R.id.tb_wifi: //選取後要做的事情 break; case R.id.tb_bt: //選取後要做的事情 break; } } else //ToggleButton on { switch (buttonView.getId()) //判斷哪個ToggleButton被選取 { case R.id.tb_wifi //取消後要做的事情 break; case R.id.tb_bt: //取消後要做的事情 break; } } } };
參考連結
http://goo.gl/rgs3Dhhttp://goo.gl/5qrzZe
沒有留言:
張貼留言