Android.widget.Switch – activar / desactivar el detector de eventos?

Me gustaría implementar un botón de conmutación, android.widget.Switch (disponible en API v.14).

<Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch" /> 

Pero no estoy seguro de cómo agregar un detector de eventos para el botón. ¿Debería ser un oyente "onclick"? ¿Y cómo yo sabría si su toggled "encendido" o no?

Switch hereda los atributos de CompoundButton , por lo que recomendaría el OnCheckedChangeListener

 mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // do something, the isChecked will be // true if the switch is in the On position } }); 

Utilice el siguiente fragmento para agregar un cambio a su diseño mediante XML:

 <Switch android:id="@+id/on_off_switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="OFF" android:textOn="ON"/> 

A continuación, en el método onCreate de su actividad, obtenga una referencia a su Switch y defina su OnCheckedChangeListener:

 Switch onOffSwitch = (Switch) findViewById(R.id.on_off_switch); onOffSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Log.v("Switch State=", ""+isChecked); } }); 

Defina su diseño XML:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.neoecosystem.samplex.SwitchActivity"> <Switch android:id="@+id/myswitch" android:layout_height="wrap_content" android:layout_width="wrap_content" /> </RelativeLayout> 

A continuación, crear una actividad

 public class SwitchActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener { Switch mySwitch = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_switch); mySwitch = (Switch) findViewById(R.id.myswitch); mySwitch.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { // do something when check is selected } else { //do something when unchecked } } **** } 

======== Por debajo de API 14 use SwitchCompat =========

XML

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.neoecosystem.samplex.SwitchActivity"> <android.support.v7.widget.SwitchCompat android:id="@+id/myswitch" android:layout_height="wrap_content" android:layout_width="wrap_content" /> </RelativeLayout> 

Actividad

 public class SwitchActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener { SwitchCompat mySwitch = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_switch); mySwitch = (SwitchCompat) findViewById(R.id.myswitch); mySwitch.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { // do something when checked is selected } else { //do something when unchecked } } ***** } 

El diseño del widget Switch es algo como esto.

 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:gravity="right" android:text="All" android:textStyle="bold" android:textColor="@color/black" android:textSize="20dp" android:id="@+id/list_toggle" /> </LinearLayout> 

En la clase Activity, puede codificar de dos maneras. Depende del uso que pueda codificar.

Primera manera

 public class ActivityClass extends Activity implements CompoundButton.OnCheckedChangeListener { Switch list_toggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.return_vehicle); list_toggle=(Switch)findViewById(R.id.list_toggle); list_toggle.setOnCheckedChangeListener(this); } } public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { if(isChecked) { list_toggle.setText("Only Today's"); //To change the text near to switch Log.d("You are :", "Checked"); } else { list_toggle.setText("All List"); //To change the text near to switch Log.d("You are :", " Not Checked"); } } 

Segundo camino

 public class ActivityClass extends Activity { Switch list_toggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.return_vehicle); list_toggle=(Switch)findViewById(R.id.list_toggle); list_toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { list_toggle.setText("Only Today's"); //To change the text near to switch Log.d("You are :", "Checked"); } else { list_toggle.setText("All List"); //To change the text near to switch Log.d("You are :", " Not Checked"); } } }); } } 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.