Example:
ToggleButton and Switch |
<LinearLayout 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" android:orientation="vertical" 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=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android-coding.blogspot.com" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ToogleSwitch" /> <ToggleButton android:id="@+id/mytogglebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="ToggleButton On" android:textOff="ToggleButton Off" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch" /> <!-- Switch require API Level 14 --> <Switch android:id="@+id/myswitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="Switch On" android:textOff="Switch Off" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
package com.example.androidswitch; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; import android.widget.ToggleButton; import android.app.Activity; public class MainActivity extends Activity { ToggleButton myToggleButton; Switch mySwitch; TextView info; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); info = (TextView)findViewById(R.id.info); myToggleButton = (ToggleButton)findViewById(R.id.mytogglebutton); myToggleButton.setOnCheckedChangeListener(myOnCheckedChangeListener); mySwitch = (Switch)findViewById(R.id.myswitch); mySwitch.setOnCheckedChangeListener(myOnCheckedChangeListener); } CompoundButton.OnCheckedChangeListener myOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { info.setText( "Button: " + buttonView.toString() + "\n" + "icChecked: " + String.valueOf(isChecked)); } }; }
No comments:
Post a Comment