2016-04-14 174 views
0

我有開關按鈕類,像這樣定義的:切換按鈕不切換在Android

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

,我總是得到的送別結果部分如果,即使在xml中,切換按鈕聲明爲false,並且按鈕不會打開或關閉。

這是怎麼發生的?

回答

2

ToggleButton s,像所有的CompoundButton一樣,在點擊時照顧自己的檢查狀態。您的OnClickListener正在抵消該行爲。相反,請使用CompoundButton.OnCheckedChangeListener,並檢查傳入onCheckedChanged()boolean是否爲新狀態。

+0

太棒了!擴展了正確的類,實現了方法,取代了buttonView,並註釋掉了我的手動切換。現在唯一的問題是它們應該根據xml開始爲false,但是在第一次點擊時,我得到了發送關閉結果,這發生在b2.isChecked()條件爲真時發生。 – marciokoko

+0

這聽起來對我來說是正確的。如果'Button'以_off_開頭,第一次點擊就會切換到_on_('isChecked()= true'),所以你的'if'塊會被執行。在狀態發生變化後,'onCheckedChanged()'方法會被觸發,因此您可能需要將自己的邏輯從「OnClickListener」中取消。 –

2

我建議您在切換按鈕上使用檢查更改的偵聽器。

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方法也沒有被單擊按鈕時被調用。