3

如何以编程方式触发以下方法:(无需单击/滑动开/关切换开​​关)

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
  //randomness
}

我想另一种方法是,打电话

switch1.setChecked(true);

然后进行手动检查。

4

2 回答 2

3

如果您引用类方法,实际上有一种调用它的方法;)

ToggleButton t = (ToggleButton) findViewById(R.id.myToggle); t.performClick();

请参阅http://developer.android.com/reference/android/widget/CompoundButton.html#performClick()

于 2014-09-22T22:11:25.607 回答
0

没有办法onCheckedChanged像这样调用 CompoundButton(ToggleButton 或 Switch)。

但是,我看到了另外两个解决方案:

  1. 保留侦听器对象并onCheckedChanged使用按钮和布尔值手动调用该函数。
  2. 将实现内部的逻辑抽象CompoundButton.OnCheckedChangedListener到另一个可以手动调用的函数中。
于 2013-10-16T21:11:42.077 回答