ToggleButtonActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; import android.widget.ToggleButton; public class ToggleButtonActivity extends Activity { int count = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final LinearLayout bg = (LinearLayout) findViewById(R.id.linearlayout); final ToggleButton button = (ToggleButton) findViewById(R.id.togglebutton); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (button.isChecked()) { bg.setBackgroundColor(0xFFF3F3F3); } else { bg.setBackgroundColor(0xFF000000); } } }); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FF000000" > <ToggleButton android:id="@+id/togglebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="Start " android:textOn="Stop " android:textSize="24sp" /> </LinearLayout> |