Prevent a ToggleButton from changing state onClick

时间:2015-07-28 16:36:34

标签: android

This question is similar to this. However it did not solve my problem.

I have a ToggleButton and when a user clicks, I do not want to change the state of the ToggleButton, as I am programatically changing the state from another Activity. How do I override it?

Here is my Activity code:

<ToggleButton
       android:id="@+id/alarm1"
       android:background="@drawable/check"
       android:layout_alignParentLeft="true"
       android:layout_margin="8dp"
       android:textOn=""
       android:onClick="alarmSet1"
       android:textOff=""
       android:focusable="false"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

Here is the Java code:

public void alarmSet1(View view)
{
    int a1=1;
    int idTime = (int) System.currentTimeMillis();
    Intent intent = new Intent(MainActivity.this, AddAlarm.class);
    intent.putExtra("pendInt",idTime);
    intent.putExtra("tts",a1);
    startActivity(intent);
}

2 个答案:

答案 0 :(得分:0)

很简单: 创建一个CompoundButton.OnCheckedChangeListener并像这样覆盖onCheckedChanged:

private CompoundButton.OnCheckedChangeListener _toggleButtonListener = new  CompoundButton.OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        compoundButton.setChecked(false);

        // And now, do whatever you want.
    }
};

当然,您必须将侦听器附加到您的按钮上:

... somewhere in the onCreate() method:
ToggleButton toggleButton=findViewById(R.id.alarm1);
toggleButton.setOnCheckedChangeListener(_toggleButtonListener);

就这样。

继续编码,让代码随身携带!

答案 1 :(得分:0)

我迟到了,但我有完全相同的问题,而且给出的答案不符合我的需要,所以我自己做了一些事情。

我想要什么(如果这不是最初的问题,请纠正我):

  • 我想拦截 SwitchRadioButtonCheckBox 或任何其他 CompoundButton 上的点击/滑动,确保按钮本身不会切换状态,但是而是调用一个最终会导致以编程方式设置正确状态的函数。 (例如,使用 LiveData),因此按钮的状态始终与其切换的数据状态相同。

我做了什么:

/**
 * Use this if you want your compoundButton to do something when (un) checked,
 * but not change it's state by itself.
 *
 * It will block the actual changing of the button, but instead run [onCheckedChanged]
 * which should in turn eventually lead to setting `isChecked` to get proper setting of the switch
 * after its action has been performed
 * This might cause some trouble as it will trigger on programmatic sets outside of this listener.
 * To get around that, use [setIsCheckedWithoutBypassedListener] if you don't want that to happen.
 */
fun CompoundButton.setInterceptedOnCheckedChangedListener(onCheckedChanged: CompoundButton.OnCheckedChangeListener){
    compoundButtonListeners[this] = onCheckedChanged
    setOnCheckedChangeListener { compoundButton, b ->
        compoundButton.isChecked = !compoundButton.isChecked
        onCheckedChanged.onCheckedChanged(compoundButton, b)
    }
}

/**
 * Set value without triggering the listener added in [setInterceptedOnCheckedChangedListener]
 */
fun CompoundButton.setIsCheckedWithoutBypassedListener(isChecked: Boolean){
    compoundButtonListeners[this]?.let{ l ->
        setOnCheckedChangeListener(null)
        this.isChecked = isChecked
        setOnCheckedChangeListener(l)
    }
}

/**
 * Holds the OnCheckedChangeListeners for the compoundbuttons, as they are private and cannot
 * be retreived otherwise.
 */
private val compoundButtonListeners = WeakHashMap<CompoundButton, CompoundButton.OnCheckedChangeListener>()

如果这最终会在您的代码中放入很多 _, _ ->,请随意将 CompoundButton.OnCheckedChangeListener 替换为 View.OnClickListener

留在这里以防其他人有和我一样的问题。

我很确定使用 Wea​​kHashMap 可以防止在重新创建活动时发生内存泄漏。如果不是这种情况,请教育我:)