android.widget.Switch – 开/关事件监听器?

我想实现一个开关button,android.widget.Switch(可从API v.14中获得)。

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

但我不知道如何添加一个事件监听器的button。 它应该是一个“onclick”的听众? 我怎么知道它是否“打开”?

Switchinheritance了CompoundButton的属性,所以我会推荐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 } }); 

使用以下片段通过XML将切换添加到您的布局:

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

然后在您的Activity的onCreate方法中,获取对您的Switch的引用并设置其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); } }); 

定义你的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> 

然后创build一个活动

 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 } } **** } 

========对于下面的API 14使用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> 

活动

 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 } } ***** } 

Switch widget的布局是这样的。

 <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> 

在Activity类中,可以通过两种方式进行编码。 取决于你使用的代码。

第一条路

 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"); } } 

第二种方式

 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"); } } }); } }