简体   繁体   中英

Android how to handle multiple switches

So I'm rather new to the android development and quickly ran into a problem, I have a view where I want to enable / disable both the bluetooth and wifi of the device, though both controlled over a seperate switch.

So here is what my view is like:

<?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>

Now I already have it working for the bluetooth, which I do as follow

@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();

    }
}

Now when I try to do the same for the wifi it will ofcourse end up using the same onCheckedChanged function, so what my ideal solution would be it how to have the wifi use it's own onCheckedChanged function?

As of now I have basically the exact same code for the WIFI and Bluetooth, but it won't work since both end up going to the same function, which makes my wifi switch change the bluetooth status.

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

    s.setOnCheckedChangeListener(this);//This goes to my onCheckedChanged
}
    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 
            }
        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM