切换按钮不在Android上切换

时间:2016-04-14 21:17:23

标签: android android-togglebutton

我有一个类,其切换按钮定义如下:

public class DeviceControlActivity extends Activity implements View.OnClickListener
private ToggleButton b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gatt_services_characteristics);
b2 = (ToggleButton) findViewById(R.id.button2);
        b2.setOnClickListener(this); // calling onClick() method
        b2.setBackgroundColor(Color.GRAY);
        b3 = (ToggleButton) findViewById(R.id.button3);
        b3.setOnClickListener(this);
        b3.setBackgroundColor(Color.GRAY);

@Override
    public void onClick(View v) {
        // default method for handling onClick Events..
        switch (v.getId()) {

            case R.id.button2:
                // call writeCharacteristic but concatenate pin #
                if (b2.isChecked()) {
                    b2.setChecked(false);
                    b2.setBackgroundColor(Color.GRAY);
                    // do code to send pin# with writeCharacteristic
                    String str = "Pin11,0" + "\n";
                    Log.d(TAG, "Sending OFF result=" + str);
                    /*final byte[] tx = str.getBytes();
                    if(mConnected) {
                        characteristicTX2.setValue(tx);
                        mBluetoothLeService.writeCharacteristic(characteristicTX2);
                        mBluetoothLeService.setCharacteristicNotification(characteristicRX2,true);
                    }*/
                } else if (!b2.isChecked()) {
                    b2.setChecked(true);
                    b2.setBackgroundColor(Color.BLUE);
                    // do code to send pin# with writeCharacteristic
                    String str = "Pin11,1" + "\n";
                    Log.d(TAG, "Sending ON result=" + str);
                    /*final byte[] tx = str.getBytes();
                    if(mConnected) {
                        characteristicTX2.setValue(tx);
                        mBluetoothLeService.writeCharacteristic(characteristicTX2);
                        mBluetoothLeService.setCharacteristicNotification(characteristicRX2, true);
                    }*/
                }
                break;
}
}

当我运行应用程序并切换b2时,我总是得到if的Sending OFF结果部分,即使在xml中切换按钮被声明为false并且按钮也没有打开或关闭。

为什么会这样?

2 个答案:

答案 0 :(得分:2)

与所有ToggleButton一样,

CompoundButton会在点击后处理自己检查的状态。您的OnClickListener正在抵消这种行为。相反,请使用CompoundButton.OnCheckedChangeListener,并检查传入boolean的{​​{1}}是否为新状态。

答案 1 :(得分:2)

我建议你在切换按钮上使用check changed listener。

ToggleButton b2 = (ToggleButton) findViewById(R.id.button2);
b2.setBackgroundColor(Color.GRAY);
b2.setOnCheckedChangeListener(new     CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean     isChecked) {
        if (isChecked) {
            // The toggle is enabled
            b2.setChecked(false);
                b2.setBackgroundColor(Color.GRAY);
                // do code to send pin# with writeCharacteristic
                String str = "Pin11,0" + "\n";
                Log.d(TAG, "Sending OFF result=" + str);
                /*final byte[] tx = str.getBytes();
                if(mConnected) {
                    characteristicTX2.setValue(tx);
                    mBluetoothLeService.writeCharacteristic(characteristicTX2);
                    mBluetoothLeService.setCharacteristicNotification(characteristicRX2,true);
                }*/
        } else {
            // The toggle is disabled
        }
    }
});

由于没有看到你的XML,听起来当点击按钮时也没有调用onClick方法。