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

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

 

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

5 Solutions collect form web for “android.widget.Switch – 开/关事件监听器?”

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将切换添加到您的布局:

  

然后在您的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布局:

    

然后创建一个活动

 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

    

活动

 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的布局是这样的。

    

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

对于那些使用Kotlin的用户,可以为交换机设置一个监听器(在这种情况下,具有ID mySwitch ),如下所示:

 mySwitch.setOnCheckedChangeListener { _, isChecked -> run { // do whatever you need to do when the switch is toggled here } } 

如果当前选中开关(ON),则isChecked为true,否则为false。

  • 如何从Firebase数据库中删除用户
  • 未解决的参考
  • Kotlinx不能解决符号“合成”
  • 在Kotlin按钮上点击HTTP请求
  • butterknife 7.x和Kotlin M14一起工作吗?
  • 使用Kotlin无法反序列化Gson的JSON(仅适用于Android KitKat,可能更低)
  • 如何在RxJava2中用重试运算符记住状态
  • 如何在不使用HTTP的情况下在KOTLIN android中请求API响应?
  • Kotlin从Recyclerview新的片段
  • 如何为kotlin中的函数参数设置不为null
  • 房间编译器java.lang.NoClassDefFoundError:org / jetbrains / annotations / NotNull
  • Kotlin language will be the best programming language for Android.