chapter 6b(standard widgets)

Post on 05-May-2017

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Android development

Standard Android Controls(CheckBox and RadioButton)

Using CheckBox• The check box button is often used in lists of

items where the user can select multiple items. • The Android check box contains a text attribute

that appears to the side of the check box. • Because the Checkbox class is derived from the

TextView and Button classes, much of the attributes and methods behave in a similar fashion.

Using CheckBox• Here’s an XML layout resource definition for a

simple CheckBox control with some default text displayed:

<CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check me" />

Event Handling In CheckBox• It can be done in 2 ways:A.1. Implement the OnCheckedChangeListener

interface2. Register the CheckBox object as source and

Activity class as Listener by calling the setOnCheckedChangeListener( ) method.

3. Override the method onCheckedChanged( )

Event Handling In CheckBoxB.1. Implement the OnClickListener interface2. Register the CheckBox object as source and

Activity class as Listener by calling the setOnClickListener( ) method.

3. Override the method onClick( )

Determining State Of CheckBox

• To determine whether CheckBox is on or off we can use the method isChecked( ) of CheckBox object:

• It’s prototype is:public boolean isChecked()

• It returns true if the CheckBox is on otherwise it returns false

Setting State Of CheckBox• To programmatically set the state of CheckBox

we can use it’s method setChecked( )• It’s prototype is

public void setChecked(boolean) • If we pass “true” then CheckBox becomes

selected (ON) and if “false” is passed it becomes deselected (OFF)

EXERCISE

Using RadioButton• RadioButton controls are two-state widgets that

can be in either a checked or unchecked state. • The difference between check boxes and radio

buttons is that you can select more than one check box in a set, whereas radio buttons are mutually exclusive—only one radio button can be selected in a group.

• Selecting a radio button automatically deselects other radio buttons in the group.

Creating RadioButton• To make RadioButtons mutually exclusive,

they are grouped together into the RadioGroup element so that no more than one can be selected at a time.

• To create a group of radio buttons, we first create a RadioGroup and then populate the group with few RadioButton controls

Creating RadioButton<RadioGroup android:id="@+id/group_hotel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView1"> <RadioButton android:id="@+id/radio_fivestar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Five Star " /> <RadioButton android:id="@+id/radio_threestar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Three Star" /></RadioGroup>

Event Handling In RadioButton

• It can be done in 2 ways:A.1. Implement the OnCheckedChangedListener

interface2. Register the RadioButton object as source

and Activity class as Listener by calling the setOnCheckedChangeListener( ) method.

3. Override the method onCheckedChanged( )

Event Handling In RadioButton

B.1. Implement the OnClickListener interface2. Register the RadioButton object as source

and Activity class as Listener by calling the setOnClickListener( ) method.

3. Override the method onClick( )

RadioButton Methods• public boolean isChecked()

• public void setChecked(boolean)

• public EdiTable getText()

RadioButton Methods• public void setText(CharSequence)

• public void setOnClickListener(OnClickListener)

• public void setOnCheckedChangeListener(OnCheckedChangeListener)

EXERCISE

top related