2015-09-04 92 views
-1

我如何知道toggleButton被選中或未被選中。 請你回答我的問題becouse我需要it.Thank所有我如何知道toggleButton被選中或未被選中

XML:

<ToggleButton 
     android:id="@+id/toggleButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

的java:

public class Setting extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_setting); 
} 
} 
+0

http://www.compiletimeerror.com/2013/08/android-togglebutton-example.html#.VelcWGSqpBc – SANAT

+0

當你想知道的切換按鈕的狀態?在'onCreate'? – Panther

回答

3

檢查響應按鈕按下的

http://developer.android.com/guide/topics/ui/controls/togglebutton.html

ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton); 
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     if (isChecked) { 
      // The toggle is enabled 
     } else { 
      // The toggle is disabled 
     } 
    } 
}); 

使用XML屬性

android:checked="true" 
相關問題