簡體   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