ToggleButton tb = new ToggleButton(this); tb.setId(1); linear1.addView(tb);
final ToggleButton tbc = (ToggleButton)findViewById(1);
tb.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (tbc.isChecked()) {
textview1.setText("ON");
} else {
textview1.setText("OFF");
} }});
//Full
ToggleButton toggleButton = new ToggleButton(this);
toggleButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String msg = "Toggle Button is " + (isChecked ? "ON" : "OFF");
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
if (linear1 != null) {
linear1.addView(toggleButton);
}
}
//set Checked
toggleButton.setChecked(true);
//curren checked
toggleButton.isChecked();
//text
toggleButton.setTextOn("ON");
toggleButton.setTextOff("OFF");
//textColor
toggleButton.setTextColor(Color.RED);
//textSize
toggleButton.setTextSize(25);
//bgColor
toggleButton.setBackgroundColor(Color.BLACK);
//Aide
//ID
<ToggleButton
android:id="@+id/simpleToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
//Current State
android:checked="true"
//Gravity
android:gravity="right|center_vertical"
//TextON/OF
android:textOff="Disable"
android:textOn="Enable"
//TextColor
android:textColor="#f00"
//textSize
android:textSize="25sp"
//TextStyle
android:textStyle="bold|italic"
//BgColor
android:background="#000"
//Padding
android:padding="40dp"
* paddingLeft
* paddingRight
* paddingTop
* paddingBottom
//Drawable
android:drawableTop="@drawable/ic_launcher"
* drawableBottom
* drawableTop
* drawableRight
* drawableLeft
Komentar