繁体   English   中英

Android如何处理多个开关

[英]Android how to handle multiple switches

所以我对android开发比较陌生,很快遇到了一个问题,我有一个我想启用/禁用设备的蓝牙和wifi的视图,尽管它们都是通过单独的开关控制的。

所以这是我的看法:

<?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
}
    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 
            }
        });
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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