![]() |
![]() |
Android Screen Layout
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 | android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" android:padding = "5dp" > < Switch android:id = "@+id/mySwitch" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentLeft = "true" android:layout_alignParentTop = "true" android:layout_marginTop = "20dp" android:text = "Play with the Switch" /> < TextView android:id = "@+id/switchStatus" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentLeft = "true" android:layout_below = "@+id/mySwitch" android:layout_marginTop = "22dp" android:text = "Medium Text" android:textAppearance = "?android:attr/textAppearanceMedium" /> </ RelativeLayout > |
Android Main Activity
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package com.as400samplecode; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Switch; import android.widget.TextView; public class MainActivity extends Activity { private TextView switchStatus; private Switch mySwitch; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); switchStatus = (TextView) findViewById(R.id.switchStatus); mySwitch = (Switch) findViewById(R.id.mySwitch); //set the switch to ON mySwitch.setChecked( true ); //attach a listener to check for changes in state mySwitch.setOnCheckedChangeListener( new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked){ switchStatus.setText( "Switch is currently ON" ); } else { switchStatus.setText( "Switch is currently OFF" ); } } }); //check the current state before we display the screen if (mySwitch.isChecked()){ switchStatus.setText( "Switch is currently ON" ); } else { switchStatus.setText( "Switch is currently OFF" ); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true ; } } |
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.