2016-09-22 27 views
0

所以我對android開發並不熟悉,很快就遇到了一個問題,我有一個想要啓用/禁用設備的藍牙和WiFi的視圖,儘管兩者都受控一個單獨的開關。Android如何處理多個開關

因此,這裏是我的看法是什麼樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_connect" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="nl.neverdull.payleven.safeport.Connect"> 

<Switch 
    android:text="Bluetooth" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:id="@+id/bluetooth_switch" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    tools:text="bluetooth" 
    android:contentDescription="Status of bluetooth" 
    android:textOn="@string/bluetooth_on" 
    android:textOff="@string/bluetooth_off" /> 

<Switch 
    android:text="Wifi" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="25dp" 
    android:id="@+id/wifi_switch" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    tools:text="wifi" /> 
</RelativeLayout> 

現在我已經有工作了藍牙,這一點我是跟着

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_connect); 

    blueToothStatus(); 
    wifiStatus(); 
} 

public void blueToothStatus() { 
    Switch s = (Switch) findViewById(R.id.bluetooth_switch); 


    if (s != null) { 
     s.setOnCheckedChangeListener(this); 
    } 
} 

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

    Toast.makeText(this, "Bluetooth " + (isChecked ? "enabled" : "disabled"), 
      Toast.LENGTH_SHORT).show(); 
    if(isChecked) { 
     mBluetoothAdapter.enable(); 
    } else { 
     mBluetoothAdapter.disable(); 

    } 
} 

現在,當我試圖做同樣的對於WiFi,它最終會使用相同的onCheckedChanged函數,所以我的理想解決方案是如何讓wifi使用它自己的onCheckedChanged函數?

截至目前我已經基本上爲WIFI和藍牙完全相同的代碼,但它不會工作,因爲雙方最終會以相同的功能,這使得我的wifi開關改變藍牙狀態。

public void wifiStatus() { 
    Switch s = (Switch) findViewById(R.id.wifi_switch); 

    s.setOnCheckedChangeListener(this);//This goes to my onCheckedChanged 
} 
+0

您可以定義onCheckedChanged監聽器,例如強似「這個」 – kingston

+0

的其它選項建議立即進行刪除是檢查buttonView的id第一像 一個匿名類,如果(buttonView.getId() == bluetoothSwitchID) 其他 –

回答

1
public void wifiStatus() { 
     Switch s = (Switch) findViewById(R.id.wifi_switch); 

     s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       // do what you need to do 
      } 
     }); 
    } 
+1

這正是我在我看來可能保持代碼的可讀性,而不是與switch語句結束了的乾淨的方式尋找,也!非常感謝你! – killstreet