2012-06-30 57 views
129

我想實現一個開關按鈕android.widget.Switch(可從API v.14獲得)。android.widget.Switch - 開/關事件監聽器?

<Switch 
    android:id="@+id/switch1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Switch" /> 

但我不知道如何添加一個事件偵聽器的按鈕。它應該是一個「onclick」聽衆嗎?我怎麼知道它是否打開「打開」?

+3

通過XML的OnClick確實可行 - 但僅適用於按鈕上的「點擊」,而不是「幻燈片」。 – m02ph3u5

回答

290

交換機繼承CompoundButton的屬性,所以我會建議OnCheckedChangeListener

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     // do something, the isChecked will be 
     // true if the switch is in the On position 
    } 
}); 
+0

工程就像一個魅力,謝謝山姆 – Johan

+1

@約翰沒有麻煩。我不知道你,但我希望他們把它命名爲OnCheckChangedListener,類似於OnItemSelectedListener,因爲On-_Noun _-_ Verb_-Listener是一個確定的命名對話。 – Sam

+5

長按按鈕然後輕掃/切換它不起作用。 –

29

使用下面的代碼片段通過XML到交換機添加到您的佈局:

<Switch 
    android:id="@+id/on_off_switch" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textOff="OFF" 
    android:textOn="ON"/> 

然後在你活動的onCreate方法,獲取對您的開關的引用並設置其OnCheckedChangeListener:

Switch onOffSwitch = (Switch) findViewById(R.id.on_off_switch); 
onOffSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    Log.v("Switch State=", ""+isChecked); 
}  

}); 
+3

這是一個更清晰的答案,讓您可以在後面匹配佈局和代碼。 – AshesToAshes

+0

如何在單個監聽器中管理多個switchcompat?請建議回答爲 –

16

定義你的XML佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.neoecosystem.samplex.SwitchActivity"> 

    <Switch 
     android:id="@+id/myswitch" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

</RelativeLayout> 

然後創建活動

public class SwitchActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener { 

    Switch mySwitch = null; 


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


     mySwitch = (Switch) findViewById(R.id.myswitch); 
     mySwitch.setOnCheckedChangeListener(this); 
    } 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     if (isChecked) { 
      // do something when check is selected 
     } else { 
      //do something when unchecked 
     } 
    } 

    **** 
} 

========對於低於API 14使用SwitchCompat =========

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.neoecosystem.samplex.SwitchActivity"> 

    <android.support.v7.widget.SwitchCompat 
     android:id="@+id/myswitch" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

</RelativeLayout> 

活動

public class SwitchActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener { 

    SwitchCompat mySwitch = null; 


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


     mySwitch = (SwitchCompat) findViewById(R.id.myswitch); 
     mySwitch.setOnCheckedChangeListener(this); 
    } 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     if (isChecked) { 
      // do something when checked is selected 
     } else { 
      //do something when unchecked 
     } 
    } 
    ***** 
} 
+2

不要忘記檢查buttonView.isPressed() – JacksOnF1re

4

Switch小部件的佈局是這樣的。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <Switch 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="20dp" 
     android:gravity="right" 
     android:text="All" 
     android:textStyle="bold" 
     android:textColor="@color/black" 
     android:textSize="20dp" 
     android:id="@+id/list_toggle" /> 
</LinearLayout> 

在Activity類中,可以通過兩種方式進行編碼。取決於你可以使用的代碼。

第一種方式

public class ActivityClass extends Activity implements CompoundButton.OnCheckedChangeListener { 
Switch list_toggle; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.return_vehicle); 

    list_toggle=(Switch)findViewById(R.id.list_toggle); 
    list_toggle.setOnCheckedChangeListener(this); 
    } 
} 

public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { 
    if(isChecked) { 
     list_toggle.setText("Only Today's"); //To change the text near to switch 
     Log.d("You are :", "Checked"); 
    } 
    else { 
     list_toggle.setText("All List"); //To change the text near to switch 
     Log.d("You are :", " Not Checked"); 
    } 
} 

方式二

public class ActivityClass extends Activity { 
Switch list_toggle; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.return_vehicle); 

    list_toggle=(Switch)findViewById(R.id.list_toggle); 
    list_toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if(isChecked) { 
      list_toggle.setText("Only Today's"); //To change the text near to switch 
      Log.d("You are :", "Checked"); 
      } 
      else { 
      list_toggle.setText("All List"); //To change the text near to switch 
      Log.d("You are :", " Not Checked"); 
      } 
     }  
    }); 
    } 
} 
0

對於使用科特林,可以設置一個偵聽的開關(在本例中具有ID mySwitch)如下:

mySwitch.setOnCheckedChangeListener { _, isChecked -> run { 
     // do whatever you need to do when the switch is toggled here 
    } 
} 

isChecked如果當前選中開關(ON),則爲true,否則爲false伊勢。