8/13/2019 04 STP Android Chapter4 2 en v1
1/21
Chapter 4. Graphical UserInterfaces
Hanoi University of Science and
Technology
Nguyen Hong Quang19/8/2012
Tr!"ng #$i h%c Bch Khoa H N&iHanoi University of Science and Technology
HUST 2012
8/13/2019 04 STP Android Chapter4 2 en v1
2/21
Content! 4.2. Basic Widgets
! Images! EditText! Checkbox! RadioGroup
8/13/2019 04 STP Android Chapter4 2 en v1
3/21
4.2. Basic Widgets
Images!Android has two widgets to help embed
images in your activities:
! ImageView! ImageButton
! android:src specify what picture touse
8/13/2019 04 STP Android Chapter4 2 en v1
4/21
4.2. Basic Widgets
Images - Example
8/13/2019 04 STP Android Chapter4 2 en v1
5/21
4.2. Basic Widgets
EditText! android:autoText, to control if the field should
provide automatic spelling assistance
!android:capitalize, to control if the field shouldautomatically capitalize the first letter of enteredtext (e.g., first name, city)
! android:digits, to configure the field to accept onlycertain digits
! android:singleLine, to control if the field is forsingle-line input or multiple-line input
! (e.g., does move you to the next widget or adda newline?)
8/13/2019 04 STP Android Chapter4 2 en v1
6/21
4.2. Basic Widgets
EditText - Example
EditTextfld=(EditText)findViewById(R.id.field);
fld.setText("Licensed under the Apache
License, Version 2.0 );
8/13/2019 04 STP Android Chapter4 2 en v1
7/21
4.2. Basic Widgets
CheckBox! The classic checkbox has two states:
checked and unchecked.
! Clicking the checkbox toggles betweenthose states to indicate a choice
8/13/2019 04 STP Android Chapter4 2 en v1
8/21
4.2. Basic Widgets
CheckBox - methods! isChecked() to determine if the checkbox has been
checked
! setChecked() to force the checkbox into a checkedor unchecked state
! toggle() to toggle the checkbox as if the userchecked it
! Listener object : an instance ofCompoundButton.OnCheckedChangeListener
8/13/2019 04 STP Android Chapter4 2 en v1
9/21
4.2. Basic Widgets
CheckBox - Examples
8/13/2019 04 STP Android Chapter4 2 en v1
10/21
4.2. Basic Widgets
CheckBox - Examples
cb=(CheckBox)findViewById(R.id.check);cb.setOnCheckedChangeListener(this);
8/13/2019 04 STP Android Chapter4 2 en v1
11/21
4.2. Basic Widgets
CheckBox - Examplespublic void onCheckedChanged(CompoundButton
buttonView, boolean isChecked) {
if (isChecked) {cb.setText("This checkbox is: checked");
}
else {
cb.setText("This checkbox is: unchecked");}
}
8/13/2019 04 STP Android Chapter4 2 en v1
12/21
4.2. Basic WidgetsRadioButton
! Radio buttons are two-state, likecheckboxes, but can be grouped such that
only one radio button in the group can bechecked at any time
8/13/2019 04 STP Android Chapter4 2 en v1
13/21
4.2. Basic WidgetsRadioButton
! Like CheckBox, RadioButton inherits fromCompoundButton, which in turn inherits from
TextView. Hence, all the standard TextViewproperties for font face, style, color, etc. areavailable for controlling the look of radio buttons.
! Similarly, we can call isChecked() on aRadioButton to see if it is selected, toggle() toselect it, and so on.
8/13/2019 04 STP Android Chapter4 2 en v1
14/21
4.2. Basic WidgetsRadioGroup
! Put your RadioButton widgets inside ofa RadioGroup
! RadioGroup indicates a set of radiobuttons whose state is tied, meaning
only one button out of the group can be
selected at any time
8/13/2019 04 STP Android Chapter4 2 en v1
15/21
4.2. Basic WidgetsRadioGroup
! check() to check a specific radio button via its ID(e.g., group.check(R.id.radio1))
! clearCheck() to clear all radio buttons, so none inthe group are checked
! getCheckedRadioButtonId() to get the ID of thecurrently-checked radio button (or -1 if none arechecked)
8/13/2019 04 STP Android Chapter4 2 en v1
16/21
4.2. Basic WidgetsRadioButton - Example
8/13/2019 04 STP Android Chapter4 2 en v1
17/21
4.2. Basic WidgetsRadioButton - Exercise
! If wetoggleon the"vertical"radiobutton,
the topRadioGroupadjusts
to match
8/13/2019 04 STP Android Chapter4 2 en v1
18/21
4.2. Basic WidgetsRadioButton - Exercise
8/13/2019 04 STP Android Chapter4 2 en v1
19/21
4.2. Basic WidgetsRadioButton - Exercise
! G!i "(RadioGroup):! setOrientation(LinearLayout.HORIZONTAL/VERTICAL);
! setGravity(Gravity.LEFT/ CENTER_HORIZONTAL/RIGHT);
T !" # i h B h Kh H N&i
8/13/2019 04 STP Android Chapter4 2 en v1
20/21
Q&A
End of Lecture
Tr!"ng #$i h%c Bch Khoa H N&iHanoi University of Science and Technology
8/13/2019 04 STP Android Chapter4 2 en v1
21/21