Академический Документы
Профессиональный Документы
Культура Документы
nr
n 6n
"
ANDROID
ANDROID
nr
n11 6n
r
"'
-
-
2011
681.3.068
32.973.26-018.1
61
. .
61
Google Android: n .
.: -, 2011.-448 .: . + CD-ROM
( )
ISBN 978-5-9775-0562-8
Googl e A ndroid.
Android. ,
ndrid- .
,
-,
, ,
, A ndro i d .
Android SDK. -
.
681.3.068
32.973.26-018.1
N2 77.99.60.953..5770.05.09 26.05.2009
n n
"n ""
199034, -, 9 , 12
ISBN 978-5-9775-0562-8
. ., 2010
, "-r", 2010
.............................................
. 1
2
. 6
.. .......................................................................... ................... .
......................................................................................................................
1.
1.1.
.
.
1.1.1. IPC
.
.
1.1.2.
1.1.3.
. .
1.2. .. .
. .
..
1.2.1. libc .. ..
..
1.2.2.
1.2.3.
1.3.
...
7
8
9
.
. 9
.
. . 1
.
. 1
1
11
12
..
.........................
.........
..
............ ........
........... .........
...
.......
.
.
....... . ..
. . . . . . ....
............................................................... .......................................
.............. ........................................................................
...
.... .... . . . . . . . .
. .
. . .
2.
12
13
13
14
................................................................................ 15
2.1. .
2.1.1.
. .
.
2.1.2. JDK .
2.1.3. Eclipse
2.1.4. Android SDK
2.1.5. Andr:>id Development Tools
15
. 15
16
l7
17
18
............................................ .............
............. ............................................
..
. ..................................
.......................................... ........................................
................................................. ..........
IV
. .
... ....
. . ........ .
.. ..
........ . .....
..............
...... ............................
3.
3.1. Eclipse
3.2.
. .
3.2.1. .
Android.....................
. .. . .
.
.. . . .. .. .
. . ...
res/layout/ .
......... ...................... 31
.
31
. 35
36
37
res/drawaie/
37
res/values/
. ..
.
37
3.2.2. R.java ........................................................................................................ 38
3.2.3. HelloAndroidActivityjava . . . .
... .
... . . .. . . 39
3.2.4. AndroidManifest.xml
... ... . ... ....... .
. ..
. .. . .. . .. . 40
.... .
........ .. .......
... . ......
. .. .
...
. .... ... . . . .
. . . .
.
.... .. .... .
.
.. . ..
.
.
....... ..
... ....
.. ....... ............
.. ...
..
.....
....... .
.......................................... ........................................
.... . .
4.1.
4.2.
4.3.
4.4.
4.5.
. ....
...
. ..... ..
.... .........
........... .
. .. ..
. ....
.. .....
. ....
.. ..
4. ndrid- .................................................. 43
43
44
.
..
44
- ..
..
.
..
.. . .
.. 45
. .
. .
.
. . . 45
4.5.1. . . . . . . . ...
.. ...
.
. 45
4.5.2. .
.
.
46
4.6.
48
4.6.1. .. . . . .
.
. . 49
4.6.2.
.
49
4.7. AndroidManifest.xml .
. ..
.
.
49
4.7.1. . .
. .
. .
51
<manifest> . ..
. . . . . .. .
. ..
. . . 52
<permission> . . . . . . . . . . . . . . .. . . . ...... . . .
. . . 52
<uses-permission> . . ........ . .... . .. . .... .. . . .. . ... .. . .
.. . 53
<permission-tree>
.
. 53
<permission-group> .
. .
.
. .
..
. . 53
<instrumentation>
.
..
. . . . . .. . .
. . . 53
<uses-sdk> .
..
.
. .. . .
.. . .
54
<uses-configuration>
. .
. . . ....
.
. . . . .. . . ...... .......... .... 54
<uses-feature>
. .
. . .. . ..
..
.. . .
. . . . . . .. 54
<supports-screens> ...
.. . . . . .... .
. . . .. . ..
.. .
.
54
4.7.2. <application>
55
<activity>
.
56
<intent-jilter>
..
56
<action> . . . .
.
. .. ... . . .. .. . .
.. 56
........................................ . . .......................... . . . . .......................................
...................................................... ..................................................................
...........................
.............................................................
..... ..............................................................
. ........... ..
.... ...
. ..
.. .... .
. .
.... .. .. ....
.. .
....
.... . .
..... .... ..
. ....
...
.. . .......
. .
........ . ....... . . . .
............
....
........
.. .
. ..... .
..... ...... . . .. ..
. . .. .
.. ....... .....
.. .
... .
. .. ..... . .. ..
.. . ..............
..
.. .. ........... . . . ... . .
.. .
......
... ......
..
...........
................................................................................................. .........
.. .
...
..
. . ...........................
<category> .
.
.
. .
. ..
.
.
.
57
<data> . . . . . . . . . , .............................................................................. 57
<meta-data> ... . ..... .. . . .. . . . .
.. . .. .
. . ..
. .
57
<activity-alias> . . . .. .. .. . . ..... .. .. . ...... .
. .
.
. . . 57
<service> .. .. . . . . .......... . .
....
. . . . .. ... . . . . .
.
. 58
<receiver> .. .... . . . .... ...... .. . .. .
.. . ... .. . .. . . .. . ..... . . . 58
<provider> . .
... .. .. . . .. ..
.... .. . . . ...... . ..... .. .. . . ... ... . ..... 58
<grant-uri-permission> . .
. ...
. .
.
.
.. . . . .
. . 58
<path-permission>
. .
.. . 59
<uses-library> . .
. . ... ..
..
.. . . .
..
59
......... ............. ..... ... .......................... .. . ...... ....... .............. ........
. ....
..
....
...
..
. . ..
.... . ...
. ... . ...
.. .. ... . ..
..
. . ......
.. .... . ..
. .. .......
... . ...
..
. .
. . .
.....
... . ...... ..
...
. ..
... .........
.......
........... .. ...........
.... ......... ..
.. ..
..
............... ..... .. .
.... .
.. . ..... .
. . .
. .
..
.. ...
..
. ... .... .
. . .
. .
.. .. . .... ...... . .
5.
..
...... . ...........
... ....
..............................
........................................... 61
5.1.
..
.
..
5.2.
5.2.1. XML . . .. . . .
. . .. .
.. . .
. . .
5.2.2. L- .. .
.
.. . .
.
5.3. .. . . .. ... ..
.
.... . .
5.4. . .
. . . . . .
..
5.4.1. FrameLayout
..
.
..
.
.
.
5.4.2. LinearLayout
5.4.3. TaleLayout .
. ... ... . ...
. . .
.
5.4.4. RelativeLayout . .
. . .
. .
.
.
5.5. Hierarchy Viewer
5.5.1. Layout View
.
.. . ..
.
... .
.
.
.
5.5.2. Pixel Perfect View.
. ........ . . . .. .
..
.
.
.
...............
62
62
64
65
67
68
68
70
74
78
81
82
. 83
.............................................. . ........................................................................
. .
. . .. ..... ... .
.. .............
.. .... .
...
...
.
..
..
.
.
.. .. .............. . ..
.
.
. ...... .
....
. ..
.............
......... ........................... . . .
............................................ ......
........... ........
. .
........ ..
....... ......
.... . ... .
.
..
... .................
6. ................................................................................ 85
6.1. .
.
..
6.1.1. TextView .
.
.
..
..
..
...
6.1.2. EditText .
. .. . .
. .
.
6.2.
. . .
6.3. .. .
..
6.4.
. . .. . .. .
.
6.5. ..
.
..
. . .
6.5.1. Button
.. . . . ..
.
....
6.5.2. RadioButton RadioGroup . .
6.5.3. CheckBox .
.. . .
6.5.4. ToggleButton
6.5.5. lmageButton
.. . .
.
6.6.
.
.
..
.
.. .
6.7.
.. .
6.7.1. ProgressBar .. .. .
..
. .
6.7.2. SeekBar
. . ..
.
.
6.7.3. RatingBar . . . . . .
.
..
. .
.. . 85
86
.
90
.
. 92
.
95
. 97
.
98
. . . . 99
. .
106
.. . . . 109
112
..
115
117
.
120
.
121
125
. . 129
.... .......... ............... . ........... . . . . ........ ................ ..... ... .......... .....
..... . .......
........
... .
............................. ..............................
.
.
...
... ......................
..... .... ..
.......
.. ..
.. ..
..
.
... .
.
.
..
...
. .
.........
. ................. ..
..
. ... . ....
........ ...........
..
.
.
..
.. .................. . .. .. .....................
...............
..
. .
... . .
...............
.
.
..
..
Vl
6.8.
6.8.1. AnalogClock Digita/Clock. . . .
6.8.2. Chronometer .. .. .
.
.. .
.................. .....................
. ....................... .....
133
. 134
. 135
.. .. . ........................................... ...................... .
7. -
. ... ...
..
...
..
7.1.
..
. .
.
.
. 139
7.2.
. . .
.
.. .
140
7.2.1. AutoCompleteTextView
. . . .. . .
..
. . .
.
140
7.2.2. MultiAutoCompleteTextView . ..... .
.
.
.
.
143
7.3. .
... . . . .
146
7.3.1. ListView ............................................................................................................ 147
7.3.2. Spinner.... . . ..
.
. . ..
. .. .
. 149
7.3.3. GridView. .
..
..
.
. ... ....
153
7.4. .
. . .
.
157
7.4.1. GridView ...........
.
.
. . . . . . 15 7
7.4.2. Gallery
.. .. ... ...
. .
..
. .. . .. .. . .. 161
7.4.3. SlidingDrawer .
.
.
. .
.. ..
.
.. . .. .. 164
7.5.
. . .. . .
. .. . .. ... 170
.....
.................... . . ....
.. .....
..... ...................
. . .
... .........
..........
....
. ............ .
....................
....................
....
......... ..
....
........................ .. ..
8. ......... .
... . .
. .........
.. .
....... ......
.. . ....... ..
..
. ....
. .. .
. . ................ ..
..
. ..
.. ............................. .....
175
8.1.
.
.
. .. .
.
. .. ... 17 5
8.2.
.
. .
. .. . . ... 179
8.3.
.
... .. .. . . . .... . . ..
...... . 182
8.4. . ..
. . . . 187
..................... ...... ............. .. . .. ........ ................
............. ........
. .. . .. . .
.... .. .
... ....
9.
. .
.....
..... .. ............
... .
....
..
.. ................
.. ... .
..
...........
........... ..
..
. .. . ..
.. . ... . . . 191
.........
. .
. ... .
. ..
...... .........
..
. .. . .
. ...........
10.1.
10.1.1.
10.1.2.
10.2.
.
..
... .. .....
.. ... . . . .. 225
........
...
.. ... . .
...
226
230
. .. 233
. ..
. .
..
..
..
........ ..
..
237
V/1
10.3.
10.4.
..................................................................................................................
11.
.....
..................................................
240
244
11.1.
.
.
11.1.1.
11.1.2.
11.2.
11.3.
11.4.
11.5.
..
. 249
253
254
254
257
257
.
271
........................................................
........................ .............................................................
.............................
12.
.......................
........................... .......
12.1.
12.2. . .
. . .
. .
.. . .
275
. 277
...... ................. ... .. ..... ...... ......... ...... ........ ... ...............
13.1. . .
. . . 286
13.2.
..
. ...
.
..
.. . 287
13.3. .
.
... . .. . .. . 288
13.3.1. .
. .
.
. 288
13.3.2.
.
..
. .
..
.
. . . . 289
13.3.3. -
.
.
. 290
13.3.4. - . . . .
. . . . 292
... ... . . . . . .
.....
. ......... . .. ..
.... .. .
.. . .
..
..
. ..
.... .
................................. ..................
...... ......... .. .. ..
..... ... .
14.
....................................... .............. ....................................................... 295
14.1. .
..
.
.
.
.
.
14.2.
.
. . . .
. . .
.
..
14.2.1.
. . ..
.
. ..
14.2.2. CheckBoxPreference
.
.
.
14.2.3. EditTextPreference
14.2.4. ListPreference
.
..
. ..
. .
14.2.5. RingtonePreference
.
.
. .
..
. .
14.2.6. PreferenceCategory
.. .. . .
.
..
14.2.7. PreferenceScreen
.
. .
295
. 301
302
. 303
310
312
. . 315
316
. 317
...... .. ..
................ ......... .
........................
................... . .. . ................................................................
......................
.....
........................
15.
SQLite - ....................
15.1. SQLite
.
.
15.1.1. : SQLiteOpenHelper .
15.1.2. : SQLiteDatabase
.... . .. 323
.
.. ....................... ..........
...... ............
...............
323
323
325
V/11
.................. .......
...................................... .......................................
... .. ..............
........ .....................................................................
.. ....
1 6.1.
16.2.
16.3.
16.4.
16. ,
.......................... 349
. ...
... ...
...
. .
. .
. . ... .
. . .. . .
..
..
........ ...
... .
..
. .. .
... .
..
......... ..... . .
.... .
..
.. .
.. ..
.....................................................
............... ......................................
................ ..............................................................
.....................................................................................................................
... ....
.. . . . .. .
.............
....
..
....
.........
. . ...
....
....................
...
............ .....
..... . . . ...........
17.1. Drawa!e .
. .. . . . . .
.. .. . . .
. .
.. .
. .
381
17.2. Drawa!e n. ..
.
.
. . 383
17.2.1. TransitionDrawa!e .... . .. . .. ... . .. .. . . . ... .
. .
384
17.2.2. ShapeDrawa!e.................................................................................. 386
17.3. .... . .. . . . ... .. ... . . .. .
. ..
. .. ... ...
393
. ..... .
.. ..... .. . . ......
....
. ..
. . . ..
.. . .... ..
. .
..
.... .. .
...
...
... .. . .
.....
....
............... .
.........
18. ......................................................................... 4 01
..
. ... .
. . .
. ........
.... .. ......... . . .. . . .
..
.....
....... . ....
......... .
....
..
...
. . .
...
........
.....
.......................... ... ..
. . . .......
. .
. . .
. ...
... .. .
IX
<alpha> . . .
.... .
.
<scale> ... .
. .
.
<translate> .
.
<rotate> .. . . . . .. . ... .
18.1.2. n
18.1.3.
18.1.4.
18.2. .
.
.
18.2.1. XML
18.2.2. n
. .. .... . . .
404
404
405
405
405
413
. 417
421
422
425
. . .. . ...
. ..
.
. ...
.......... . .
. . ....
..
..... .
........ . . ............ . . ..
. - .............. 429
n-
........................................................................................................
429
429
Goog\e Android
.
,
, Android.
Android?
Goog\e ,
,
SDK,
. Android
.
- Google
Adroid . Android
, .
Google Android ,
.
Goog\e Android
2008 - - Android Market,
. ,
Android Market,
.
Android, -
Java,
Android SDK.
, Java,
- ( C#.NET),
Android . Android
I-, Android SDK. ,
Java
Androi d .
Java ,
Android.
:
D ,
Java C#.NET
Android;
D ,
iPhone, W indows Moile, Symian JavaME,
Android.
, ,
Android. ,
, .
,
. , ,
.
1 8 .
.
D 1. Android
Android.
Android, ,
Android SDK.
Android, Dalvik V irtual Mach ine.
D 2.
, Android: Java
n n n Android, n
n .
n, n
Ecl ipse.
4. drid-
ndrid
- , ,
-n.
ndrid-n ,
n .
5.
Android
n n
- pynnax n .
n
n,
)
.
6.
( ). Android - n,
, , , ,
. )
Android.
7. -
-n,
,
, n - -
n
.
D 8.
.
,
.
, ,
.
. -
.
D 9.
Android.
, ,
.
.
D 1 .
Android SDK .
,
Android, , ,
, .
D 11.
-
.
ndrid- .
.
And
roid - . -
.
Android
, .
D 12.
Android. -
,
.
.
D 1 3.
(Broadcast Receiver). -
, ,
,
.
1 4.
-
,
- .
15. SQLite -
-
.
, Android, - SQLite.
-
.
, - , ,
(
) .
16. ,
. -
ndrid-.
, , , , , L
. .
ndrid-
.
, .
17.
ndrid- .
, L-
.
I-
.
1 8.
Android
Android SDK,
.
,
. .
Android SDK 2.0
Ecl ipse.
2.
,
.
, ,
.
- , , ,
, .
.
" -",
.
An d roi d
Android,
.
Android - ,
,
(middleware),
(-mil- , , , , .).
Adroid :
D
D ;
D ;
D
. 1 . 1 Adroid
.
1 . 1 .
.
,
.
Android Linux 2 .6, Android
Linu- ,
, Android, -
, .
11
.
11
l 1'
'- ------'
Dalvik
. 1.1. Android
:
D (IPC Driver);
D (Android Power Management);
D ,
.
Android.
1.1.1. IPC
,
.
Android I P C (Inter-process Communi
cation),
.
Android
IPC n :
n;
n n n;
n n;
n n .
1.1.2.
n n (Android Power Management)
n n Linux, n
n
.
n n -
n . n
"n " n n,
n n.
1.1.3.
An.droid ,
n n ,
. n n n
n n nn .
n n
( , , GPS, . .).
n
.
n . n
n n ,
n Linux n
n , Linux-cce.
n Linux Android ,
n n
, n . ,
n n , n n
,
n , n , n
.
10
1.2.
Linux /++
OpenGL, WebKit, FreeType, S S L, libc,
SQLite (Media Framework).
Berkeley Software Distribution (BSD) '
Linux.
Linux /++,
.
Appl ication
Framework - .
:
/++.
1 . 2.1. l ibc
Google - ( l ibc) - Bioic.
:
, ,
. Bionic
200 , gl ibc;
.
,
.
Bionic Android
. Bion ic
, ++,
GNU l ibc POSIX.
1.2.2 .
Android ,
Compiz ( W indow System,
3 D- OpenGL).
,
, ,
, .
,
.
A[ldroid
11
. 2D- 3 D-
.
I-.
, .
2D-, Khronos.
. 1 .2 .
Surface
Surface
Surface
Fame
Flinger
Buffer
Surface
. 1 . 2.
1 . 2.3.
Android /++,
.
.
D
(Media Framework).
- .
PacketVideo OpenCORE
- (MPEG4, .264, 3 .).
D SQLite - , . SQLite
-, . . SQLite
, ,
, ,
. ,
(API) SQLite.
,
. SQLite
( , , )
,
.
D OpenGL ES - 3 D-, I
OpenGL ES 1 .0. OpenGL ES -
OpenGL,
. -
12
3 D- ( )
.
D FreeType - ,
.
D WebKit - ,
Android W-. Webit CSS, JavaScript, DOM,
Ajax.
D SGL - 20-. Android
20- Skia,
++ (Skia Google Chrome).
D S S L - ,
SSL: S S Lv .O TLSv 1 .2.
1 . 3 .
Dalvik V irtual Mach ine
( Dalvik),
Jv- .
Android
13
,
.
1 . 4 .
,
Dalvik V M .
Android ,
, . .
\,
.
.
, ,
( ).
.
Android - ,
, . :
D (Activity Manager) -
( );
D (Package Manager) -
, ;
D (Window Manager) - .
,
,
. , ,
<Back>
, W-;
D ( Resource Manager) -
, ;
D - (Content Providers) - !,
n ,
;
14
(View System) -
, ,
, , , ,
, W- ;
- API,
,
,
;
- ,
;
-
.
1 . 5.
Android
, , SMS, ,
, , .
, Android
-
, ,
, .
Android Java.
.
, ,
.
.
Android,
. Java Development Kit,
Ecl ipse, Android SDK Android Development Too\s,
Ecl ipse Android.
2 .1 .
Google
(Software Development it), 86-
Windows , OS Linux.
2. 1 . 1 .
ndrid
, .
:
D Windows (86) Vista (32 64 );
D OS 1 0.4.8 ;
D Linux ( Linux Ubuntu Hardy Heron).
Ecl ipse 3 .5
Ecl ipse 3 .4 (Ganymede) :
D Ecl ipse IDE for J ava Developers;
D Ecl ipse I D E for J ava Developers;
D Ecl ipse for RCP/Piug-in Developers.
(Gal ileo)
16
NetBeans I D E , ,
.
:
Ll J DK 5 6 ;
Ll Ecl ipse I DE;
Ll Android SDK 2.0;
Ll Android Development Tools (ADT).
M i crosoft Windows
2. 1 .2. J D K
n Java
.
, ,
Sun Microsystems.
Java Runtime Environment
( Java, JRE).
- JDK (Java Development
it). J ava Development K it -
Java, Java Uavac),
Java, , ,
Java Runtime Environment (JRE). Java Development Kit
Sun M icrosystems
http://java.sun.com/javase/downloads/index.j sp.
JDK 6 U pdate 1 7
JDK 6 U pdate 1 7
with J ava .
17
JDK ,
.
JDK Java
(IDE), Eclipse.
2. 1 .3. Ec l i pse
- Ecl ipse.
Ecl ipse http://www.eclipse.org/downloads/.
Ecl i pse,
. 2. 1. 1 ( Ecl i pse IDE for Java
Developers).
Ecl ipse,
ecl i pse.exe. Ecl ipse
( M S Windows), Program Fi les
.
IDE,
, Ecl ipse ndrid
:
D Ecl ipse - ,
Java. Eclipse
- . Ecl ipse
IDE Android;
D Google Android Eclipse
Android Development Tools, Android-poe,
Android-ycpoca
. Android Development Tools
Ecl i pse Android
poea .
2. 1 .4. A n d roid S D K
Android, Android SD. SDK
,
Android, Android.
- Android SDK v2 .0. Android SDK
Android
http://developer.android.com/sdk/index.html.
.
2.0 Android SDK .
18
SDK
Android. 2.0 Android SDK and VD
Manager, SDK -
, , .
,
Android, Android SDK and VD Manager.
, . .
SDK Google.
Android SDK and V D Manager, n SDK
Setup.exe SD. n
n n n, n
. 2 . 1 .
revi 10n 2
. 2 . 1 .
n, n Install Accepted ,
, n SDK.
Android SDK n D-n Ecl ipse.
19
. 2.2. n ADT
Eclipse.
Ec l ipse Android SDK.
Ec li pse Window 1 Preferences,
Preferences. And roid . SDK
Location ,
Android SDK. Browse
Android S DK, . 2 . 3 .
Apply, . IDE Ecl ipse ""
Android SDK,
Android.
D- Ecl ipse
Android,
Ecl ipse, ,
.
20
Build
DDMS
Launch
LogCat
Usage Stats
fii
Android 1 . 1
Android 1 . 5
Android 1 . 6
Android 2 . 0
Google AP!s
Google APis
Google APis
Ant
fii
'*
Help
Instaii/Update
Java
Java
Android Open
Android Open
Android Open
Android Open
Google Inc .
Google Inc.
Source
Source
Source
Source
Project
Project
Project
Project
Google Inc.
1 .5
1.6
2.0
1 .5
1.6
2.0
4
5
Plug-in Development
fii
'J
Run/Debug
Server
fii Tasks
rti
Team
Terminal
r1:i
fii Web
r1:i
;tJ
fii
Web Services
XDoclet
XML
D- Ecl ipse :
L] Android - New Proj ect Wizard,
Android ;
L] Layout Editor -
;
L] , -
L- .
D- Android
Ecl i pse. , ADT
, DDM S (Dalvik Debug
Monitor Service) - SDK ,
(breakpoints),
Ecl ipse.
.
21
2 . 2 . A n d ro i d S D K
Android S DK ,
,
Android.
D API Android SDK - I- Android,
.
D SDK - ,
,
.
D AVD (Android V i rtual Device) -
Android. ,
Android-ycpoca.
D Development Tools - SDK
,
.
D Sample Code - Android SDK ,
Android,
, ,
API .
22
API, Adroid,
,
drid-.
7 API.
. 2 . 1 API Adroid.
2. 1 . /
API
A n d ro i d 2 . 1
And ro i d 2 . 0 . 1
Android 2 . 0
A n d ro i d 1 . 6
A n d ro i d 1 . 5
A n d ro i d 1 . 1
Android 1 . 0
2 . 2 . 2 .
, SDK ,
.
Adroid I DE Ecl i pse,
, S DK,
. S DK
:
D
adroid - ,
, ,
, drid- (
Ecl ipse) Adroid SDK ,
;
23
Hierarchy V i ewer - ,
. ,
;
Layoutopt - ,
.
, ;
Draw 9-patcl1 - ,
N i tl1-
;
sq l ite - SQLite,
Android;
Tracev iew -
, ;
mksdcard - ,
(, SD).
, ,
.
- ,
S DK ,
.
24
SD Card - SD;
LJ Skin - .
, -
25
.
SDK, Target,
, :
. 2.5. AVD
26
Hardware - , .
New
n (. 2.6).
. 2 . 6 .
n C reate AVD ( . . 2 . 5 )
, API
List of existing And roid Virtual Devices
( . . 2 .4).
a n d oid . exe . , AVD
g, ,
. , -
.
API
. 1 . 5 ,
. 2 . 7, 1 .6, 2 . 0, 2. 1 ,
. 2 . 8 .
.
. 2 . 8 .
Home screen - Android.
.
-
.
,
( - ).
.
27
. 2 . 7 . AVD 1 . 5
. 2 . 8 . AVD 2 . 0
28
2
SI
<Cti>+ < F 1 1 >
.
- Status Bar.
: ,
. Status Bar
( , )
,
,
, . Status Bar
,
.
- Application Launcher (. 2.9).
.
, , ,
:
.
;
29
D USB;
D ( );
D ;
D ;
D ;
D
SD;
D 8 \uetooth .
, ,
VD
.
And roi d
,
,
, "Hel lo,
Android ! "
3 . 1 . Ecl i pse
Android ,
Ecl i pse. Ecl ipse
File 1 New 1 And roid . And roid Project
Next (. 3 . 1 ).
New And roid Proj ect (. 3 .2).
:
Application name - He l l o ,
Package name -
And ro i d !
Samp l e ;
s amp l e s . he l l oandroid;
1 . s;
.
Project name - Ec l ipse - ,
Application name - - ,
.
32
Select
wizard
& Class
fi Interface
Java Pro}ect
!1
;j;j
ill3 dd'I
Jci1 Android Test Project
. Android XML File
. 3.1 .
Package name - ,
, J ava.
, , Ac t ivi t y,
.
,
Adoid.
.
C reate Activity - ,
. Ac t i v i t y .
,
, Ac t i v i t y .
Ac t iv i t y 4,
ndrid-.
Use default location - ,
.
Min SDK Version - A P I ,
33
Android
1 .5
1 .6
1.6
2.0
2.0
2.0. 1
Build Target -
API ( ,
SDK Min SDK Version).
API
.
A P I . AP I , , AP I ,
, .
34
B u i l d Target A n d roid 1 . 5
( M i n S D K Vers i o n - 3) . ,
n Android 1 . 5 . A n d roid
, , A P I 1 . 5 ,
1 .6 , 2 . 0 , 2 . 0 1 2 . 1 .
Run . Run As
. Select way to run 'HelloAndroid' Android
Application (. 3 .3 ) .
2_elect
i!tiiID
gnd
_r
Java pplet
Java pplicati
,Tti JUnit Test
Desription
. 3 . 3 .
D- Ecl i pse
,
.
35
Android
, MENU.
(. 3 .4 )
.
3 .2 .
D-
, Java. Ecl ipse, Package
Explorer, (. 3 . 5).
AP I , . , 7 ( Adroid
2 . 0 ) res/ d rawa e/ : d rawa e
h d p i/, d rawae-m d p i/, d rawae-l d p i/
.
36
src
com. samples.helloandroid
lfi.. 0 Hellondroidctlvity. java
!!:
com.samples. helloandrord
ilJ R . java
:iJ W. ndroid 1 .5
. ... assets
res
::.0 2& drw
.
iJ icon. pnQ
lti
!i:il 2&
layout
8 (i&,
values
i)t
main.xml
LJt strings.xml
<Qi ndroidManifest . xml
[@ default.properties
bin/.
Jv-
apk.
.
.
, .
3 . 2. 1 .
: , , .
ADT- , ,
.
:
D res/drawaJe/ - (PNG, JPEG . .);
D res/layout/ - L- ( );
D res/menu/ - L- ;
D res/values/ - , . .;
D res/xm l/ - L-,
.
. -, Android
res/. ,
n nn L-
( res/).
Android
37
res/layo ut/
XML,
.
TextView ( 3 . 1 )
.
- 6 7.
5,
res/va l u es/
: ,
, , . . ,
"Hello, Android ! " ,
:
D ;
D strings.xml he l l o " Hel lo, Android ! ",
android : t ext TextView
strigs.xm l , 3 . 1 :
a n d r o i d : t e x t = " @ s t r i n g / he l l o "
38
3 .2 .
<resources>
< s t ring narne= " he l l o " >He l l o , Android ! < / s t ring>
< s t r ing narne=" app_narne " > "He l l o , Android ! " Sarnple< / s t r ing>
< / res ource s >
3.2.2. R.java
, R
R.j ava.
, res/.
R.j ava 3 . 3 .
AUTO-GENERATED FILE .
DO NOT MODI FY .
It
39
Adroid
pul i c s t a t i c f i n a l c l a s s s t r ing
pu l i c s t a t i c final int app_name=Ox7 f 0 4 0 0 0 l ;
pu l i c s ta t i c final int hel lo=Ox7 f 0 4 0 0 0 0 ;
R ,
res/:
Ll
d rawale -
Ll
l a yout - res/layout/.
. - main.xm l ,
.
,
;
Ll
id - ,
main .xm l ;
Ll
s t r ing -
res/drawa\e/;
strings.xm l .
R,
, . .
.
He l l oAndroidAc t i vi t y -
onc reate ( ) ,
. ,
n .
He l l oAndroidAct ivity,
3 .4 .
Ac t ivity,
40
pu i c class He l l oAndroidActivity extends Act ivity
/ * * Ca l l ed when the activity i s first c reated .
@ Ove r ride
pu i c void onCreate ( Bundle s avedins tance S tate )
s upe r . onCreate ( savedinstanceState ) ;
set ContentView ( R . l a yout . main ) ;
,
, Ac t ivity.
,
.
AndroidMan i fest.xml .
: , ,
( Android,
), , .
3 . 5 .
<mani fest xmlns : android= " http : / / s chema s . android . com / apk / res / android"
android : ve r s i onCode= " l "
android : ve r s i onName= " l . O "
package= "com . s amples . he l l oandro i d " >
<app l i cation
android : i con= " @ drawal e / icon "
android : labe l= " @ s t ring / app name " >
_
<act ivi t y
android : name= " . He l loAndroidAct ivi ty"
android : l abe l= " @ s t ring / app_name " >
< intent - f i l ter>
<act ion android : name=" android . intent . ac t i on . AIN "
<category android : name= " android . intent . category . LAUNCER" / >
< / intent- f i l ter>
< / a c t ivity>
Android
41
Act ivity,
.
, ndrid-
.
nd r i d -
Android ,
, .
Android ,
( ,
).
,
.
,
, .
, ,
Android ,
, , main ( ) -
. ndr id- ,
.
ndrid- :
D (Activity);
D (Service );
D
(Broadcast Receiver);
D - (Content Provider).
4. 1 .
- . ,
, , . -
44
,
,
, -
.
. n n n
n . n
n.
n
. Android n n
n - n, n, , n ,
. . n
5, 6 7.
n Act ivity (
n n n ,
He l loAndroidAct i vi t y) .
.
. n
n,
(
1 1) .
4. 2 .
n n
n n .
, n .
n ,
n, n .
, , n
.
4. 3 .
-
. n
n . n
, ,
.
Android-npuoe u
45
. ,
, ,
.
4.4. -
- ,
, .
, SQLite
. -
. ,
-,
-
.
4. 5 .
( )
, Android ,
.
.
,
.
.
. ,
,
, . ,
( , )
, . .
. .
4.5 . 1 .
Adroid
, .
, , .
.
Android
, . . ' -
46
,
, , ,
.
, ,
.
, .
4. 5.2.
, ,
. Android
, , ,
, .
, , Android
, ,
(. 4. 1 ).
. 4 . 1 .
.
.
.
D
(Foreground Process) - ,
, .
, :
,
;
, ,
;
did-
47
S e rvice,
, ;
. -
, .
(Visiie Process) -
,
.
. , , ,
:
, ,
. , ,
, , ,
;
, ,
(
).
,
.
(Service Process) -
,
. ,
, , ,
,
.
(Background Process) -
, .
,
, .
, LRU ( least recently used, "
"), ,
, ,
.
(Empty Process) -
-.
. -
48
, .
.
, Android
.
, ,
, .
,
. , ,
, , .
, -
,
, .
, ,
,
(, ),
, ,
. ,
,
, ' ,
( ) .
,
.
4 . 6 .
- , Android
, , ,
, ,
.
Android ,
. Android
.
,
, (, )
, Android
.
Android-npuoe u
49
, , Android
,
.
4.6. 1 .
,
,
.
- Intent,
. , ,
.
Intent .
- ,
.
4.6 . 2 .
, , ,
. , - ,
,
,
.
, ,
.
,
.
. Android
.
,
Android
.
4. 7 . A n d ro i d M a n ifest.xm l
Android , ,
.
AndroidManifest.xml,
50
.
AndroidMan ifest.xm l .
:
Jv- .
;
D - , ,
-,
. ,
, .
Android ,
;
D ,
API ;
"'Hellodroid nifest
. q;: The appl1catn tag descries appcationlevel components contained 1n the package, as we" as gener 1 apphcation attr1utes.
?.] Define an <applicatlon> tag 11 the AndroidManifest.xml
1\.ppUt:t:it AttribtE':
lerow . . 1 Per
1 rows . . . .J n
...................] ( rowse j Debugt;)o'ble
' '
Theme
rmiss1
Has code
Appltr..aHon Node
@ @ @ @ ( @ @ :
od.itent.
. 4. 2 .
drid-
51
, ,
;
API Android, ;
, : .
, L-
.
Ecl i pse - Manifest Editor (
),
, . 4 . 2 .
4.7. 1 .
ndrid-,
.
n ,
,
.
. 4.3 ,
, .
<ma ifest>
1
<uses-perm issio >
1
< permissio -tree>
1
<permissio-group> 1
<i stru me tatio>
1 }
<uses-sdk>
1 } API
<uses-cofiguratio > 1 }
<uses-feature>
1 }
<supports-scree > 1 }
<applicatio >
1 }
<permissio>
. 4.3.
52
, ,
. .
<app l i cat ion>
, .
<mani fe s t >, <app l i c a t i on> <us e s - sdk> .
.
<manifest>
<man i f e s t > Adroid
Ma i fest.xm l . Android Ecl ipse
:
<mani f e s t xmlns : andro i d= " ht tp : / / s chema s . andro id . com/ap k / res / androi d "
pac kage= " com . programmingandro i d . he l loandro i d "
andro i d : ve r s i onCode= " l "
andro i d : ve r s i onName= " l . 0 " >,
xmlns : andro i d -
package -
andro i d : ve r s ionCode -
andro i d : ve r s ionName - .
.
Android.
;
,
;
;
<permission>
<pe rmi s s ion> ,
. ,
.
(
, ,
) .
, Adroid ( andro i d . Man i f e s t .
pe rmi s s i on) ,
.
andro i d : name <pe rmi s s ion> :
pe rmi s s ion andro id : name= " com . s ample s . custom_permi s s ion "
Android-npuoe u
53
, :
andro id : label -
andro i d : de s c ription -
andro i d : i con -
, ;
;
, ;
<uses-permission>
<use s -permi s s i on> ,
. ,
.
-
andro i d : name .
,
<pe rmi s s i on> , ,
, :
andro i d : name = " andro i d . penni s s i on . CAERA "
android : name= " " andro i d . pe rmi s s i on . READ_CONTACT S "
<permission-tree>
<pe nni s s i on - t ree> .
, ,
.
<permission-group>
<pe nni s s i on-g roup>
.
<pe rmi s s i on> , .
, ,
.
, pe nni s s i onGroup
<pe rmi s s ion>.
<instrumentation>
< i n s t rument at i on> I n s t rumen t a t i on,
. -
54
rls- .
<uses-sdk>
<us e s - sdk>
( API) Adroid.
API, , API
, .
- rninSdkVe r s i on,
API, .
Adroid ,
, API , ,
. , :
a ndro i d : rninS dkVe r s i on= " "
<uses-configuration>
<us e s - feature> ,
. ,
,
.
, ,
. ,
.
<supports-screens>
drid-
55
A P I 4
.
4. 7 2 <application>
.
}
}
}
}
}
}
}
}
---1
<meta-data>
<service>
---1
---1
<intent-filter>
<meta-data>
<receiver>
---1
---1
-1
-1
1
<intent-filter>
<meta-data>
<provider>
-1
1
1
1
1
<grant-uri-permission>
<path-permission>
<uses-libary>
<meta-data>
1
1
1
}
}
}
}
n n
_
n
n n
"-"
n
-n
n
56
,
.
<app l i cat ion>
. 4.4.
<activity>
:
CJ
andro i d : narne -
.
, . .
<man i fe s t >, , ,
, :
CJ
and ro i d : l abe l -
, .
, < a c t i v i t y>
, , . .
,
, <act i v i t y > .
,
.
<intent-filter>
<action>
<act ion>
.
<ac t i on > .
drid-
57
<category>
<data>
<da t a > .
( mimeType ) , URI
URI. URI
, . . URI : andro id : s cheme, android : ho s t ,
andro i d : po r t , andro i d : path andro id : path Pre f ix, andro id : pathPa t t e rn .
<meta-data>
<activity-alias>
< a c t i vi t y- a l i a s > - ,
t a rgetAc t ivity.
, ,
.
.
,
,
.
,
andro id : name = " android . intent . a c t i on . AI N "
58
,
.
<service>
< s e rv i c e > .
< s e rvice>
. , ,
. ,
, , , . .
<receiver>
<provide r> -. -,
,
<provi der> . ,
, . . .
<provide r>
:
CJ
CJ
<path-permi s s i on>;
CJ
<meta-data>.
, , ,
, . .
<grant-uri-permission>
drid-
59
<path-permission>
<use s - l ibrary> ,
.
.
Adroid,
(
Ac t i vi t y, S e rvi ce, I ntent, View, But t on, App l i ca t i on,
Cont ent P rovider . .). (, maps awt )
,
.
, ,
<us e s - l ibrary>.
ndrid-
View () Vi ewG roup (
). view V i ewG roup
View (. 5 . 1 ) .
, v i ew.
. 5. 1 . G V i ew Vi ewGroup
View -
Android. View ,
,
, . .
View - ,
.
View , , ,
, ,
. , view
.
V i ewGroup ,
(layouts),
,
, , .
62
5 .1 .
Android
, V i e w V i ewG roup,
. 5 .2 . ,
-
.
View
View
View
View
View
View
View
. 5.2.
Adroid
.
, View Vi ewG roup
.
5.2 .
, , -
,
. n
, n .
:
XML. Adroid
L-, View
V i ewG roup;
63
Adroid
. ,
XML, ,
, , ,
,
XML.
D- Ecl ipse -
Layout Ed itor (. 5 .3 ), ?\
,
res/\ayout/ .
Pfopertles
. 11 i
([IDralerFrer
!I) ExpandableL.stV
[) Fr.nelayout
([]Gdlr'rew
(ii] Hootont:aiScro/1
ImaqeSwltchr
...
: :: But.ton
Auto lrnk
BackQround
Buffertype
Cllck.,ie
Cursor v1srbl
Drae bottom
Dra1aeleft
Drawable paddrnq
Drawaie r'Qht
...
Drawaie top
{jJLnearLo!Jyout
ERJI)srze
Em<
Surfao:elr'tew
FadtngedQe
Fdlng e1ge length
2) V.ew
(!}VIewStu
,,.i AnalogCiock
@ AutoComplete . . .
Focusable
Focusdie n touch r
Freezes text
@utton
Gravll:y
Haptrcfeedbackf'nc
HerQht
Hrnt
(Q Checkox
- L- . X M L
, L- W- .
L-
,
, .
, ,
.
64
, L-
(portrait, landscape),
. , L-
, .
XML View ViewGroup (
). View - , ViewGroup -
(. . 5 .3 , Outline). View
Vi ewGroups Jv-, addView (View ) ,
View Vi ewGroup .
,
.
, Android (
), Linea rLayout, Re lat iveLayout, TaleLayout . (
).
, ,
.
5.2 . 1 . XM L
L- Android,
, ,
, W- HTML
.
,
View ViewGroup.
,
,
, .
,
. , L- " Hel lo,
Android ! " 3 ( 5 . 1 ).
65
<TextView
andro id : id= " @ +id / TextViewO l "
android : layout width= " f i l l pa rent "
_
_
android : layout he i ght= " wrap content "
_
_
android : t ext=" @ s t ring / he l l o " >
< / TextView>
< / LinearLayout >
L- : - L
, View (
View - TextView) .
, Vi ew, L-,
.
, ,
.
5.2.2. L-
L- <Linea rLa yout >
View . TextView ( ),
. , , . 5 . 1 .
5. 1 .
L-
XML
xmlns : android
android : l ayout_width
,
V i ew ( Vi ewGroup) . - ,
, , f i l l_parent
android : l ayout_he i ght
android : l ayout_width,
android : t ext
, Textv iew.
. he l l o es/values/sti ng s.xm l .
n
n , n , n
66
5. 2.
L-
X M L
Java
android : gravi t y
setGravi t y ( int )
setHeight ( i nt )
android : t ext
setText ( CharSequence )
android : t extCo l o r
setTextCol or ( ColorStateLi st )
android : t ext S i z e
setextS i z e ( f l oat )
andro i d : width
setWidth ( i nt )
s e t H i ghl i ghtColor ( i nt )
View
,
. ,
,
L- id.
L- :
and roid : id= " @ + i d /TextViewO 1 " ,
67
@ ,
XML
.
+ , - ,
R.j ava, Adroid
, 5 .2.
pu l i c final class R
pul i c static final class id
pu i c static fina l int TextView01=0x7 f 0 5 0 0 0 0 ;
, ,
Re lat iveLayout ( ). Re lat iveLayout
, :
andro id : l ayout_toLe ftOf= " @ id/ TextVi ewO l "
. 5. 4. 4.
,
( ,
, ).
5 . 3 .
,
.
onC reate ( ) s e tContentView ( ) ,
:
R . layout . layout_fil e_name
, L- mai .xm l,
Act ivit y ( 5 .3).
68
@ Overr ide
pu i c vo id onCreate ( Bundle savedins tanceState )
super . onCreate ( savedins tance Stat e ) ;
setContentView ( R . layout . ma in ) ;
.
.
, ,
, - . .
.
5 .4.
,
:
r:J FrameLayout ;
r:J L inea rLayout ;
r:J Tae Layout;
r:J Re lati veLayout .
,
, View.
ViewGroup
(. 5 .4)
Framelayout
Relativelayout
-, -Ta-b-le-La_y_ou-t'l
. 5.4.
5.4. 1 . FrameLayout
FrameLayout .
, -
69
View viewGroup.
FrameLayout .
" ", Ecl ipse
New Android Project :
Project name -
FrameLayoutApp;
g Application name -
main.xml , 5 .4.
, Layout Editor, , L-,
.
.
, . 5 . 5 .
Frame Layout
FrameLayout
View. View
',
, ,
FrameLayout
:
android : l ayout_width= " fi l l_pa rent "
android : l ayout_he i ght= " fi l l_pa rent "
70
5.4.2. LinearLayout
L i n e a r L a yout n
n - ,
, n and r o i d : o r i e nt a t i o n :
and r o i d : o r ient a t i on= " ho r i zont a l "
a n dr o i d : o r i en t a t i o n = " v e rt i ca l "
n n ,
n n
, .
n n ,
n .
n n
New Android Proj ect :
Package name -
S amp l e ;
. s amp l e s . l i ne a r l ayou t ;
Act i v i t y .
71
res/layout/mai n .xml
, 5 . 5 .
Linea rLayout
, andro id : l ayout_width
wrap_cont ent, - fi l l_parent, . .
.
. andro
id : layout_height:
andro id : o rientat ion= " vert ical ",
.
Linea rLayout
. 5 .6.
Linea rLayout andr <;> id : l ayout_we ight,
. -
72
n "" n n
, n n
. .
. 5.6. L i n e a r L a yout
, n,
t:, 1 , (0),
n , n
, . ,
, n.
n 2 ( 0), "
", , n 5 0% n
, 25% n.
n n Linea rLayout
" " n
New Android Proj ect :
Package name -
. sampl e s . t a e l ayou t ;
73
. 5 .7 .
, X M L
.
l ayout_we ight , ,
.
74
5.4.3. Ta/eLayout
ale Layout n
. al e L a yout ,
. a e Layout .
n
n .
n TaleRow,
TaleLayout ( Ta leRow n
).
, V i ew
V i ewG roup. V i e wG r oup (n,
n Tale Layout Linea rLayout ).
n New And roid Project
:
D
S amp l e ;
75
aleLayout
, .
Tal eRow
Layout width -
Gravity - center.
f i l l_parent;
g ravity ,
- .
Properties :
, ( 1 , 2, 3, 4, 5, 6,
7, 8, 9, * , , # ) .
, . 5 . 8 .
);>
+ - ' t
aieLvutoi <elayut)
;:. [) TaieRowO I (TaieRow)
...
Button O I (Button)
Button02 (Button)
ButtonO (Button)
[) TaieRow02 (TaieRow)
Button04 (Button)
Button05 (Button)
ButtonO (Button)
[) TaieRowO (TaieRow)
Button07 (Button)
Button08 (Button)
Button09 (Button)
[) TaieRow04 (TaieRow)
Button ! O (Button)
Button l l (Button)
Button 1 2 (Button)
. 5.8. TaleLayout
, 5 . 7 .
76
5
android : layout_width= " f i l l_pa rent "
android : layout_he ight= " fi l l_parent " >
<Tal eRow
android : id= " @ + id/TaleRowO l "
android : layout_he ight= "wrap_content "
android : layout_width= " fi l l_pa rent "
android : gravity= " cent e r " >
<Button
androi d : i d= " @ +id/ButtonO l "
android : l ayout_he i ght="wrap_content "
androi d : text= " l "
android : l ayout_width= " 2 0pt " / >
<Button
android : id= " @ +id/Button0 2 "
android : layout_he ight= "wrap_content "
androi d : text= " 2 "
androi d : layout_width= " 2 0pt " / >
<Button
androi d : i d= " @ + i d/ButtonO "
android : l ayout_he ight=" wrap_content "
androi d : text= " "
androi d : l ayout_width= " 2 0pt " / >
< /TaleRow>
<Tal eRow
android : id= " @ + id/TaleRow0 2 "
androi d : l ayout_he i ght= "w rap_content "
androi d : l ayout_width= " fi l l_parent "
androi d : gravi ty= " cente r " >
<But ton
android : id= " @ + id/Button0 4 "
androi d : l ayout_he i ght = "wrap_content "
android : l ayout_width= " 2 0pt "
android : text= " 4 " / >
<Button
android : id= " @ + i d/Button0 5 "
android : l ayout_he ight=" wrap_content "
android : l ayout_width= " 2 0pt "
androi d : text= " S " / >
<But ton
androi d : i d= " @ + i d/Button0 6 "
android : l ayout_he i ght=" wrap_content "
android : l ayout_width= " 2 0pt "
android : text= " " / >
< /Tal eRow>
<Tal eRow
android : id= " @ + id/TaleRowO "
android : layout_he ight="wrap_content "
android : layout_width= " fi l l_parent "
android : gravi ty= " cent e r " >
<Button
android : id= " @ + i d/Button0 7 "
android : layout_he ight="wrap_content "
android : layout_width= " 2 0pt "
android : text= " 7 " / >
<Button
<Button
android : id= " @ +id/Button 0 9 "
android : l ayout_he i ght="wrap_content "
android : l ayout_width= " 2 0pt "
android : text= " 9 " / >
< /TaleRow>
<Tal eRow
android : id= " @ + id/TaleRow0 4 "
android : l ayout_he ight= "wrap_content "
android : l ayout_width= " fi l l_parent "
android : gravi ty= " cente r "
<Button
android : id= " @ +id/But ton l O "
android : l ayout_he ight= "wrap_content "
android : l ayout_width= " 2 0pt "
android : t ext= " * " / >
<Button
android : id= " @ + id/Buttonl l "
android : l ayout_he ight=" wrap_content "
android : l ayout_width= " 2 0pt "
android : text= " O " / >
<Button
android : id= " @ + id/Button l 2 "
android : l ayout_he i ght="wrap_content "
android : l ayout_width= " 2 0pt "
android : text= " # " / >
< /TaleRow>
</Tal eLayout>
77
78
.
Ta l e Layout ,
. 5 .9 .
5.4.4. RelativeLayout
( )
( ).
Re l a t i veLa yout
Re l a t ive Layout ,
, ,
, .
, L-, ,
, , ,
.
New Android Proj ect
:
D
S amp l e ;
79
5 . 8 .
5
android : text=" Right "
androi d : l ayout_a l i gnPa rentRight = " t rue "
android : l ayout_centerVert i ca l = " t rue " / >
<But ton
andro id : id= " @ + id/button_re l right "
android : layout_width= " wrap_content "
android : l ayout_he i ght= "wrap_conten t "
android : layout_toLe ftOf= " @ id/button_right "
android : l ayout_a l i gnTop= " @ id/button_right "
android : text= "RelRight " / >
<Button
android : id= " @ + id/button rel left "
andro id : l ayout_width= "wrap_content "
andro i d : layout_he i ght= "wrap_content "
android : l ayout_toRightOf= " @ id/button_left "
android : l ayout_a l i gnTop= " @ id/button_l e f t "
android : text= "Re l Le ft " / >
0
,
( , layout_toLe ftOf ) ,
@ id/ id.
. 5. 1 0 . Re l a t i veLayout
81
.
Re lat i veLayout , . 5 . 1 .
5 . 5 .
H i era rchy Viewe r
Android S DK Hierarchy Viewer -
(. 5 . 1 1 ) .
.
Hierarcl1y V iewer
- Pixel Perfect V iew.
F ifo V lew
re;hv
Ser'Ver
tiY'9UNd-l
. . . ..
20%
. 5 . 1 1 . H i e ra rchy Viewe r
Hierarchy Viewer :
D
82
Refresh
Windows , .
5 . 5. 1 . Layout View
Layout View .
:
Tree View - ;
Properties View - ;
Wire-frame View - .
Setv'
............. ----
----'ile!WiietW'Iy \1
'"''" ]
--
::---'
:dl
l
Ydoto Rst\Ayout
ritJa.l
od.'\Uiqoo_'ll !
. 5 . 1 2 Layout View
RelativeLayoutApp . 5. 5. 4.
83
, Properties View
Tree View.
Wire-frame View
.
.
Layout View
- Invalidate Req uest Layout.
inva l idate ( ) reques tLayout ( )
.
, Layout Vi ew
. Layout Vi ew , Load
View H iera rchy .
Explorer View - ;
Normal View - ;
Loupe View - n .
, (padding) (margin).
-
. n
n ,
(paddi g). n
n
(margi ).
n
n Normal
Views Loupe Views. n,
n, n
. n Load
n Normal Views, n .
nn .
84
.
-- - -'.<; .. .;.
ltd:e RaQUOstLayout
Llnearlayoul:
U Fra!:L.syout
Yextvtew
J Fr4tl'lel-wcut
: ";..,) Relatvelayct
uttcn
ul.tCil
Butta'l
utton
ul.ta'l
vt.tm
Elul.ta'l
Normal View Loupe Vi ews
(5 ) ,
Expl orer View .
Explorer View, Load
Vi ew H iera rc h y .
- V i ew,
. , -
. Adroid v.,
, ,
.
6 . 1 .
Adroid :
T e x t V i ew;
Edi t T e x t .
e x t v i ew
. ,
E d i t T ex t .
TextView E d i t T e x t ,
V i ew, .
. 6 . 1 .
TextView
Editext
. 6.1 .
86
6 . 1 . 1 . TextView
Textview -
. extView n
n .
n . ,
TextView n
-n .
, ,
.
android : text, :
TextView
setText ( ) :
TextView text
11
, L
XML-pecypc:
android : text= " @ s t r ing /text_he l l o " ,
text_he l l o -
XML-pecypc s etText ( ) ,
,
R.j ava ( ), :
TextView text
1/
extView L-
. , L-,
TextView:
D
android : text Si z e - .
:
(pixel s) - ;
87
i n (i nches) - , ;
pt (points) - 1 /72 , ;
m m ( m i l l imeters) - ,
.
sp, , :
android : text Si ze= " 4 B s p " ;
norma l ;
bold;
italic.
. :
.
:
androi d : textColor -
#RGB;
#ARGB;
#RRGGBB;
#AARRGGBB;
R, G,
,
(alpl-channel).
, , 1 00%.
, alpha, - .
-
TextView '
.
TextView
Create New Project :
Sample;
88
Linea rLayout
TextView text l , text 2 , text; text 4 . textl
L-:
android : text= " He l l o , Android ! "
text2 .
,
text text 4 . 6 . 1 .
< ? xml ve r s i on= " l . O " encoding= " ut f-8 " ? >
<Linea rLayout xmlns : android=" http : / / schemas . android . com/apk / re s / android"
android : id= " @ + id/Linea rLayout O l "
android : layout_width= " fi l l_pa rent "
android : layout_he ight= " fi l l_parent "
andro id : orientation= "vert i ca l " >
<TextView
android : id= " @ +id/text l "
android : l ayout_width= "wrap_content "
android : layout_he ight = "wrap_content "
android : text= "Hel l o , Android ! " />
<TextView
android : id= " @ +id/text 2 "
android : la yout_width= "wrap_content "
android : layout_he i ght = " wrap_content "
android : text= " @ s t ring / text_hel l o "
android : text S t yle= "bold" />
<TextView
androi d : id= " @ +id/text "
androi d : layout_width= "wrap_content "
android : la yout_height="wrap_content "
android : textS i z e= " sp"
android : textS t yle= "bold"
android : textColor= " #AAA " / >
<TextView
android : id= " @ +id/ t ext 4 "
android : layout_width= "wrap_content "
android : layout_he ight= "wrap_content "
89
android : text S i z e= " 4 8sp"
android : textS t yle= " i t a l i c " / >
</Linea rLayout>
stri ngs.xm l
"Hel lo, Android ! " ( 6 .2).
_n
1 1
90
. 6.2. TextView
. ,
. 6 . 2 . ,
- , - ,
- .
6 . 1 . 2 . EditText
Edi t e x t -
.
TextVi ew,
.
EditText
Edi t T e x t - g e t T e x t ( ) , ,
E d i t T ex t .
E d i t a l e . ,
(
S t r i ng, ,
s t r i ng ) .
:
D
D
s e l ectAl l
( ) - ;
s e t Se l e c t ion ( i n t
s t a rt ,
int
s top )
s t a r t s t op;
s e t S e l e c t ion ( i n t i ndex ) -
index.
91
extView.
setType face ( nul l , Type face ) , s etTextS i z e ( int text S i z e ) , SetTextColor ( int
Color ) .
Sample;
6.4.
H e l l o, Android!
. 6. 3 . Edi tText
92
<Edi tText
androi d : id= " @ + id/EditText O l "
android : la yout_he ight= " f i l l_pa rent "
android : la yout_width= " fi l l:_parent "
android : text= " He l l o , Android ! " />
< / LinearLayout>
.
extview . 6.3 .
Act i vi ty.
.
6 . 2 .
TextView
,
TextView (
, )
.
Android
scrol lView
Hor i z ont a l Scrol lView,
vi ewGroup, . 6.4.
View
ViewGroup
ScroiiView
HorizontaiScroiiView
. 6.4.
93
scrol lView, ,
,
scrol lView
Hor i z ont a l S c rol lView. Scrol lView
, Hor i z ont a l S c ro l l View - .
. Ecl ipse
Create New Project :
Sample;
main.xml ,
S c ro l lView, Hori zont a l S crol lView TextView, 6 . 5 .
94
S c ro l l ViewActi vi ty oncre a t e ( )
TextVi ew, L-,
s etText ( ) ,
.
S c r o l l Vi ewAct i vi ty 6.6.
,
u-
. 6. 5.
95
TextView text
1 1
.
extview . 6.5 .
6 . 3 .
Imageview.
, ImageView
, .
extView,
Imageview ,
- .
:
D set imageResource ( i nt res i d )
;
D set imageURI ( Uri uri )
- URI ;
L-
andro id : s rc .
,
getMinimunWidth ( ) ,
setScaleType ( ) .
ImageView
, setMaxWidth ( ) , getMinimunHei ght ( ) ,
- get Scal eType ( ) ,
setMaxHe ight ( )
ImageView
Create New Proj ect :
D
Application name -
C reate Activity -
ImageVi ewApp;
ImageView Sample;
res/drawa\e/ , android.png
adroidmarker.png ( CD-ROM).
96
,
ImageView image l , image 2 . .
android : s rc= " @ drawale / andro id" .
6. 7 .
ImageViewAc t ivity
ImageVi ew, s e t imageResource ( ) , 6 . 8 .
97
pu l i c vo id onCreate ( Bundle savedins tance State )
supe r . onCreate ( savedinstanceState ) ;
setContentView ( R . layout . mai n ) ;
final ImageView image
/ 1
. ,
. 6.6.
. 6.6 .
ImageView .
6 . 4 .
,
, , , .
, ,
.
View ,
on . . . Listener ( ) , -
98
.
. Android, Vi ew,
,
.
view :
OnC l i c kL i s tene r ;
OnLongCl i c k L i s tener;
OnFocusChangeL i s tener;
OnKeyL i s tene r;
OnTouchL i s tene r ;
, ,
,
OnC l i ckLi s t ene r onC l i c k ( ) ,
,
setOnC l i c kL i s tene r ( ) :
but ton l . s etOnC l i ckLi s t ene r ( new V i ew . OnC l i c kL i s tene r ( )
pu l i c vo i d onC l i c k ( V i ew v )
mText . s etText ( " C l i c k on Fi r s t Button" ) ;
,
.
6 . 5 .
Android :
Button;
Che c kBox;
ImageButton.
. Imageutt on,
Textvi ew,
99
ImageView
utton Textview.
CompoundButton.
CompoundBut t on
: Che c kBox, Toggl eButton
Radi out ton. . 6.7.
Object
-----,
View
-----,
TextView
,.--------,
Button
,---------,
'-------1 Com pou ndButton
l mageView
ToggleButton
-----,
'-------1 l mageButton
1
J
1
-------,
CheckBox
RadioB utton
. 6.7.
Button, Chec kBox, Toggl eButton, RadioButton ImageBut ton
Compoundutton
- checked unchec ked.
. compoundutton
onche c kedChangeListener
onChec kedChanged ( ) .
6.5. 1 . Button
utton -
.
onC l i c k .
onc l i c k ( ) .
, , .
Ecl i pse C reate New Project
:
D
Package name -
Samp l e ;
100
LinearLayout
buttonl button2 , " Button 1 " " Button 2"
( 6.9).
uttonActivity ,
6. 1 .
101
@ Ove r ride
pu l i c voi d oC reate ( Budle s avedistace State )
supe r . oCreate ( s avedistace State ) ;
s e tCotetView ( R . layout . ma i ) ;
mText
( Butto )
fidViewByi d ( R . id . buttol ) ;
pu l i c vo id oCl i c k ( Vi ew v )
mText . s etText ( " C l i c k Fi rst But to" ) ;
( Butto )
fidViewByi d ( R . id . butto 2 ) ;
pu l i c vo id oC l i c k (View v )
mText . s etText ( " C l i c k Secod But t o " ) ;
.
(. 6 . 8).
.
ocreate ( ) .
, ,
oCreate ( ) .
s e toc l i c k L i s teer ( )
, :
butto . s e tOCl i c kL i s tee r ( butto c l i c k ) ;
_
:
pu l i c OCl i c k L i s tee r but to c l i c k
_
@ Ove r ride
w OC l i ckListee r ( )
pu l i c void oC l i c k (View v )
1 1
1 02
. 6.8.
ut tonAc t i v i t y , 6 . 1 1 ,
. ,
.
@ Ove r r i de
pu l i c vo id onC reat e ( Bundle s ave dins tance S t a t e )
buttonl . s e tOnC l i c kL i s tene r ( buttonl c l i c k ) ;
button2 . setOnC l i c kL i s t ene r ( button2_c l i c k ) ;
@ Ove r r i de
pul i c void onC l i c k ( View v )
mText . s e tText ( " C l i c k on but tonO l " ) ;
1 03
pub l i c vo i d onC l i c k ( V i ew v )
mText . s etText ( " C l i c k o n but ton 0 2 " ) ;
, , , , -
.
View . OnC l i c kL i s tene r:
pu l i c c l a s s Edi tTextAct ivity ext ends Act i v i t y
impl ements OnC l i c kL i s t ene r
:
abs t ract vo i d onC l i c k ( View v ) ,
uttonAc t iv i t y .
(, ),
s w i t ch, I D
get r d ( ) :
@ Ove r r ide
pu l i c vo i d onC l i c k ( V i ew v )
switch
( v . get i d ( ) )
/ 1 ID
. C reate New Project
:
Application name -
, 6. 1 2 .
1 04
1 05
android : l ayout_width= " f i l l_parent "
android : text= " He l l o , Androi d " />
< / Ta leLayout>
Editext
( 6 . 1 3).
20;
{
s upe r . onCreate ( s avedinstanceState ) ;
s e t ContentView ( R . l a yout . ma in ) ;
mEdi t = { Edi tText ) f indV iewByid ( R . i d . edit_text ) ;
final Button buttonR
( Button ) f indVi ewByid ( R . i d . button_r ) ;
f inal Button buttonB
( Button ) f i ndViewByid ( R . i d . butt on_b ) ;
final Button buttoni
( Button ) f i ndVi ewByid ( R . i d . butt on_i ) ;
f ina l Button buttonPlus
( Button ) findV iewBy id ( R . i d . button_plus ) ;
f ina l Button buttoninus
( Button ) f i ndVi ewByid ( R . id . button_minus ) ;
1 06
but t onR . s etOnC l ickLi s t ene r ( thi s ) ;
but tonB . s etOnC l i c k L i s tene r ( thi s ) ;
but t on i . s e tOnC l i c k L i s t e ne r ( th i s ) ;
but tonPlus . s etOnC l i c kLi s t ene r ( th i s ) ;
but t oninu s . setOnC l i c kLi s tene r ( th i s ) ;
@ Ove r r i de
pu i c void onC l i c k ( V i ew v )
s w i t ch
( v . get i d ( ) )
c a s e R . i d . bu t t on r :
mEdi t . s e tType face ( nul l ,
bre a k ;
c a s e R . i d . button :
mEdi t . s etType face ( nu l l ,
brea k ;
c a s e R . i d . button i :
mEdit . setType fa ce ( nu l l ,
Typeface . I TAL I C ) ;
brea k ;
case R . id . bu t t on_plus :
if
( mText S i ze <= 7 2 )
mText S i ze+=2 ;
( mText S i z e >= 2 0 )
mText S i z e -=2 ;
.
,
(. 6.9).
107
He/lo, Android!
. 6.9.
. , , Red,
Green Blue, Red, Blue,
Red .
- t o g g l e ( ) ,
. ,
, i s Chec ked ( ) , ,
s e tchecked ( ) , .
Create New Proj ect :
D
Application n - RadioButton
S arnp l e ;
RadioG roup, TextView.
ID rad i o l , radi o 2 , radio Mode # 1 , Mode # 2 , Mode # 3 ,
6. 1 4 .
1 08
onC l i c k ( )
. Radi outtonActivity
6. 1 5 .
1 09
( RadioButton ) v ;
+ rb . getext ( ) ) ;
.
extVi ew,
radioBut ton_C l i c k. . 6 . 1 .
6.5.3. CheckBox
Chec kBox () - .
CompoundButton . OnChec kedChangeL i s t ene r .
1 10
che ckBox
C reate New Proj ect :
D
Sarnp l e ;
Act ivity.
Chec kBox 6 . 1 6 .
<Linea rLayout xmlns : andro i d= " http : / / s chema s . andro i d . com/ apk / re s / andro i d "
android : id= " @ + id / L inea rLayout O l "
andro i d : l ayout_width= " f i l l_pa rent "
andro i d : l a yout_he i ght= " f i l l_pa rent " >
<Che c kBox
android : id= " @ + id/ checkbo x "
android : la yout_wi dth= "wrap_content "
111
android : la yout he i ght= " wrap content "
_
android : text= " Chec kBox OFF" / >
< / LinearLayout >
CheckBoxAct ivity
( 6. 1 7).
1 1
mChe ckBox . s etOnChec kedChangeLi s tene r ( thi s ) ;
pul i c void onChe c kedChanged (
Compoundut ton but tonView , bool ean i s Chec ked )
if
( i s Che c ked )
mChec kBox . s etText ( " Chec kBox ON " ) ;
else
mChec kBox . s etText ( " Chec kBox OFF " ) ;
.
, .
. 6. 1 1 .
1 12
. 6 . 1 1 . Che c kBox
6. 5.4. ToggleButton
Toggl eButton - : " " "
" ON/OFF LD
, ON .
ToggleButton - android : textOff andro i d : textOn,
.
s e tTextO f f ( ) setTexton ( ) .
s e tChe c ked ( bool ean checked)
. Togg leBut ton -
onChe c kedChanged ( ) .
S arnple;
,
Toggl eButton extView (
6. 1 8).
1 13
CompoundBut ton . OnChec kedChange
L i s tene r .
onCheckedChanged ( ) ,
.
i s Checked.
6. 1 9.
1 14
@ Ove r r i de
pu i c void onC rea t e ( Bund l e s aved i n s t ance S t at e )
s upe r . onCreate ( s avedinstanc e S t a t e ) ;
s e t ContentView ( R . l a yout . ma i n ) ;
mut ton
@ Ove r ride
pu i c void onChec kedChanged (
Compoundutton but tonView,
if
( i s Chec ked )
mLabe l . s e tText ( " Button checked " ) ;
else
mLabe l . s e tText ( "Button unchecked " ) ;
.
OFF ON LD-,
. 6. 1 2 .
1 15
6.5.5. /mageButton
ImageButton (
). IrnageButton Button,
,
.
android : s rc
< IrnageButton>
s e t irnageResource ( int ) .
Project name -
Application name -
Package name -
C reate Activity -
ImageButtonApp;
IrnageButton Sarnple;
. sarnpl es . imagebutton;
IrnageButton 6.20.
res/drawa\e/
( r: CD-ROM
Resources/lmages/ - play.pg pause.pg).
Ima geButtonAct i vi ty
, 6.2 1 .
116
boolean mPlay
@ Ove rride
1 1 n
but ton . s e t imageRe source ( R . drawale . play) ;
but ton . setOnCl ickLis tener ( new \Tiew . OnCl i ckLis tene r ( )
pu l i c void onC l i c k (View v )
1 1
i f (mPlay)
button . s e t imageResource ( R . drawal e . pause ) ;
else
button . se t imageResource ( R . drawale . play) ;
mPlay
! mPlay ;
n n n. n
n, n . 6. 1 3 .
117
. 6. 1 3 . ImageBu t t on
6 . 6 .
Adroid
s t
TabWidget
( . 6. 1 4 ) .
Framelayout
Ta bHost
TabWidget
. 6 . 1 4.
s t
-. s t
abW i d g e t . ,
.
- s t . ,
s t .
.
1 18
s t
.
abHos t :
D s etup ( )
- .
, s t
findVi ewByi d ( ) ;
D addTab ( )
- ;
D s e tCurrentTab ( )
- .
,
,
.
TabWidget .
TabSpe c:
TabHos t . TabSpec spec
C reate New
Proj ect :
D
Proj ect
Package
TabHos tApp;
Sample;
- . samples . t abhos t ;
6.22.
1 19
aostActivi t y
s t .
6.23 .
tabs . s etup ( ) ;
TabHos t . TabSpec spec
1 20
6
spec=tabs . newTabSpec ( " tag2 " ) ;
spec . s etContent ( R . i d . tabPage2 ) ;
spec . s et indicator ( " Document 2 " ) ;
tabs . addTab ( spec ) ;
t abs . s etCurrentTab ( O ) ;
.
s t . 6. 1 5 .
Enter text. .
. 6. 1 5. TabHost
6 . 7 .
Android :
Cl Progre s s Bar;
Cl Rat ingBa r;
Cl Seekar.
. 6. 1 6.
Ra tingBa r ( ) SeekBa r ()
Progres sBa r As SeekBar. AsSeekBar
121
Rat ingBar SeekBar.
View
ProgressBar
Rati ng Bar
SeekBar
. 6. 1 6.
6. 7 1 ProgressBar
.
ProgressBar
. ,
Progres sBar:
D s e t Progre ss
( ) - ;
Cl get progre s s ( ) - ;
- ;
- .
.
Android andl e r
.
-
andl e r .
andle r, , ,
(, Progre s sar)
.
andler,
obtaine s s age ( ) , Mes s age
:
Handle r h ;
1 1
Mes sage msg
mandler . obta ines s age ( ) ;
=
1 1 Handl e r
h . s endes sage (ms g ) ;
1 22
:
s ende s s age
andler
( ) - n (
);
( ) - n
, , n (
),
n ;
- n
!\'I ;
- n n ,
.
, andle r
handl ee s s age ( ) ,
.
Handl er h ;
h
new Handle r ( )
@ Ove r ride
pul ic vo id handleMes s age ( Mes sage rns g )
1 1
n n Progre s sBar,
n ( n n
n , 1 ) n
andl er .
n Create New Project
:
Application name -
C reate Activity -
Progres sBarApp;
Progres sar Sarnple;
ProgressBarAct ivity.
LinearLayout,
ProgressBar n, Stari Stop, n 6.24.
1 23
ProgressBarAct ivi ty n,
, Q Progres sBar.
P rogres sBarAct ivity n 6.25 .
1 24
fa lse ;
@Ove r ride
pul ic void onCreate ( Bundle s avedi ns tanceState )
supe r . onCreate ( s avedinstanceStat e ) ;
s etContentView ( R . l ayout . main ) ;
mProgres sBar
new Handler ( )
@Ove rride
pu l i c void handleMes s age ( Me s sage ms g )
mProgre ssBa r . inc rement ProgressBy ( l ) ;
};
1 1 ProgressBar
1 1 P rogre ssBar
1 25
onStop ( ) ;
1 1
pu l i c void onStart ( )
supe r . onStart ( ) ;
mProgre s s Ba r . setProgre s s ( O ) ;
1 1
Thread background = new Thread ( new Runnale ( )
pu l i c vo id run ( )
whi l e
(mi s Running )
try {
Thread . s leep ( l O O ) ;
catch
( I nte rruptedException )
}) ;
mi sRunning
t rue ;
bac kground . s t a rt ( ) ;
pu l i c void onStop ( )
supe r . onStop ( )
mi sRunn ing
fa l s e ;
.
. 6. 1 7.
6. 7 2 SeekBar
.
SeekBa r - (),
. seekBa r Progressar.
,
.
1 26
. 6 . 1 7. Progres s Ba r
s e e kar
SeekBa r . OnSee kBarChangeLi s t ener.
,
Ac t i vi t-:
D onProg re s s Changed ( )
- ,
;
D onS t a rtTrackingTouch ( )
- ,
;
- ,
.
s e e kBar,
, - S e e kBa r . OnS e e kBa rChange L i s t ener.
' s e e ka r Create
New Proj ect :
D
S e e kBar;
S amp l e ;
127
Lia rLayout,
SeekBar ,
, 6.26.
seekBarActivity ,
.
6.27.
1 28
( Seeka r ) findViewBy id ( R . id . s e e k ba r ) ;
_
1 1 n
@ Ove rride
pu l i c void onStopTrackingTouch ( Se e kBar seeka r )
1 1 , OnSeekBarChange L i s tener
11 n n
@ Ove rride
pul i c void onProg res s Changed ( See kBar seekBa r ,
bool ean fromU s e r )
1 29
. 6. 1 8. SeekBar
.
. 6 . 1 8 .
.
6. 7 3 RatingBar
.
Rat ingBa r:
- ;
- ;
i s i ndicator ( )
- ;
s e t StepS i z e ( float )
- .
1 30
Rat ingBa r
Create New Project :
D Proj ect name - Rat ingBar;
D Application name - RatingBar
D Package name -
Sample;
131
android : layout_he i ght="wrap content "
android : text= "Value :
android : padding= " ! Opx "
android : textStyle= "bold " />
<TextView
android : id= " @ +id/ text value "
android : l ayout_he i ght = "wrap_content "
android : layout_width= " 4 0px "
android : textStyle= "bold " / >
< / LinearLayout>
< /LinearLayout >
RatingarActivity
n U Down, n
Rat i ngBar, OnRat i ngBa rChangeLi s tene r
( 6 .29).
5;
0 . 5f;
l . Of;
@ Ove r ride
pu i c void onCreate ( Bundle savedins tance State )
supe r . onCreate ( s avedinstance State ) ;
setContentView ( R . layout . main ) ;
1 32
6
fina l Rat i ngBar ratingBa r l
( Rat ingBa r ) findVi ewByid ( R . id . rating ) ;
final Button buttonUp
( Button ) f indViewByid ( R . id . button up ) ;
_
-=
mS t ep ;
(mRating < 0 )
mRat ing
1 33
.
. 6. 1 9 .
Rat ingBa r : ,
Rat ingBa r , Up
Down.
. 6 . 1 9 . Rat ingBar
6 . 8 .
:
D
Ana logClock;
D i g i t a l C l ock;
Chronorne t e r .
D i g i t a l C l o c k
Ana logClock. ,
. Ana logClock,
D i g i t a l C l o c k Chronorne t e r . 6.20.
1 34
Object
-----,
View
------,
TextView
.------,
f------1 AnalogCiock
DigitaiCiock
'------1 Chronometer
Sarnple;
6.30.
1 35
.
. 6 .2 1 .
. 6.21 .
6 . 8 . 2 . Chronometer
Ch ronomet e r - .
,
.
s t art
Ch ronome t e r :
( ) - ;
s t op ( )
- ;
s e t Fo rma t
Chronome t e r
:
getOnChronome t e rT i ckLi s tene r ( ) ;
s e t OnChronome t e rT i c k L i s tene r ( ) .
1 36
.
, Chronometer,
Create New Project :
Linea rLayout,
Chronometer - Start,
Stop Reset ( 6 .3 1 )
.
137
<Button
android : id= " @ + id/button res et "
android : la yout_width= "wrap_cont ent "
android : l ayout_he ight = "wrap_content "
android : text= " R se t " / >
< / Linea rLayout >
< /Linea rLayout>
ChronometerActivity
, , 6.32.
1 38
6
pu l i c vo id onC l i c k ( V i ew v )
mCh ronome t e r . s t a r t ( ) ;
1 1
but tonStop . s e tOnC l i c kL i s t ene r ( new OnC l ickLi s t ene r ( )
@ Ove r r i de
pu l i c vo i d onC l i c k ( V i ew v )
mCh ronome t e r . s t op ( ) ;
1 1
but tonRe s e t . s e tOnC l i c kLi s tene r ( new OnC l i c k L i s tene r ( )
@ Ove r r i de
pu l i c vo i d onC l i c k ( V i ew v )
.
Chronome t e r . 6 .22.
. 6 . 2 2 . Chronome t e r
.7
-
-,
,
.
7 .1 .
,
.
ArrayAdapter<T>.
, , :
S t ring [ ] items= { " one " ,
ArrayAdapt e r :
D Context - , Act ivity.
context
. context
,
, , , ,
. .;
D .
- s imp le_l i s t_item_l .
- ,
android . R . layout, s imple_spinne r_dropdown_i tem, s imple
1 40
g a l l e ry i tem, s imple_l i s t _ i tern_checked
., , ,
L i s t <T >
ArrayAdapte r toString ( )
. R . l ayout . s irnple_l ist_
i tern_ 1 TextVi ew, ,
, Li s tView (
- ).
,
ArrayAdapter, getView ( )
, .
7 .2 .
Adroid :
AutoCornpl eteTextView;
Mult iAutoCornpl eteTextView.
extView
Editext .
. 7 . 1 .
TextView
Ed itText
. 7.1 .
AutoCornpleteTextView
.
,
.
-
141
AutoCompleteTextView Editext,
.
, AutoComple teTextView and roid : completion
Threshold ,
,
. AutoCompleteTextView
, setAdapte r ( ) .
AutoComplet eTextView
Create New Proj ect
:
Application name
AutoComple teTextViewSample;
7. 1 .
AutoCompleteTextView
7.2.
(,
),
AutoCompleteTextView.
142
"Mi chael Honeyman " , " I sabe l l a Jackson " , "Wi l l i am Patterson " ,
" Jos eph Godwi n " , " S amantha Bush " ,
@ Ove rride
pu l i c void onCreate ( Bundle s avedinstance State )
super . onCreate ( savedins tanceState ) ;
setContentView ( R . layout . ma in ) ;
mText
int a fte r )
1 43
.
(. 7 .2).
joshua Ha rrison
joseph Godwi n
Mu lt iAutoCompleteTextView -
,
AutoComplet eTextView,
, .
setTokeni zer ( ) :
Mu lt iAutoCompleteTextView textView
( Mult iAutoCompleteTextVi ew ) findViewByid (
R . id . Mul t iAutoCompl eteTextViewO l ) ;
textView . s etAdapte r ( adapte r ) ;
1 44
Sarnple;
7.3 .
,
s e tToken i z e r ( ) :
textView . setTokeni zer ( new Mul t iAutoCornpleteTextView . CommaToken i z e r ( ) ) ;
1 45
Harrison, joshua
joh nso n, Madison
Godwi n, joseph
146
.
. 7.3 . ,
.
7 . 3 .
- Android :
L i s tVi ew;
GridVi ew;
Ga l l e ry;
Spinne r;
S l i dingD rawe r.
, ( S l i dingD rawe r)
Adapt e rVi ew.
.
. 7.4.
Object
1
-ViewGro u p--,1
-----,
View
---
--,
---
1-------1 AdapterView
,----....,
Abs li stView 1
AbsS p i n n e r
'------1 S l i d i n g D rawer
,----....,
G ridView
ListView
1
1
-----,
Gallery
Spinner
1
1
. 7.4.
Adapte rView
:
AdapterVi ew -
Adapter,
Vi ewGroup,
147
Adapter
(, ) AdapterVi ew, .
Adapter ,
Cursor ArrayAdapt e r
.
,
, ,
SQLite. ,
AdapterView View
Adapt er.
15.
Curso rAdapt er
7 3 1 ListView
.
L i s tView .
L i s tView L i s tAdapter.
,
Act ivity, ListView
L i s tAct i vi ty.
L i s tAct ivity ,
, ,
, . . L i s tAct ivity
L i s tVi ew,
. L i s tActivity
, ,
( - ).
Li stAct ivity
, L i s tAdapter. Android
:
S impleAdapt er;
S impleCursorAdapte r.
s impleAdapt e r
. S impleCursorAdapt e r , ,
15.
1 48
ListView
Create New Project :
D Project name - L i s tVi ewApp;
D Application name - ListView
D Package name -
Sample;
LinearLayout ' L i s tView
TextView ,
7 . 5 .
L i s tAct ivity ,
, .
1 49
-
import android . view . View;
import andro id . widget . ArrayAdapt e r ;
import android . widget . ListView ;
import andro id . widget . TextView ;
pu l i c c l a s s L i s tVi ewAct ivity extends L i s tAct ivity
,private TextView mTextView ;
St ring [ ] mCont acts
" Jacob Ande rson" , " Emi ly Duncan " , "Mi chael Fu l le r " ,
" Emma Greenman " ,
" Jos eph Godwi n " , " S amantha Bus h " , " Chris tophe r Gateman " ) ;
@ Ove rride
pu l i c void onC reate ( Bundle s avedinstance State )
super . onCreate ( savedinstanceStat e ) ;
setContentView ( R . layout . ma in ) ;
setLis tAdapte r ( new ArrayAdapter<String> (
thi s , android . R . layout . s imple_l is t_item_l , mContacts ) ) ;
mTextView
.
(. 7.5).
7 3 2 Spinner
.
Spinner - m ( ) Android.
-
, ,
Lis tView, .
Spinner, ,
Spinner, - .
1 50
. 7.5. L i s tView
C reate New Project :
Proj ect name -
Spinne rApp;
Spinne r S amp l e ;
- .
Create Activity -
s amp l e s . spinne r ;
Sp inne rAc t i v i t y .
7.7
<Linea rLayout xmlns : andro i d= " http : / / s chema s . andro i d . com/ apk / re s / andro i d "
andro i d : o r i entation= " v e rt i ca l "
andr oid : l ayout_width= " f i l l_pa rent "
andro id : l ayout_he i ght= " f i l l_pa rent "
<TextView
andro i d : id= " @ + id/TextViewO l "
151
-
android : layout_width= " fi l l_parent "
android : layout_he i ght= "wrap_content " / >
< Spinner
android : id= " @ + id/SpinnerO l "
android : layout_width= " f i l l_parent "
android : layout_he i ght="wrap_cont ent "
android : drawSe lectorOnTop= " t rue " />
< /Linea rLayout>
7 . 2 . SpinnerAct ivi ty 7 . 8 .
"Emma Greenman " 1 " Joshua Har r i s on " 1 "Madi s on Johnson " 1
"Matthew Cotman " 1
@ Ove r ride
pu l i c void onC reate ( Bundle s avedinstance State )
super . onCreate ( s avedi nstanceState ) ;
s etContentView ( R . layout . ma in ) ;
1 52
7
mLabel
new ArrayAdapter<String> (
pu l i c void on i ternSelected (
Adapte rView< ? > parent , View v,
Williarn Patterso
. 7.6. Spinner
1 53
.
Spinne r . 7.6.
7 3 3 GridView
.
Gr idView . Gr idView,
TextView,
Lis tVi ew,
, ,
ArrayAdapte r, getView ( ) .
GridView .
,
.
, :
.
auto_fi t, ,
;
android : numColnn s -
;
android : colnnWidth -
android : s t retchode -
GridView
Ecl i pse
Create New Project :
Application n - Gr idView
Sample;
7.9.
1 54
-
7 . 1 .
DataAdapter,
Greenman " ,
ArrayAdapter,
1 55
-
" . Cotman " ,
Jackson" ,
"I .
"W .
Patterson " ,
" . Gateman" ,
" .
" .
Duncan " ,
"I .
"W .
Patterson " ,
Context mContext ;
DataAdapter ( Context context ,
int resource )
context ;
GridVi ewActivity 7 . 1 1 .
, GridView ,
:
setAdapter ( 1 ,
s etOni temSelectedLi s t ene r ( } ,
oni t emSelected ( ) onNothingSe lected ( } .
1 56
final Gr idView g
mAdapte r
@ Ove rride
pu l i c void oni t emS e l ected (AdapterView< ? > parent , View v ,
int pos it ion,
mSe l e c tText . s e tText ( " S e l e cted items :
l ong i d )
+
mAdapt e r . Get item ( p o s i t i on ) ) ;
@ Ove r ride
pu l i c vo id onNothing S e lected (AdapterView< ? > parent )
mS e l ectText . s etText ( " S e l ected items : none " ) ;
.
(. 7. 7).
157
'
. 7.7. GridView
7 4
.
Android
. , -
.
GridView
- G a l l e ry S l idingDrawer.
7.4. 1 . GridView
GridView n .
( - )
, .
n GridView
Ecl i pse n Create
New Proj ect :
Package name -
1 58
,
GridView ,
7.9.
a seAdapter. ,
.
ArrayAdapte r<T> .
, res/drawaie/.
photo l .j pg ... photo8.j pg
Resources/Images/ .
New 1 Class. New
Java Class. Name , ImageAdapter,
Finish (. 7 . 8).
Java Class
dfi The use of the default package is discouraged.
- : :. : .
::: :
L._P.?.y-w-ag!c
. .
. 7.8.
1 59
7. 1 2 .
IrnageAdapte r
context ;
pu l i c int getCount ( )
return mimage s . length ;
1 60
7
pu l i c long get i temid ( int po s i t i on )
return mimage s [ po s i t ion] ;
ImageView
pu l i c View getView (
int po s i t ion, View conve rtView, Vi ewG roup parent )
ImageView view ;
i f ( convertView
view
nul l )
. 7.9.
GridVi ew,
161
(.
7. 1 1 ), onCreate ( ) :
Adapter
, .
(. 7.9).
7 4 2 Gallery
.
Ga l le r y - ,
.
D-pad . Ga l l e ry
.
,
.1 .
Gallery
Create New Project :
Sample;
7. 1 3 .
1 62
<TextView
android : id= " @ +id/ text "
android : layout_width= "wrap_content "
android : layout_he ight= "wrap_content "
android : textS t yle= "bold"
android : text S i z e= " 2 4 px " / >
< / Linea rLayout>
- ImageAdapter 7. 1 4.
GridView Ga l l e ry
getView ( ) , .
pu l i c ImageAdapte r ( Context )
mContext
pu l i c View getVi ew (
int pos i t ion, View conve rtVi ew , ViewGroup parent )
ImageView view = new ImageView (mContext ) ;
1 63
-
view . s e t imageResource (mimage [position] ) ;
view . s e t Padding ( 2 0 , 2 0 , 2 0 , 2 0 ) ;
view . set LayoutParams ( new Ga l lery . LayoutParams ( l 4 0 , 1 9 0 ) ) ;
view . setSca leType ( ImageVi ew . ScaleType . FIT_XY ) ;
view . setBackgroundResource ( mGa l leryi temac kground ) ;
return view ;
pu l i c int getCount ( )
Gal l eryActivity
7 . 1 5 .
( Ga l lery)
findViewByid ( R . id . ga l l e ry ) ;
1 64
final TextView labe l
+ g . getAdapte r ( ) . getCount ( ) ) ;
v,
label . s etText (
" S l ide
+ ++pos
f rom
+ pa rent . ge tCount ( ) ) ;
.
<
,
. 7 . 1 .
. 7. 1 0 . Ga l l e ry
7 4 3 SlidingDrawer
.
S l i d i ng Drawe r - .
Application Launcher,
, ( . . 2 .9).
t-
1 65
S l i dingDrawe r
.
. S l i dingDrawe r .
: , ,
,
.
S l i dingDrawe r ,
S l idingDrawe r.
f i l l_pa rent .
L- S l idingDrawe r
( )
.
S l idingDrawe r
. S l idingDrawe r ,
- ,
.
, .
L- S l idingDrawe r
:
android : handle= " @ + id/handl e "
android : content= " @ +id/content "
S l idingDrawe r
Ecl ipse C reate New Project
:
D Project name -
S l i dingDrawe rApp;
D Application name -
S l idingDrawe r Sample;
, . 7. 4. 1 .
Resources/Images/
. , ,
Android SDK.
GridView
7 . 1 6 .
1 66
167
< /LinearLayout>
< / S l idingDrawe r>
< / Linea rLayout>
S l idingDrawe rActivity
GridView 7 . 1 7 .
new ImageAdapter ( th i s ) ;
@ Override
pu l i c void oni temSel ected (Adapte rView< ? > parent ,
View v,
1 68
@ Override
pul i c void onNothingS elected ( Adapte rView< ?> parent )
mSe l ectText . s etText ( " Sel ected items : none " ) ;
- ImageAdapt er
,
.
7 . 1 8 .
1 69
==
nul l )
pul i c i n t getCount ( )
return mimages . length ;
. ,
. S l i dingorawe r
(. 7. 1 1 )
.
1 70
. 7.1 1 . S l i dingDrawer
7 5
.
- ,
.
,
.
S impl eAdapter S impleCurs orAdapter.
, L-. :
S impl eAdapte r -
S impleAdapte r ( Context context , L i s t < ? extends Map< St ring , ? > > dat a ,
int resource , S t ring [ ]
from, int [ ] t o )
..
, , ,
: -,
- , - .
ArrayLi s t .
S implecursorAdapt e r
. 15.
1 71
,
. C reate New
Project :
Project name - Lis tContact;
Application name - Contacts Sample;
Package name -
. samples . l is tcontact;
, ,
: Name Phone
.
7. 1 9 .
TextView,
, ,
contact r t em, sh,
N PHONE -
. 7.20.
1 72
lL;
"name " ;
"phone " ;
ListContactActivity ArrayLis t
( 7.2 1 ).
/ 1
" 4 12 4 1 2 4 1 1 " ) ) ;
" 8 9 6 4 4 3 65 8 " ) ) ;
" 964990543" ) ) ;
1 73
-
" 687 699999 " ) ) ;
" 1 61 8 6 3 1 8 7 " ) ) ;
"54 6599645" ) ) ;
" 907868756" ) ) ;
"687699693 " ) ) ;
" 8 9 68 7 4 55 6 " ) ) ;
L i s tAdapt e r adapt e r
th i s ,
1ist ,
new S t ring [ ]
new int [ ]
{ R . i d . name ,
R . id . phone } ) ;
s e t L i s tAdapte r ( adapte r ) ;
. 7 1 2
.
( 1 1)
( 15) .
. 7. 1 2 .
, .
, ,
.
:
Toast Not i f i ca t i on -
:::J
B a r Not i f i ca t i on - ,
.
,
;
S t atus
8 . 1 .
,
.
.:: , ,
.
.
\ , .
. ,
,
.
Toa s t oa s t . ma keText ( ) , -
1 76
show ( ) ,
:
Context context
Toa s t toa s t
getAppl i ca t i onContext ( ) ;
toa s t . show ( ) ;
ma keText ( ) :
D ;
D ;
D ,
:
LENGTH_LONG -
.
LENGTH SHORT -
, .
.
s e tG ravi t y ( int , int , i nt ) . :
D
, Grav i t y (,
GRAVITY . CENTER, GRAVITY . .);
D ;
D .
, ,
:
toast . s e tGravi t y ( G rav i t y . CENTER ,
, 0) ;
,
. -
.
C reate New Proj ect :
D Proj ect name - Toas tNot i f i c a t i onApp;
D Application name - Toas tNot i f i ca t i on
Samp l e ;
1 77
Ll Package name -
8 . 1 .
Button
oastAct ivity, , ,
8 . 2 .
1 78
s e tContentView ( R . l a yout . ma i n ) ;
mutton
Context context
ge tApp l i c a t i onContext ( ) ;
Toa s t toas t
toas t . s how ( ) ;
.
(. 8 . 1 ) .
. 8 . 1 .
1 79
8.2 .
,
.
L-
Layout inflater getLayout inflater ( )
getsys temS e rvice ( ) , Layout inflater. i
inflate ( ) View .
, cus tom_layout . xml
android : id= " @ +id/toa s t la yout "
:
Layoutinflater inflater
View layout
getLayout inflate r ( ) ;
inflate ( ) :
custom
la yout . xml ) ;
toast _layout ) .
findViewByid ( )
.
o a s t , ,
, Gravi t y .
Toast toast
setView ( ) ,
, show ( ) ,
:
toast . s etView ( layout ) ;
toast . show ( ) ;
Create New Proj ect :
1 80
( . 8. 1 ).
Android Tools 1 New Resource File. New And roid XML File.
File - cus tom_l ayout . xml,
What type of resource would you like to create?
Layout, Folder Select the root element for the XML fille
(. 8.2).
Project L9!mTtiFit;=--
torn)Yct:;rnl .
Aie
What
type
of resourc
would
you bke
to
cr
Ovalues
0i;;!i@
Searchae
reference
Wht type of resouce coofiguration you lil<e?
.
i ;,=ode
li::
"
! 4$ Navigati
1R
\' i j!i
1
---...................... .
= ::::;
l:J.oimensi
Foider
..
.,. ",".
j d Orientation
! Pixel Density
1 ;::!
(! AppWidget Provlder
i.i<;
vaiahki Quaiflers
l'fct;c;,; .
Q enu
Q Anlmatlon
Fin1sh
. 8.2. L-
1 !' Cancel
181
Finish .
toast_layout.
, IrnageView TextView, 8 .3 .
LinearLayout
<LinearLayout xmlns : android= " http : / / schernas . android . com/ apk/ re s / andro id"
andro id : id= " @+ i d/toast_l ayout "
andro id : orientation= " hori z onta l "
andro id : l ayout_width= " fi l l_pa rent "
andro id : layout_he i ght= " fi l l_pa rent "
android : padding= " l Odp "
android : background= " # DAAA" >
< IrnageView android : id= " @ +id/ irnage "
androi d : l ayout_width= "wrap_content "
androi d : layout_he ight= " f i l l_pa rent "
andro id : l ayout_ma rginRight= " l Odp " / >
<TextView android : id= " @ +id/text "
andro i d : layout_width= "wrap_content "
andro id : layout_he i ght= " fi l l_pa rent "
android : textColor= " # FFF" / >
</LinearLayout >
1 82
pu i c c l a s s CustomToastActivity extends Activity
implements View . OnC l i ckLi s tener
Button mutton ;
@ Ove rride
pu i c void onCreate ( Bundle s avedi ns tance S tate )
supe r . onCreate ( savedinstance State ) ;
setContentView ( R . layout . ma in ) ;
mut ton= ( Button ) findViewByid ( R . id . button) ;
mut ton . s etOnC l i ckLi s tene r ( thi s ) ;
getLayout inflat e r ( ) ;
.
(. 8.3).
8 . 3 .
( ,
, Notifications).
, Adroid I ntent,
. -
1 83
. 8.3.
.1 ,
.
,
\1 - .
.1 ,
, , . .
,
.
, ,
.
1 2 .
,
:
Not i f i cat ion -
, ,
( .);
1 84
getSystemSe rvice ( ) ,
NOT I FI CA ION_SERVICE,
Context :
"Warning ! " ;
long when
System . cur rentT imeMi l l i s ( ) ;
Not i fi cat ion not i f i cat ion
new Not i fication ( i con , t i c ke rTex t , when ) ;
:
Cha rSequence contentT i t l e
Cha rSequence contentText
rntent :
Context context
getApplicationCont ext ( ) ;
Intent intent
new Intent ( th i s , MyC l a s s . c l a s s ) ;
Pendingintent,
,
:
Pendingintent contentintent
Pending intent
:
getAct i vi ty ( )
,
;
( , );
Pending rntent
rntent;
1 4.
1 85
not i f i cat ion . s etLatestEvent info (
context , contentT i t l e , contentText , content intent ) ;
Not i ficationanage r
NOT I FY
,
.
_
Create New Proj ect
:
Proj ect name - StatusBarNot i ficationApp;
Application name - StatusBarNot i fi cation Sarnple;
Package name - com . s arnpl e s . s tatusbarnot i f i cation;
Create Activity - S tatusBarNo t i f i cationActivity.
8 . 1 ( n
).
StatusBarNot i f i cationActivity ,
, .
St atusBarNot i f i cationActivity 8 . 5 .
101;
1 86
@ Ove r ride
pul i c void onCreate ( Bundle savedins tanceState )
supe r . onCreate ( s avedinstance State ) ;
setContentView ( R . layout . rnain ) ;
mNot ifyMgr
" He l l o ! " ;
Context context
getAppl icationContext ( ) ;
new Intent (
.
.
(. 8 .4).
Notifications Clear.
, ,
"No service"
.
187
l'fD( 01 :38
2010 1 3
. 8.4.
8 . 4 .
'v ,
.
,
Notifications, .
s e t La t e s tEvent i n fo ( )
:
contentT i t l e - contentext -
.
, RemoteViews
contentVi ew :
RemoteVi ews cont entV iew
getPacka geName ( ) ,
new RemoteViews (
text ) ;
R . drawa e . android d ) ;
1 88
Create New Project
:
Project name - CustornStatusBarNot i ficationApp;
Application name - CustornS tatusBarNo t i fication Sarnple;
Package name - corn . s arnp l e s . custorns tatusbarno t i f i cat ion;
C reate Activity - CustomS tatusBa rNot i f i cationAct ivity.
mai .xml 8 . 1 .
custom_layout.xm l , 8.3
.
CustornStatusBarNot i f icat, ionAct ivity
, ,
. cus tornS tatusBarNot i f icat ionAc t ivity
8.6.
ID
Oxl O O l ;
1 89
@ Ove r ride
pul i c void onCreate ( Bundle s avedins tanceState )
supe r . onCreate ( savedinstance Stat e ) ;
setContentView ( R . layout . main ) ;
mNot i fyMgr = ( Not i ficationanage r ) get SystemSe rvi ce (
Context . NOT I FICAT I ON_SERVICE ) ;
mut ton= ( Button ) f indViewByid ( R . id . but t on ) ;
mut ton . setOnC l i ckLi stene r ( thi s ) ;
R . drawae . android_happy ;
"Hello ! " ;
Pendingintent . ge tActivi t y (
content !ntent ;
contentView;
.
.
,
(. 8 . 5 ).
1 90
. 8.5.
- ,
. ,
.
,
,
.
Android :
Ale rtDialog - , , -
;
Progre s s Di a l og - ;
Date PickerDialog - ;
T irnePi ckerDialog - .
. 9 . 1 .
Dia log
.
DatePickerDia log -
.
Object
Dialog
AlertDialog
1
1-----1 TimePickerDialog 1
P rogressDialog 1
------,
1-----1 DatePickerDialog
9.1 .
1 92
9 . 1 .
.
oncreateDialog ( ) ,
. Android
( ) ,
.
onCreateDialog ( ) ,
,
. ,
setOWnerAct i vi t ( ) , .
showDialog ( )
(,
), .
:
private s t a t i c final i n t I D D EXIT
showDialog ( I DD_EXIT ) ;
Android
onCreateDia log ( ) . onCreateDialog ( )
, showDialog ( ) .
, .
,
, :
pr ivate s ta t i c final int I D D ALERT
onCreateDialog ( ) swi t ch:
showDialog ( )
1 93
de faul t :
dialog
nul l ;
return dialog ;
swi t ch
,
.
Adroid
onPrepa reDia log ( int , Dialog ) .
(,
),
. Dialog,
onCreateDialog ( ) .
9 . 2 . AlertDialog
Ale rt D ialog - Dialog.
.
:
;
;
, ;
;
;
.
9 . 2. 1 . A lertDia/og
Ale r t Dialog
( ) Al ertDialog . Bui lde r:
setPosit iveButton ( ) ;
setNegat i veButton ( ) ;
setNeutra lButton ( ) .
n n :
AlertDialog . Bu i lde r bui lder
new AlertDialog . Bu i lde r ( getAppl icat ionContext ( ) ) ;
ui lder,
1 94
, j1 Bui lde r,
,
setMe s sage ( ) :
bui lder . setMe s s age ( "Are you sure you want to exit ? " ) ;
. AlertDialog
: Pos it ive, Negat ive Neut ral, . .
- .
set . . utton ( )
Dia loginterface . onc l ic kLi s tene r, ,
. , Yes
No :
,
bui lde r . s e t Pos i t iveButton ( " Yes " , new D i a l oginterface . OnCl ickLi s tene r ( )
pu l i c void onCl i c k ( Dia loginte rface dialog , int i d )
1 1 rn
AlertDialogButtonActivity . this . finish ( ) ;
}) ;
bui lde r . setNegat iveButton ( "No " , new D i a l oginterface . OnCl i ckLi s t ene r ( )
pu l i c void onCl i c k ( Dia loginterface dialog , int i d )
1 1 m
dialog . cancel ( ) ;
<Back>
, setCance lale ( ) :
bui l de r . setCance lale ( fa l s e ) ;
, :
Alert D i a l og alert
1 95
9 . 1 .
@ Ove r r i de
pu l i c void onCreate ( Bundle s avedins tance S tate )
supe r . onCreate ( savedi nstance State ) ;
setContentView ( R . layout . main ) ;
1 96
9
final Button cal lButton
@ Ove rride
pu l i c void onC l i c k (View v )
1 1
showDialog ( I DD_EXIT ) ;
@ Ove rride
protected Dialog onCreateDia log ( int i d )
swit ch ( id )
case I DD EXIT :
Ale rtDialog . Bu i lder bui lder
bui lder . s etMe s s age ( "Are you sure you want to exit ? " ) ;
1 / "Ye s "
bui lde r . s etPo s i tiveButton (
"Yes " , new Dialoginterface . OnC l i c kLis tener ( )
pu l i c void onCl i c k ( Dialoginte rface dialog , int id)
AlertDialogButtonAc t ivity . this . finish { ) ;
}) ;
1 1 "No "
bui lde r . s e tNegativeButton (
"No" , new Dia loginte rface . OnC l i ckListener ( )
pu l i c void onCl ick ( Dialoginte rface dialog , int id)
dialog . cance l ( ) ;
.
AlertDialog Yes No.
197
Yes .
. 9.2.
9 . 2 . 2 . A lertDialog
Al ertDialog ,
s e t r t ems ( ) ,
Dialogint e r face . OnC l i c kLis tener, ,
, :
Cha rSequence [ ] colors
bui lder . s e t i t ems ( colo r s , new D i a l ogint e r face . OnCl ickLi stener ( )
pu i c vo id onCl i c k ( Dialogint e r face dialog , int i tem )
1 1
Toa s t . ma keText ( getApp l i ca t ionContext ( ) ,
"Color :
Create New Project :
1 98
main.xml 9. 1 .
, . 9.3 .
;
{ " Red" , "Green" , " Blue "
@ Override
pu i c void onC reate ( Bundle s avedinstance State )
supe r . onCreate ( s avedinstance State ) ;
setContentView ( R . layout . ma in ) ;
final Button cal lButton
callButton . s e tOnCl ickLis tener ( new OnCl ickLi stene r ( ) {
@Ove rride
pu ic void onC l i c k (View v )
showDi a l og ( I DD_COLOR ) ;
1 99
@ Ove r r i de
protected D i a l o g onCre a t e D i a log ( int i d )
swi t ch
( id )
c a s e I D D COLOR :
Al e r t D i a l og . Bu i l der bui lde r
return bu i l de r . create ( ) ;
defaul t :
return nul l ;
Pick
"Color :
bu i l de r . s e t Cance l ale ( fa l s e ) ;
G)
int i t em)
color
Red
Green
Blue
. 9 . 3. Al e r t D i a log
200
n . n
AlertDi a l og n n
(. 9.3). n n
, .
9 . 2 . 3 . AlertDialog
n n
s e t S ingleCho i c e i tems ( ) .
AlertDialog
n n
s e t i t ems ( )
n s e t S i ngl eChoiceitems ( ) -
, -
, n .
, - 1 .
.
, ,
.
.
n n
Create New Proj ect
:
Proj ect name - Ale rtDialogRadioBut tonsApp;
Application name - Alert Dial ogRadi oButtons Sample;
201
Package name - com . sample s . ale rtdialogradiobuttons ;
Create Activity - AlertDialogRadioButtonsActivity.
main .xm l 9. 1 .
, .
9.4.
AlertDialogRadioButtonsActi vi ty
@ Override
pu l i c void onCreate ( Bundle s avedins tance State )
supe r . onCreate ( savedinstance State ) ;
setContentView ( R . layout . main ) ;
f ina l Button callButton
cal lButton . setOnC l i c kLis tene r ( new OnCl ickLis tene r ( ) {
@Ove rride
pu l i c void onC l i c k ( View v )
showDi alog ( I DD_COLOR ) ;
202
9
@ Ove rride
protected D i a l og onCreateDia l og ( int i d )
switch ( id ) {
case I D D COLOR :
Al e r t D i a l og . Bui lder bui lder
new Al ertDia log . Bu i l der ( thi s ) ;
bu i lde r . s etT i t l e ( " Pi c k color'
bu i l de r . s etS ingleChoicei tems (mColors , ,
new D i a log interface . OnC l i c kL i s tene r ( )
pu ic vo id onCl i c k ( Di a l oginterface d i a l og , int item)
Toast . ma keText ( getAppl icationContext ( ) , "Color :
mColors [ i tem] , Toa st . LENGTH_SHORT ) . s how ( ) ;
.
AlertDi a l og .
Green
B l ue
. 9.4. AlertDialog
203
,
(. 9.4).
9 . 2.4. AlertDialog
setS ingl eCho icertems ( ) .
{ t rue ,
"Blue " } ;
fa l s e , fa l s e } ;
i s Checked ;
, ,
.
Create New Project :
D Proj ect name - Al ertDial ogChec kBoxesApp;
D Application name - Al ertDial ogChec kBoxes S ample;
D Package name - . samp l e s . a l e rtdial ogche ckboxe s ;
D C reate Activity - Al ertDi a l ogChec kBoxesActi v i ty.
main.xml
9 . 1 .
AlertDia logCheckBoxesActivity
204
, . ,
, 9. 5 .
{ t rue , false , fa l s e ) ;
@ Ove r ride
pul i c voi d onC reate ( Bundle savedinstance State )
super . onCreate ( savedinstance State ) ;
setContentView ( R . layout . main ) ;
f inal Button callButton
cal lButton . s etOnCl ickLis tene r ( new OnC l i c kListene r ( ) {
@ Ove r ride
pu l i c void onCl ick ( View v )
showDialog ( I DD_COLOR ) ;
@ Ove rride
protected Dialog onCreate D i a l og ( int i d )
s.w i tch ( id )
case I DD COLOR :
205
Al ertDialog . Bui lde r bui l der
int which,
i s Checked ;
s tate :
i f (mChec keditems [ i ] )
s tate . append ( " checked\n " ) ;
else
s tate . append ( "unchecked\n" ) ;
Toas t . ma keText ( getApp l i ca t ionContext ( ) ,
s tate . toString ( ) , Toas t . LENGTH_LONG ) . show ( ) ;
builde r . s etNegativeButton (
"No" , new Dialog interface . OnCl ickLis tene r ( )
pu l i c void onCl ick ( Dialoginterface dialog, int i d )
dialog . cance l ( ) ;
Toas t . ma keText ( getApp l i cationContext ( ) ,
" Di a l og cancel " , Toas t . LENGTH_SHORT ) . show ( ) ;
206
n n n . n
n A l e r t D i a l og n
(. 9 . 5 ) . n Yes n n
, n .
Blue
color:
Blue,
state: checked
. 9.5. Al e r t D i a l og
9 . 3 . ProgressDialog
Progre s s D i a log - n Al e r t D i a log, n
n. n
n, n n .
Prog re s s D i a log
Progre s s D i a l og ( Context ) , n n
:
Progre s s D i a l og progre s s D i a l og
, n:
progre s s D i a l og . s et Progre s s S t y l e ( Progre s s Di a l og . STYLE_HOR I Z ONTAL ) ;
progre s s Di a log . s etMe s s a ge ( " Loading .
progre s s D i a l og . s e t Cance lale ( fa l s e ) ;
207
, . .
, Handler
( 5):
Handler handler
new Handler ( )
,
, Ecl ipse
Create New Project :
L] Proj ect name - Progre s s D i a l ogApp;
L] Application name - Progre s s Di a l og Sample;
Package name - com . samples . progre s sdia log;
9 . 1 .
, .
Se condThread,
onCreateDia log ( ) .
Progre s s DialogActivity
208
showDialog ( I DD_PROGRE S S ) ;
case I DD PROGRE S S :
mProgre s s Dialog
mSecondThread . s tart ( ) ;
return mProgre s s Dialog;
defaul t :
return nul l ;
1 1 ,
1 1
final Handle r handler
new Handle r ( )
209
rnProgre s s Dia log . s et Progres s ( total ) ;
i f ( total >= 1 0 0 ) {
di srni s s Dia1og ( I DD_PROGRES S ) ;
rnSecondThread . set State ( SecondThread . STATE_DONE ) ;
1;
int rnState ;
int rnTotal ;
Se condThread ( Handler hnd )
rnandl e r
hnd ;
rnState
STATE_RUNN ING ;
rnTotal
whi le (rnState
STATE_RUNN ING )
try {
Thread . s leep ( 1 0 0 ) ;
catch ( I nte rruptedExcept ion )
Log . ( "ERROR" , "Thread Interrupted " ) ;
Mes s age rnsg
Bundle
210
rns g . s e t Da t a ( ) ;
rnandl e r . s ende s sage ( rns g ) ;
rnTo t a l + + ;
rnS t a t e
.
,
(. 9 . 6).
s e t Progre s s S t y l e ( ) .
, Progre s s D i a l og :
0
STYLE_HOR I Z ONTAL;
STYLE SPINNER.
.
9 . 6 s e t Progre s s S t y l e ( ) :
progre s s D i a l og . s e t P rogre s s S t yle ( Prog res s Di a log . STYLE S P INNER ) ;
_
21 1
n n n .
n n Progre s s D i a l og,
, n (. 9 . 7).
9 . 4 . DatePickerDialog
Da t e P i c ke rD i a l og n n . n
n ,
. , n Ca l enda r n
g e t ( ) Cal enda r n ,
:
C a l enda r
Ye a r
onth
mDay
c . ge t ( Ca l e nda r . YEAR ) ;
c . g e t ( Ca l enda r . MONTH ) ;
c . ge t ( Ca l enda r . DAY O F MONTH ) ;
_ _
onCre a t e D i a log ( )
:
a t e P i c k e r D i a l og d i a l og
Da t e P i c ke rD i a l og
new Da t e P i c k e r D i a l o g (
212
, , -
, -
, ,
.
, -
Date PickerDialog . OnDate setLi s tener,
, , ,
:
private DatePi ckerDia log . OnDateSetListener mDateSetLis tener
new DatePicke rDia l og . OnDateSetListene r ( )
pu l i c void onDateSet ( DatePi cker view ,
int yea r ,
{
yea r ;
month
monthOfYea r ;
mDay = dayOfonth ;
C reate New Proj ect
:
D Proj ect name - DatePickerDial ogApp;
Sample;
213
android : layout_width= "wrap content "
android : layout_he i ght="wrap_content "
android : textS i ze= " 2 8px "
android : textStyle= "bold"
andro id : text= " " / >
<Button
andro id : id=" @ + id/button"
andro id : layout_width= "wrap_content "
andro id : layout_he ight="wrap_conten t "
android : text= " Change t h e date " / >
< / Linea rLayout >
,
9 .9.
@ Ove rride
protected vo id onCreate ( Bundle savedins tanceState )
DatePickerDia log,
214
9
super o onCreate ( savedins tanceState ) ;
setContentView ( R o layout o main ) ;
( TextView ) findViewByid ( R o id o text ) ;
mDateDisplay
mPic kDate
mPi c kDate o s etOnCl i c kL i s tene r ( new View o OnC l i ckLis tene r ( )
pu l i c vo id onCl i c k ( View v )
showDialog ( I DD_DATE ) ;
1 1
final Calenda r
mYe ar
month
mDay
update D i splay ( ) ;
@ Ove r ride
protected D i a l og onCreateDialog ( int id)
swi tch ( id )
case I D D DATE :
return new Date PickerDialog ( th i s ,
mDateSetLis tene r ,
mYea r , month, mDay ) ;
return nul l ;
, TextView
private void updateDisplay ( )
mDateDisplay o setText (
new St ringBui lder ( )
1 1 , 1
o append (month
215
p r ivate Dat e P i c ke rD i a l og . OnDa t e S et L i s t ener rnDat e S e t L i s tene r
new Dat e P i c ke rD i a l og . OnDa t e S e t L i s tene r ( )
pu l i c void onDa t e S e t ( Da t e P i c k e r v i ew ,
int monthOfYea r ,
mYe a r
month
rnDay
int yea r ,
int dayOfMonth )
yea r ;
monthOfYear ;
dayO fonth ;
upda t e D i s p l a y ( ) ;
.
Da t e P i c ke r D i a l og,
.
.
. 9 . 8 .
. 9.8. Da t e P i c ke r D i a l og
216
9 . 5 . TimePickerDialog
.
DatePi ckerDialog .
T imePi ckerDialog
,
Cal enda r, :
Ca lenda r . get instance ( ) ;
Calendar
mour
c . get ( Ca l enda r . HOUR_OF_DAY ) ;
minute
c . ge t ( Ca l enda r . MINUTE ) ;
oncreateDialog ( )
:
T imePi ckerDialog dialog
TimePi ckerDialog
1 2- (/) - fa l se 24- - t rue.
-
TimePi ckerDialog . OnT imeSetListene r,
:
private T imePickerDi a l og . OnT imeSetLi s tener mTimeSetListene r
new T imePi ckerDialog . OnT imeSetListene r ( )
pu l i c void onT ime Set ( TimePicke r view,
int ourOfDay , int minute )
mour
minute
hourOfDay ;
minute ;
Create New Project
:
LJ Project name - TimePicke r D i a logAct i vi tyApp;
LJ Application name - T ime P i c ke rDia logAct ivity Sample;
LJ Package name - com . samp l e s . t imepicke rdialog;
LJ Create Activity - T imePickerDialogActivity.
217
0;
@ Ove r ride
protected vo id onCreate ( Bundle s avedins tanceState )
supe r . onCreat e ( s avedinstance State ) ;
setContentView ( R . layout . ma in ) ;
mT imeDi splay
mPi c kT ime
mPi ckTime . setOnCli ckLi stene r ( new View . OnCl i ckLis tener ( )
pu l i c voi d onC l i c k ( View v )
showDialog ( I DD_T IE ) ;
1 1
final Ca lenda r
mour
Ca lenda r . ge t instance ( ) ;
218
updateDi splay ( ) ;
@ Override
protected D i a l og onCreateDia log ( int id)
swi t ch ( i d )
case I DD I :
return new T imePicke rDia log (
thi s , mTimeSetLis tene r , mou r , minut e , fa l se ) ;
return nul l ;
ffi TextView
private vo id updateDisplay ( )
mT imeDisplay . s etText (
new S t r ingBu i l der ( )
. append (pad (mRou r ) ) . append ( " : " )
. append ( pad (minute ) ) ) ;
hourOfDay ;
minute ;
updateDisplay ( ) ;
.
T ime PickerDialog,
.
(. 9.9).
219
9 . 6 .
,
.
n
.
n ,
n
n L-
View n .
Layout inflater.
Layout i n f l a t e r
get Layout inflater ( ) , n n
inflate ( int , ViewGroup ) , n n -
, - n
:
Layout inflater inflater
View l ayout
getLayout inflate r ( ) ;
n , findViewByid ( )
n -
220
n . n, n
ImageView, :
TextView
TextView text
setView ( )
n
AlertDialog.
n n
n Create New Project :
r:J Project name - Cus tomDialogApp;
r:J Application name - CustomDia log Sample;
r:J Package name -
n
9 . 1 . ,
9 . 1 1 .
<Linea rLayout xmlns : andro id= " http : / / s chema s . android . com/ apk / re s / andro id"
android : id= " @ +id/toa s t_layout "
android : orientat ion= " hori zont a l "
andro id : layout_width= " f i l l_pa rent "
andro id : layout_he i ght= " fi l l_pa rent "
android : padding= " l Odp "
andro id : background= " # DAAA" >
< ImageView android : id= " @ + i d / image"
android : layout_width= " wrap_content "
android : layout_he i ght= " f i l l_pa rent "
android : la yout_ma rg inRight= " l Odp " / >
22 1
<TextView android : id= " @ +id/text "
android : la yout_width= "wrap_content "
android : la yout_he ight= " fi l l_parent "
androi d : textColor= " # FFF" / >
< / L inearLayout >
customDialogAct ivity
, .
Cus tomDialogActivity 9 . 1 2 .
0;
@ Ove r ride
pu l i c void onCreate ( Bundle s avedins tance S tate )
supe r . onCreate ( savedinst ance State ) ;
setContentView ( R . layout . main ) ;
final Button cal lButton
cal lBut ton . setOnC l i ckLi s t ene r ( new OnC l i ckListene r ( ) {
@Override
222
@ Override
protected Dialog onCreateDia log ( int i d )
swi t ch ( id )
case I DD CUSTOM :
Layout inflater inflater
View layout
223
.
A l e r t D i a log Yes No
.
Yes (. 9 . 1 0).
. 9. 1 0 .
10
- . Android
.
Android :
Options ( ) - ,
.
<M EN U> .
:
Icon Menu ( ) - ,
.
. - ,
;
Expanded Menu ( ) -
.
.
More.
Adroid. More
,
;
Context ( ) -
,
( log-press);
S () - ,
. .
1 0
226
1 0 . 1 .
n - n n n
. , , n
<.: <M EN U>.
n, Adroid
onCreateOptionsMenu ( ) , n n enu.
, n
, n enu.
n L- add ( J n
n , n:
private s t a t i c final int I DM OPEN
101;
private s t a t i c final int I DM SAVE
102 ;
menu . add ( Menu . NONE , I DM_SAVE , Menu . NONE , " Save " ) ;
add ( J , , :
r:J - n
n ( n n
);
r:J (n
);
r:J n -
. ( enu . NONE )
n ;
r:J - ( -
, ).
enurtem, n
n , n , " "
n n .
onCreateOpt ionsMenu ( ) , n
. n
n , n n
onPrepareOptionsMenu ( ) .
227
, ,
.
enui t em - , .
get i temid ( ) ,
,
, add ( )
onCreateOpt ionsMenu ( ) .
, .
onOpt ions i temS e l ected ( )
:
pu l i c boolean onOpt ions i temS e lected ( Menuitem item )
switch ( item . geti temid ( )
case I DM OPEN :
return t rue ;
case I DM SAVE :
return t rue ;
return false ;
" " (
) , .
" " :
D
- ;
- ;
- .
:
setAlphabe t i cShort cut ( ' q ' ) ;
( <M ENU>)
<q> . (
) ,
.
C reate
New Proj ect :
D Project name - Opt ionsMenuApp;
D Application name - Opt ionsMenu
Sample;
10
228
Package name
1 0. 1 .
Opt ionsMenuActivity
, .
Opt ionsMenuActivity 1 0.2.
101;
102 ;
103 ;
229
104 ;
105;
@ Ove rride
pu l i c void onCreate ( Bund1e s avedinstanceState )
supe r . onCreate ( s avedinstanceStat e ) ;
set ContentView ( R . 1ayout . main ) ;
@ Ove rride
pu l i c boo1ean onCreateOpt ionsMenu ( Menu menu )
menu . add ( Menu . NONE , I DM_OPEN , Menu . NONE , " Open " )
. s etA1phabeticSho rtcut ( ' o ' ) ;
menu . add ( Menu . NONE , I DM_SAVE , Menu . NONE , " Save " )
. s etA1phabeti cShortcut ( ' s ' ) ;
menu . add ( Menu . NONE , I DM_EDI T , Menu . NONE , "Edit " )
. s etA1phabeticShortcut ( ' e ' ) ;
menu . add ( Menu . NONE , I DM_HELP , Menu . NONE , " 1 " )
. setA1phabet i cShortcut ( ' h ' ) ;
menu . add ( Menu . NONE , I DM_EXI T , Menu . NONE , " Exit " )
. setA1phabet i cShortcut ( ' x ' ) ;
return ( supe r . onCreateOpt ionsMenu (menu ) ) ;
@ Ove rride
pu l i c boo1ean onOpt ions i t emSe1ected ( Menuitem i tem)
Cha rSequence mes s age ;
switch ( item . getitemid ( ) )
case I DM OPEN :
me s sage
brea k ;
case I DM SAVE :
me s sage
bre a k ;
c a s e I DM HELP :
me s sage
break ;
case I DM EDIT :
me s sage
break ;
case I DM EXIT :
me s sage = "Exit item s e 1ected" ;
brea k ;
230
1 0
de faul t :
return fa l s e ;
Toast toa s t
Toa s t . LENGTH_SHORT ) ;
0) ;
toa s t . s how ( ) ;
r e turn t rue ;
. <M ENU>
: Open,
Save, Edit, Help, Exit (. 1 0. 1 ) .
Save
Op<.m
Edlt
Help
EXIt
. 1 0 . 1 .
1 0 . 1 . 1 .
.
s e t i con ( ) . :
menu . add ( Menu . NONE ,
23 1
, ,
Ecl ipse C reate New Project :
D Proj ect name -
I conenuApp;
Iconenu Samp1e;
D C reate Activity -
I conenuAct ivity.
,
(. 1 0. 1 ) .
CD-ROM, Resources/Menu_Icons/ -
ic_menu_open .png, ic_menu_save.png, ic_menu_edit, ic_menu_help ic_menu_
exit.
Opt ions i conenuActivity, ,
1 0 . 3 .
@ Ove rride
pu 1 i c boo1ean onCreateOpt ionsMenu ( Menu menu ) {
menu . add ( Menu . NONE , I DM_OPEN , Menu . NONE , " Open " )
. s e t i con ( R . drawal e . i c_menu_open ) ;
1 0
232
menu . add ( Menu . NONE , I DM_SAVE , Menu . NONE , " Save " )
. s e t i con ( R . drawal e . i c_menu_save ) ;
menu . add ( Meu . NONE , I DM_EDIT , Menu . NONE , "Edi t " )
. s e t l con ( R . drawale . i c_menu_edi t ) ;
menu . add ( Menu . NONE , I DM_HELP, Menu . NONE , "Help " )
. s e t l con ( R . drawa le . i c_menu_he lp) ;
menu . add ( Menu . NONE , I DM_EXI T , Menu . NONE , "Exit " )
. s e t i con ( R . drawal e . i c_menu_ex i t ) ;
return ( supe r . onCreateOpt i onsMenu (menu ) ) ;
@ Ove r ride
pu l i c boo l ean onOpt i ons l t emSelected ( Menul tem i tem)
Cha rSequence mes s age ;
'
swi tch ( i tem . g etltemld ( ) )
case I DM OPEN :
"Open i t em se lected" ;
me s s age
brea k ;
cas e I DM SAVE :
me s s age
" Save i t em selected" ;
bre a k ;
case I DM HELP :
me s s age
"Help i t em selected" ;
brea k ;
case I DM EDIT :
"Edit i t em selected" ;
me s s age
brea k ;
case I DM EXIT :
me s s age
"Exit i t em s e l ected " ;
brea k ;
defaul t :
return fa l s e ;
Toa s t . makeText (
Toast toast
thi s , mes s age , Toa st . LENGTH_SHORT ) ;
toas t . setG ravi t y ( G ravi t y . CENTER , , 0 ) ;
toast . s how ( ) ;
return t rue ;
. <MENU>
, ,
, (. 1 0.2).
233
ii
Open
Edit
Sa.re
0
Exlt
. 1 0. 2 .
1 0 . 1 . 2 .
, ,
.
Andro id .
C reate New Proj ect :
D Proj ect name - Opt i onsExpandedenuApp;
D Application name - Opt ionsExpandedenu Sample;
D Package name -
,
(. 1 0 . 1 ) .
, C D-ROM,
Resources/Menu Icons/.
, ,
:
pr ivate s t a t i c final int I DM FIND RE PLACE
106;
pr iva te s t a t i c fina1 int I DM FIND NEXT
107 ;
private s t a t i c fina1 i nt I DM FIND PREV
108 ;
=
234
1 0
onCreateOpt ionsMenu ( ) (
):
pu 1 i c boo1ean onC reateOpt ionsMenu ( Menu menu )
menu . add ( Menu . NONE , I DM_FIND_RE PLACE , Menu . NONE , " Find/Rep1ace " ) ;
menu . add ( Menu . NONE , I DM_FIND_NEXT , Menu . NONE ,
n n , n
n .
101 ;
102 ;
103 ;
104 ;
105 ;
106;
107 ;
108 ;
@ Ove rride
pu1 ic void onCreate ( Bund1e savedins tanceState )
supe r . onCreate ( savedl nstanceState ) ;
235
@ Ove rride
pu l i c boo lean onCreateOpt ionsMenu ( Menu menu )
1 1
menu . add ( Menu . NONE , I DM_OPEN , Menu . NONE , " Open " )
. set icon ( R . drawale . ic_menu_open ) ;
menu . add ( Menu . NONE , I DM_SAVE , Menu . NONE , " Save " )
. s et icon ( R . drawale . i c_menu_save ) ;
menu . add ( Menu . NONE , I DM_EDIT , Menu . NONE , "Edi t " )
. s et icon ( R . drawale . i c_menu_edi t ) ;
menu . add ( Menu . NONE , I DM_HELP , Menu . NONE , "Help " )
. s et icon ( R . drawal e . ic_menu_he l p ) ;
menu . add ( Menu . NONE , I DM_EXI T , Menu . NONE , " Exit " )
. s et icon ( R . drawale . i c_menu_ex i t ) ;
. ,
. . _
menu . add ( Menu . NONE , I DM_FIND_REPLACE , Menu . NONE , " Find/Replace " ) ;
menu . add ( Menu . NONE , I DM_FIND_NEXT , Menu . NONE ,
@ Ove rride
pu l i c boo lean onOpt ions i t emSe lected ( Menui tem item)
CharSequence mes s age ;
swi t ch ( it em . get i t emi d ( ) )
case I DM OPEN :
me s s age
brea k ;
c a s e I DM SAVE :
mes s age
break ;
case I DM HELP :
mes sage
brea k ;
c a s e I DM EDIT :
me s sage
brea k ;
ca s e I DM EXIT :
me s s age
bre a k ;
236
1 0
ca s e I DM FIND RE PLACE :
me s s age
bre a k ;
c a s e I DM FIND NEXT :
me s s age
brea k ;
ca s e I DM FIND PREV :
me s s age
break ;
de faul t :
return fa l s e ;
Toa s t toas t
0) ;
toa s t . s how ( ) ;
return t rue ;
. <M EN U >
n
More . More
(. 1 0 . 3 ) .
Fi nd/Rep lace
11
Find
G>
Find P revious
Open
sav
Exlt
N e xt
Edl t
Mor e
. 1 0. 3 .
237
1 0 . 2 .
Adroid
, .
( \og-press).
(
) .
oncreateContextMenu ( ) .
onCreateContextMenu ( ) ,
add ( ) onContext i temSelected ( ) .
:
pu l i c void onC reateContextMenu ( ContextMenu menu , View v ,
Cont extMenuinfo menuinfo )
supe r . onCreateContextMenu (menu ,
menu info ) ;
menu . add ( Menu . NONE , I DM_OPEN , Menu . NONE , " Open " ) ;
menu . add ( Menu . NONE , I DM_SAVE , Menu . NONE , " Save " ) ;
, ,
.
Menu i tem - , .
, .
onContext i t emS e l ected ( )
238
1 0
C reate New Project :
arnple;
sarnple s . contextmenu;
ContextMenuActi vity
, 1 0.6.
239
101 ;
102 ;
103 ;
104 ;
105 ;
@ Ove r r i de
pu i c void onCreate ( Bundle savedins tance State )
super . onCreat e ( savedinstance State ) ;
setContentView ( R . l a yout . ma in ) ;
fina l LinearLayout edi t
@ Ove r ride
pu l i c voi d onCreateContextMenu ( ContextMenu menu , View v ,
ContextMenuinfo menuinfo )
" He l p " ) ;
@ Ove r ride
pul i c boolean onContext i t emS e l e cted ( Menuitem i tem)
Cha rSequence mes s age ;
swi t ch ( i tem . get i t emid ( ) )
ca s e I DM OPEN :
mes s age
brea k ;
ca s e I DM SAVE :
me s sage
brea k ;
ca s e I DM HELP :
rne s sage
brea k ;
1 0
240
case I DM EDIT :
" Edit item s e lected " ;
me s sage
brea k ;
ca s e I DM EXIT :
me s s age
brea k ;
de faul t :
n n n . n
n . 1 0.4.
Edit
H el p
Exit
. 1 0.4.
1 0 . 3 .
, n.
n , n ,
24 1
(File, Edit, View . .).
onCreateOpti onsMenu ( ) ,
.
addSubMenu ( ) ,
SubMenu. SubMenu
, add ( ) . :
pu l i c boolean onCreateOpt ionsMenu ( Menu menu )
SubMenu subMenuFi le
"New" ) ;
subMenuFi le . add ( Menu . NONE , I DM_OPEN , Menu . NONE , " Open " ) ;
subMenuFi le . add ( Menu . NONE , I DM_SAVE , Menu . NONE , " Save " ) ;
C reate New Project :
Proj ect name - suenuApp;
Application name - SubMenu S ample;
Package name - com . samples . sumenu;
Create Activity - SubMenuActivity.
,
1 0 . 1 : File, Edit Help.
:
File - New, Open, Save;
Edit - Cut, , Paste.
suenuActivity
1 0.7.
242
1 0
101 ;
201;
202 ;
203 ;
@ Ove rride
pul i c boolean onC reateOpti onsMenu ( Menu rnenu )
SuMenu suMenuFi le
@ Override
pul i c boo lean onOpt i ons l ternSel ected ( Menultern i tern)
Cha rSequence rnes s age ;
swi tch ( it ern . get l t ernid ( ) )
case I DM NEW :
rne s sage
bre a k ;
c a s e I DM OPEN :
rne s sage = " Open i t ern s e l ected" ;
bre a k ;
"Paste " ) ;
243
c a s e I DM SAVE :
me s s age
brea k ;
c a s e I DM CUT :
me s s age
brea k ;
c a s e I DM :
me s s age
"
i t em s e l e c t ed " ;
brea k ;
c a s e I DM PASTE :
me s s age
" Pa s t e i t em s e l ec t e d " ;
brea k ;
c a s e I DM HEL P :
me s s age
"Help i t em s e l ec t ed " ;
brea k ;
de faul t :
return fa l s e ;
rn
Toa s t toa s t
0) ;
toa s t . show ( ) ;
return t rue ;
Cut
Paste
. 1 0.5.
1 0
244
. <MENU>
.
File (. 1 0.5). Edit
.
1 0 . 4.
.
,
setCheckae ( )
Menui tern i t ern
, .
( ).
, add ( ) ,
. .
Color
:
pu i c s t a t i c final int I DM COLOR GROU P
200;
201 ;
pu i c s t a t i c final int I DM COLOR RED
-
202 ;
203;
setGroupCheckal e ( ) .
setCheckale ( ) :
SubMenu suMenuFi l e
"Blue " ) ;
setGroupCheckale ( )
- ;
- t rue, ;
245
- ( t rue )
( fa lse )
.
- .
. ,
:
pulic boolean onOpt ions i temS e lected (Menuitem item)
switch ( i tem . get i t emid ( ) )
case I DM COLOR RE D :
1 1
item . s etChecked ( ! item . i sChec ked ( ) ) ;
brea k ;
Create New Proj ect :
Proj ect name - Chec kaleSubMenuApp;
Application name - CheckaleSuMenu Sample;
Package name - com . samples . chec kalesubmenu;
Create Activity - Checkal eSubMenuActi vi ty.
main .xml 1 0. 1 .
: Color, Font Style, Help. Color Font Style
. :
Color - Red, Green, Blue;
Font Style - Regular, Bold, Italic.
Chec kaeSuMenuActi vi t y
1 0 . 8 .
246
1 0
101 ;
200 ;
201;
2 02 ;
203;
300 ;
301 ;
3 02 ;
303;
@ Ove rride
pu i c vo id onC reate ( Bundle savedins tance State )
supe r . onCreate ( savedins tance State ) ;
setContentView ( R . layout . main ) ;
@ Override
pu l i c boolean onCreateOpti onsMenu ( Menu menu )
SuMenu suMenuFi le
. s etChecked ( t rue ) ;
suMenuEdi t . add ( I DM_FONT_GROUP
suMenuEdi t . add ( I DM_FONT_GROUP
t rue , t rue ) ;
24 7
@ Ove r ride
pu l i c boolean onOpt ions i t emSel ected (Menu i t em i tem)
CharSequence me s s age ;
swi t ch ( item . get i t emi d ( ) )
case I DM COLOR RED :
item . s etChecked ( ! item . i sChe cked ( ) ) ;
me s sage
break ;
case I DM COLOR GREEN :
i t em . s etChecked ( ! item . i sChecked ( ) ) ;
me s sage
bre a k ;
c a s e I DM COLOR BLUE :
item . setChecked ( ! item . i sChe cked ( ) ) ;
me s sage
brea k ;
case I DM REGuq :
item . s etChecked ( t rue ) ;
me s sage
brea k ;
case I DM BOLD :
item . s etChecked ( t rue ) ;
me s sage
bre a k ;
case I DM ITAL I C :
item . s etChecked ( t rue ) ;
me s sage
break ;
case I DM HELP :
me s sage
break ;
defaul t :
return fa l s e ;
)
1 1
Toa s t toast
. <M EN U>
.
1 0
248
Color , Fot
Style - .
.
. 1 0.6.
Red
Regu l a r
G r e e r1
Bold
Blue
Ita l i c
. 1 0.6.
11
.
, , .
,
.
, (
) .
,
. ,
,
.
1 1 . 1 .
- , Adroid
, , ,
.
:
(active wru running) -
. ,
;
u (paused) - ,
.
.
;
(stopped) -
1 1
250
, ,
.
, ,
,
.
,
:
CJ protected void onC reate ( Bundle s avedins tanceState ) ;
CJ protected void onStart ( ) ;
[J protected void onRes tart ( ) ;
[J protected void onRe sume ( ) ;
[J protected void onPaus e ( ) ;
[J protected void onS top ( ) ;
[J protected void onDest roy ( ) .
- ,
,
.
onC reate ( ) ,
.
onPause ( ) ,
.
. :
protected void onPause ( )
super . onPause ( ) ;
. ,
:
CJ
: (enti1e lifetime) -
onCreate ( ) onDe s t roy ( ) .
. onC reate ( )
onDe s t roy ( ) ,
,
.
25 1
,
onDe s t roy ( ) ;
onCreate
( )
D (visi/e lifetime) -
( ) onStop ( ) .
,
.
, ,
;
ons tart
D (foregrond lifetime) -
onResume ( ) onPause ( ) .
.
active pased,
(
), ,
, .
1 1 . 1 ,
. ,
,
. ,
,
, .
:
D onC reate ( ) - .
-
, . .
- undle,
( );
D onRe s t a rt ( ) - ,
.
onStart ( ) ;
D onS tart ( ) - ,
.
onRe sume ( ) , ,
onstop ( ) , ;
D onResume ( ) - ,
.
onPause ( ) ;
1 1
252
. _____L___-,
"Back" o Restart()
. 1 1 . 1 .
onPause ( ) - ,
. ,
,
n n
. n onRe sume ( ) ,
n n, ons top ( J ,
n ;
253
onStop ( ) - ,
. ,
( ),
.
onRe s t a rt ( ) , ,
, onDe s t roy ( )
;
,
onDe s t roy ( ) - . -
, .
,
finish ( ) ,
.
i s Fini shing ( ) .
,
.
1 1 . 1 . 1 .
n n,
,
.
,
onSave instance State ( ) .
Android , . .
onPause ( ) .
n undle,
, - .
, undle
onCreate ( ) onRes tore i nstance State ( ) ,
n ons tart ( ) ,
.
onPause ( ) , ,
on Save ins t ance State ( ) onRes tore i ns t ance State ( )
.
. , Android onSave i nstance S tate ( ) ,
,
,
( <>).
.
254
1 1
onSave instanceState ( ) ,
. on Pause ( ) .
1 1 . 1 . 2 .
,
.
. < >,
,
, .
.
, .
( )
.
,
Activity,
. ,
.
, ,
.
1 1 . 2 .
(
, ) ,
. -
.
, Intent,
, .
Intent Context . startActi v i ty ( )
Act i vi ty . s t a rtAct i vi tyForRe sul t ( ) ,
- . Intent
Act i vi ty . setResul t ( ) ,
,
Android ,
, .
255
Intent - . ,
, , ,
. , Intent
, Adroid, -
,
. , Intent
:
. ,
. - ComponentNarne -
(, "MainAct ivi t y " )
,
( , " com . sarnpl es . application " ) .
. , Intent
. , Adroid
Intent,
.
setComponent ( ) , setClass ( )
setClas sNarne ( )
getComponent ( ) ;
. , . Intent
, :
ACT ION_CALL -
ACT ION_EDIT -
ACT ION_AIN -
,
;
_
.
,
, ,
.
. , ,
,. com . sarnples . proj ect . u s_
ACT ION . Intent setAc t i on ( )
getAct ion ( ) ;
u . U RI M I M E .
.
15;
. ,
, .
Intent
256
1 1
.
CATEGORY, :
Intent
CATEGORY_BROWSALE -
, ,
;
Screen, ,
, < >;
Application Launcher
(
).
CATEGORY_ -
CATEGORY_LAUNCHER -
Intent
addCategory ( )
removeCategory ( )
getCategories ( )
Intent;
- , ;
-
Intent;
D . - ,
, .
, ACT ION_T IMEZONE_CANGED t ime - z one,
, ACT I ON_HEADSET_PLUG
s tate, , ,
name . Intent
put
( )
get . ( ) .
undle
D . , (,
)
, (,
).
Intent .
Android , Q ,
Intent
( ,
).
257
1 1 . 3 .
:
' - (
, , );
- (
- ). ,
.
,
,
.
. (
. 1 1. 5) . n
Andid
( ,
),
.
1 1 . 4 .
,
,
,
s t a rtActi vi ty ( Intent ) .
rntent, , .
, .
, ,
.
, : .
, s tartActivityForResult ( Intent ,
int ) , .
onAct ivityRe sult ( int , int , I ntent ) ,
.
1 1
pr ivate s t a t i c final int I DM ADD
Intent intent
new Intent ( ) ;
101 ;
258
1 1
, setResult ( int ) ,
.
,
RESULT_CANCELED, RESULT_
RESULT FIRST USER.
private EditText mName ;
private EditText mPhone ;
I ntent i ntent
new I ntent ( ) ;
1 1
.
intent . putExtra ( ContactLis tAc t i vi ty . N ,
mName . getText ( ) . toString ( ) ) ;
intent . putExtra ( ContactListAc t ivity . PHON E ,
mPhone . getText ( ) . toString ( ) ) ;
setResul t ( RE SULT_OK , intent ) ;
finish ( ) ;
,
Intent, .
Act i vity . onAct i vityRe sult ( ) , ,
:
protected v o i d onActivityResult (
int reques t Code , int resultCode , Intent data )
RESULT_OK )
data . getExt ras ( ) ;
259
,
RE SULT_CANCELE D.
. Ecl ipse
Create New Proj ect :
D Project name - ContactEdi tor;
D Application name - Contacts Sample;
D Package name -
;
D
NewContactActivity -
Edi tContactActivity -
NewContactAc tivity Edi tContactAct ivity,
1 1 . 1 . ,
android : name android : l abe l .
1 1
260
<intent - f i lter>
<act ion android : narne= " android . intent . action . AI N "
main .xm l , 7 . 1 9 . 7. 5.
.
,
, res/values/strings.xm l,
1 1 .2 . ,
1 6.
< ?xrnl ve r s i on= " . 0 " encoding= " ut f-8 " ? >
<resources>
< s t r ing narne=" app_narne " >Contacts s arnple < / s tring>
< s t r ing narne="btn_crea t e " >Create < / s t r ing>
< s t ring narne= "btn_save " > S ave< / s t r ing>
< s t r ing narne="btn_cance l " >Cance l < / s t ring>
< s t ring narne= " f ield_narne " >Narne : < / s t r ing>
< s t r ing narne= " field_phone " >Phone : < / st ring>
< s t ring narne= " t i t l e_add" >Add new Contact < / s tr ing>
< s t ring narne= " t i t l e_edi t " >Edit Contact < / s t r ing>
< s t r ing narne= " t i t l e de let e " > Delete this Contact ? < / s t ring>
< s t r ing narne="menu_add " >Add< / s t ring>
< s t r ing narne="menu_edit " >Edi t< / str ing>
< s t r ing narne="menu_de lete " > Delete< / s t r ing>
<st ring narne= " toast_not i f y " > Please select Contact ! < / s t ring>
< / res ources>
261
NewContactActivity EditContactActivity
, n
n n
. n ,
n n.
n 1 1 .3 .
1 1
262
<EditText
android : l ayout_he ight=" wrap_conten t "
android : l ayout_width= " f i l l_parent "
android : id= " @ + id/edit_phone " / >
< / Linea rLayout >
<LinearLayout
android : la yout_he ight= "wrap_content "
android : layout_width= " fi l l_parent "
android : or ientation= " hori zonta l "
android : padding= " l Opx " >
<Button
androi d : l ayout_he ight= " wrap content "
android : l ayout_width= " f i l l _parent "
android : l ayout_we ight= "
android : id= " @ + id/but ton save " / >
<Button
android : layout_he ight=" wrap_content "
android : layout_width= " f i l l_pa rent "
android : layout_we i ght= " l "
android : text= " @ s t r ing/btn_cance l "
android : id= " @ +id/but ton cancel " / >
< / Linea rLayout >
< / Linea rLayout >
n n , 7 ( 15
"" n ,
n ).
contac t i t em, n
.
Contacti tem n
1 1 .4 .
lL;
263
pulic Contacti tem ( St ring n , Str ing phone )
supe r ( ) ;
supe r . put ( NAE , n ) ;
supe r . put ( PHONE, phone ) ;
ContactListAc t ivity
.
contactListAct ivity n 1 1 . 5 .
264
1 1
101;
102 ;
103 ;
-1 ;
1 1
f i l l Contacts ( ) ;
updateLi st ( ) ;
{ R . id . name , R . id . phone } ) ;
setListAdapte r (mAdapte r ) ;
@ Ove r ride
pu i c boo 1ean onC reateOptionsMenu ( Menu menu )
menu . add ( Menu . NONE , I DM_ADD , Menu . NONE , R . s t r ing . menu_add )
. s e t i con ( R . drawale . i c_menu_add )
. setA1phabet i cShortcut ( ' a ' ) ;
menu . add ( Menu . NONE , I DM_EDIT , Menu . NONE , R . s t r ing . menu_edi t )
. s e t i con ( R . drawale . i c_menu_edi t )
. setA1phabeti cShortcut ( ' e ' ) ;
menu . add ( Menu . NONE , I DM_DELETE , Menu . NONE , R . s t r ing . menu_de 1ete )
. se t i con ( R . drawale . i c_menu_de 1ete )
. setA1phabet i cSho rtcut ( ' d ' ) ;
return ( supe r . onCreateOptionsMenu (menu ) ) ;
@Ove r ride
pul i c boo1ean onOpt i ons i temS e 1ected ( Menui tem i tem)
mid
265
swi tch ( item . get i t emid ( ) )
case I DM_ADD :
new Intent ( ) ;
Intent intent
intent . setClas s ( this , NewContactActivity . c l a s s ) ;
1 1
sta rtActivi tyForResul t ( intent , I DM_ADD ) ;
break ;
case I DM EDIT :
if (mid >= 0 )
Contactitem mitem
Intent intent
new Intent ( ) ;
intent . putExtra ( Contac t i t em . NAE , mi tem . getName ( ) ) ;
intent . putExtra ( Contact i tem . PHONE , mitem . getPhone ( ) ) ;
intent . setClass ( this , Edi tContactActivi t y . c l a s s ) ;
1 1
sta rtActivit yFo rResult ( intent , I DM_E D IT ) ;
else
Toast . makeText (
this , R . st ring . toast_notify, Toast . LENGTH_SHORT )
. show ( ) ;
brea k ;
case I DM DELETE :
if
(mid >= 0 )
AlertDialog . Builde r bu i lder
new AlertDia log . Builde r ( thi s ) ;
Contact item mitem
mLi s t . get ( ( int ) mid) ;
builde r . setMes s age ( R . s t ring . t i t l e_de lete + " \n "
+ mi tem . getName ( ) + " \n " + mitem . getPhone ( ) ) ;
=
266
1 1
bui1de r . setCance1ale ( fa 1 s e ) ;
bu i 1de r . show ( ) ;
e1se
Toast . makeText ( this , R . s t r ing . toast_not i fy ,
Toast . LENGTH SHORT )
. show ( ) ;
updateLi st ( ) ;
break ;
return ( supe r . onOptions i temSe 1ected ( i tem) ) ;
RESULT_OK )
data . getExt ras ( ) ;
switch ( requestCode )
case I DM ADD :
mLi s t . add ( new Conta c t i tem (
ext ras . getS t r ing ( Contact i tem . NAE ) ,
ext ra s . getS t r ing ( Contac t i tem . PHONE ) ) ) ;
bre a k ;
c a s e I DM EDIT :
mLi s t . s et ( ( int ) mid, new Contac t i tem (
extras . getS t r ing ( Contacti tem . NAE ) ,
ext ras . getS t r ing ( Conta c t i tem . PHONE ) ) ) ;
bre a k ;
updateLi st ( ) ;
" 1 61 8 63 1 8 7 " ) ) ;
mLi s t . add ( new Contact i tem ( "Michae1 Ful le r " , " 8 9 6 4 4 3 65 8 " ) ) ;
mLi s t . add ( new Contacti tem ( " Emma Greenman " ,
mLi s t . add ( new Contacti tem ( " Joshua Har r i s on " , " 7 5 92 8 5 0 8 6 " ) ) ;
mLi s t . add ( new Contacti tem ( "Madison Johnson " , " 9 5 0 2 8 5 7 7 7 " ) ) ;
267
rnL i s t . add ( new Contact i t ern ( "Matthew Cotrnan " ,
rnL i s t . add ( new Contact i tern ( " Olivia Laws on" ,
" 68 7 6 9 9 9 9 9 " ) ) ;
" 1 61 8 6 3 1 8 7 " ) ) ;
" 5 4 6 5 9 9 64 5 " ) ) ;
rnList . add ( new Contact i tern ( " Dani e 1 Honeyrnan " , " 8 7 6 5 4 5 6 4 4 " ) ) ;
.
" 907868756 " ) ) ;
rnList . add ( new Cont act i tern ( "W i 1 1 i arn Pat terson " , " 6 8 7 6 9 9 6 9 3 " ) ) ;
rnList . add ( new Contact i t ern ( " Joseph Godwin " ,
rnList . add ( new Contact i tern ( " Sarnantha Bush" , " 9 0 7 8 6 5 6 4 5 " ) ) ;
rnL i s t . add ( new Contact itern ( " Chr i s t ophe r Gat ernan " ,
Newcont actActivity
. NewContactActivity
1 1 .6 .
268
1 1
btnOK . setOnCl ickLis tener ( new View . OnC l i ckLi stene r { )
@ Ove rride
pu l i c vo id onCl i c k ( View v )
Intent intent
new Intent { ) ;
i n tent . putExtra ( Contactitern . NAE ,
rnNarne . getText ( ) . to S t r ing { ) ) ;
intent . putExt ra ( Contact i tern . PHONE ,
rnPhone . getText ( ) . toString ( ) ) ;
setRe sult ( RE SULT_OK , intent ) ;
finish ( ) ;
setResult ( RESULT_CANCELE D ) ;
finish ( ) ;
EditContactAc t ivity
. ,
NewContactActivity,
. EditContactAct ivity
1 1 .7.
269
@ Ove rride
pu l i c void onCreate ( Bundle savedins tance State )
supe r . onCreate ( savedinstanceState ) ;
set ContentView ( R . layout . contact_i tem) ;
mName
mPhone
setResult ( RESULT_CANCELE D ) ;
finish ( ) ;
(. 1 1 .2).
, ( )
(. 1 1 .3 ) .
270
. 1 1 . 2.
. 1 1 . 3 .
11
271
RESULT_CANCELE D)
,
.
( RE SULT_o
1 1 . 5 .
, .
.
,
. ,
.
rntent : , , .
, .
< intent
f i l ter>. , ,
:
<activity
android : name= " . ContactLi stActivity"
android : l abe l= " @ st ring/ app_name " >
< intent - f i lter>
<act i on andro id : name=" andro id . intent . action . AIN "
<category android : name=" android . i ntent . category . LAUNCHER"
< / intent - f i lter>
< / act ivity>
<act ion> ,
. <catego ry>
Application Launcher,
, .
0
- I ntent F i l t e r . , . .
A droid ,
,
.
<intent- f i l t e r > ,
272
1 1
, ,
Intent.
. , ,
. ,
- , .
, . . , ,
,
.
, .
contactListAct ivity
Contacts,
ContactLi stAct ivity, 1 1 . 8 .
<activity
android : name= " . ContactLis tActivity"
android : labe l= " @ st ring / app_name " >
< intent - f i lter>
<act i on andro id : name=" android . intent . act i on . AIN "
<category android : name= " android . intent . category . LAUNCHER"
< / intent - f i l te r>
< intent - f i l te r>
<act i on andro id : name= " c o . samples . contact . VIEW_CONTACT S "
<category android : name= " android . intent . category . DEFAULT "
< / intent- f i lter>
< / activi ty>
273
.
1 1 .9 .
I II 1 1 . 1 .
2 74
1 1
Cont a c t s
.
Cont a c tEdi tor (. 1 1 .4 )
.
. 1 1 .4 .
12
Adroid .
, ,
.
, -
.
.
.
1 2 . 1 .
(. 1 1)
, ,
. :
vo id onCreate ( ) ;
vbid onStart ( Intent intent ) ;
void onDe st roy ( )
,
.
:
Jf :
onC reate ( ) onDe s t roy ( )
, oncreate ( )
onDe s t roy ( ) ;
s t a r t S e rvice ( ) .
:JICUI :J1
onS tart ( )
rntent,
-
276
12
Context . startService ( ) ,
Cont ext . stopServi ce ( )
, S e rvice . stopSe l f ( )
Service . s topSel fResult ( ) .
.
Context . bindservice ( )
Context . un indS e rvice ( ) . ,
indService ( ) .
onCreate ( )
,
Context . startService ( )
. 1 2 . 1 . ,
startService ( ) ,
bindS e rvice ( ) , , , ,
onReind()
. 1 2. 1 .
277
,
, . . onind ( )
onUn ind ( )
,
:
CJ IBinde r onBind ( Intent intent ) ;
CJ boolean onUnind ( Intent intent ) ;
CJ void onRebind ( Intent intent ) .
onind ( ) Intent,
indService ( ) , onUnind ( ) Intent, unindService ( ) .
, onind ( ) ,
, .
onReind ( ) onUnind ( ) ,
.
1 2 . 2 .
, ,
S e rvice.
onBind ( ) oncreate ( ) , :
import android . app . S e rvice ;
import andro id . content . I ntent ;
import android . os . I Binde r ;
pu l i c class MyService extends S e rv i ce
@ Ove r ride
pu l i c IBinder onBind ( I ntent intent )
1 1
@ Ove r ride
pul i c void onCreate ( )
1 1
onStart ( ) . , -
278
1 2
startService ( ) , ,
:
@ Ove rr ide
pu l i c void onStart ( Intent intent , int s t a r t i d )
1 1 .
,
. < s e rvice>
<application> . < s e rvice>,
,
,
< requ i res -penni s s ion> . ,
MyService :
< ? xrnl ve rs ion= " . 0 " encoding= " ut f- 8 " ? >
<mani fe s t xrnlns : android= "http : / / schemas . androi d . com/apk/ res / android "
<appl ication
android : icon= " @ drawa l e / i con "
android : labe l= " @ s t ring/app_name " >
< s e rvice
android : enaled= " t rue "
android : name= " . MyService " >
< / s e rvi ce>
< / app l icat i on>
< /man i fe s t >
,
s ta r t S e rvice ( ) :
D ;
D .
MyService
, .
:
s tartS ervice ( new Intent (MyService . SERVICE_ACT I ON ) ) ;
279
,
, MyS e rvice, :
SERVICE_ACT I ON,
,
SERV I CE_ACT ION.
, ,
Securi tyException.
, stopS e rvice ( )
, :
s t opService ( new Intent ( thi s , s e rvice . getClass ( ) ) ) ;
startService ( ) , ,
onstart ( ) .
startService ( ) , ,
s t opService ( )
, startService ( ) .
-.
,
. .
Create New Project
:
Project name - S e rviceLaunche r;
Application name - S e rvice Launche r Sample;
Package name - . samples . s e rvicelaunch;
Create Activity - LaunchAc t ivi ty.
PlayService.
sample.mp res/raw/ (
).
1 2. 1 .
280
12
return nul l ;
@OVe rride
pu l i c void onCreate ( )
@OVe rride
pu l i c void onStart ( Intent intent ,
Toas t . ma keText ( this ,
Toast . LENGTH_SHORT ) . show ( ) ;
int startid)
mPlaye r . s tart ( ) ;
@OVe rride
pu l i c void onDe s t roy ( )
Toast . ma keText ( thi s ,
Toast . LENGTH_SHORT ) . show ( ) ;
{
" Se rvice Stopped" ,
mPlayer . s top ( ) ;
( 1 2.2).
< ?xml ve rsion= " l . O " encoding= " utf-8 " ?>
<manifest xmlns : andro id= " ht tp : / / s chemas . android . com/apk/res / android"
package= " c om . samples . s e rvi ce l aunch "
android : ve r s i onCode= " l "
android : ve r s ionName= " l . O " >
<appl icat ion
android : icon= " @ drawale/ icon "
android : label= " @ s t ring /app_name " >
281
<activity
android : name= " . LaunchActivity"
android : label= " @ st ring / app_name " >
<intent-fi lter>
<action androi d : name= " android . intent . action . AI N " / >
, Start Player
Stop Player ( 1 2.3).
startService ( ) stopService ( ) .
1 2 .4 .
282
1 2
1 1
btnStart . s etOnClickLis tener ( new View . OnCl i c kL i s tene r ( )
@Ove r r i de
pu l i c vo id onCl i c k (View v )
1 1
startService (
new I ntent ( LaunchActivi ty . thi s , PlayService . class ) ) ;
1 1
btnS top . s etOnC l i ckLis tene r ( new View . OnCl i c kLis tener ( )
@ Ove rride
pu l i c void onCl i c k (View v )
stopSe rvice (
new Intent ( LaunchAc t ivity . th i s , Pl ayService . clas s ) ) ;
. 1 2 .2.
,
283
:
, .
. 1 2. 2 .
13
, -
.
.
B roadcastRecei ver
,
s endroadcast ( ) .
BroadcastRecei ver
Context . regi s t e rReceive r ( ) ,
< rece ive r> .
,
:
(Normal broadcasts) -
286
1 3
, , ,
Context . s t a r tAct i vi ty ( ) .
Broadca s tReceiver
, sta rtAc t i vi t y ( ) . ,
Broadcas tReceiver,
. :
,
,
.
, ,
, .
Broadcas tRece iver (
< recei ver> )
.
1 3 . 1 .
:
void onRece ive ( Context curContext , Intent broadcas tMs g )
,
Android onReceiv e ( )
Intent, .
. ,
roadca stReceiver, . .
onRece i ve ( ) ,
Android, ,
.
onReceive ( ) ,
, roadcas tRece iver .
. , ,
, ,
, .
,
. onRecei ve ( )
287
, , ,
, Android (
),
.
,
roadcas tRece ive r. n
Not i ficationanager.
Context . s tartService ( ) ,
.
, onReceiv e ( )
Broadcas tReceiver
,
.
1 3 . 2 .
Adroid
, , ,
, , ,
SMS .
,
, .
,
I ntent.
,
:
,
. RECEIVE_oo_coPLETED;
<Camera> ;
0 ACT ION_DATE_CANGE D ACT ION_I_CANGED -
;
0 ACT ION_SCREEN_OFF ACT ION_SCREEN_ON - ,
;
ACT ION_T IEZONE_CANGED -
288
1 3
1 3 . 3 .
,
.
11 12
.
sendroadcast ( ) .
, ,
,
.
.
; ,
,
, ,
.
, (
)
.
1 3 . 3 . 1 .
.
, ,
sendroadcast ( ) , .
action, data category (, )
,
"" .
,
, ,
.
, ,
Java. ,
( . ) :
pr ivate static S t r ing ACT ION
,
ACT I ON -
289
, Intent,
sendroadca s t ( ) ,
Intent :
pri vate s ta t i c final St ring = " type " ;
private static final int I D_ACTION_PLAY = ;
Intent intent = new Intent ( ACT ION ) ;
intent . putExtra ( TYPE ,
I D_ACTI ON_PLAY ) ;
s endroadca s t ( intent ) ;
1 3. 3 . 2 .
,
.
, ,
, .
<applicat ion> <receiver>,
.
<rece ive r> <intent - f i lter>,
. ,
(. ) :
<appl ication
< receiver android : name= " . Pl aye rRece ive r " >
<intent - f i lter>
<act i on android : name= " com . samples . media . PLAYER" />
< / intent- filter>
< / rece ive r>
< / appl i cat i on>
, ,
Broadca s tRe cei ver, onRecei ve ( )
, :
pu l i c clas s PlayerReceive r extends BroadcastRece iver {
priva te static final St ring = " t ype " ;
pr ivate static final int ID ACTION PLAY = 0 ;
private static final int I D ACTION STOP = 1 ;
290
1 3
@ Ove r r ide
pu l i c void onRece ive ( Context context , Intent intent )
int type
switch ( type )
case I D ACT ION PLAY :
/ 1
context . s tart Service ( new Intent ( context , PlayService . c las s ) ) ;
brea k ;
onRece ive ( )
.
n n
.
1 3 . 3 . 3 . -
n
n ,
, ,
LaunchActivity, ,
. Ecl ipse
n:
Proj ect name - BroadcastRece i ve r;
Package name - . sarnples . broadcast receive r .
Playe rReceiver PlayService,
1 3 . 1
29 1
<service
android : enaled= " t rue "
androi d : name= " . PlayS e rvice " >
< / s e rvice>
< receive r android : name= " . Pl ayerReceive r " >
<intent - f i lter>
<act i on android : name=" corn . s amples . rnedia . PLAYER"
< / intent- f i l t e r >
< / receiver>
< / appl icat i on>
<us e s - s dk android : rninSdkVers i on= " "
< /rnan i fe s t >
,
, 1 3 .2 .
roadcastReceiver,
1;
@ Ove rride
pu l i c void onRece ive ( Context context , Intent intent )
int type
swi t ch ( type )
1 3
292
c a s e I D ACT ION STOP :
Toas t . rnakeText ( context ,
1 2 . 1 .
Playservice,
12,
- .
,
.
1 3 . 3 .4. n -
- ,
. Ecl ipse
:
D Project name - Broadcast Sende r;
D Application name - Mus i c
D Package name -
Launche r Sample;
1 2 .3 . LaunchActivity
, ,
.
LaunchActivity 1 3 .3 .
293
" type" ;
1;
@ Ove r r ide
pulic void onCreate ( Bundle s avedinstanceStat e )
supe r . onCreate ( savedinstance State ) ;
setContentView ( R . layout . rnain ) ;
( Button )
( Button )
1 1
Intent intent = new Intent (ACT ION ) ;
intent . putExt ra ( TYPE ,
I D_ACT ION_PLAY ) ;
s endroadcast ( intent ) ;
1 1
Intent intent = new Intent ( ACTION ) ;
intent . putExt ra ( TYPE ,
I D_ACTION_STOP) ;
sendroadcast ( intent ) ;
, 12 ( . . 1 2.2),
.
14
Adroid n
:
nn (Prefereces);
nn -
n n.
.
1 4 . 1 .
Adroid n
(n, SD-).
n .
n - Adroid n
Jv-n. Adroid n n ,
n n j ava. io (. 1 4 . 1 ). n java.io
n n n
.
,
( ) n n .
Jv- Fileinput Stream. n,
:
296
14
11
Buf fe redReade r reade r
Str ing s t r ;
Str ingBuffer buffer
new S t r i ngBuffer ( ) ;
1 1
whi l e ( ( s t r
reade r . readLine ( ) )
nul l )
inStrearn . close ( ) ;
O bj e c t
l n p u t S tre a m
O u tp u tS tre a m
Reader
1
1
1
F i le l n p u tS tre a m
F il e O u tp u tS tre a m
l n p u tS tr e a m R e a d e r
uffe r e d R e a d e r
S tri n g R e a d e r
W r ite r
O u tp u tS tre a m W rite r
B u ffe re d W rite r
S tr i n g W rite r
1
1
1
1
1
. 1 4. 1 . - Android
, context .
.
FileOutputSt rearn.
,
. file.txt :
openFi leOutput ( ) ,
S t r ing data ;
Output St rearn out St rearn
OutputSt rearnWr iter sw
sw . wr ite ( data ) ;
sw . close ( ) ;
openFileOutput ( fi l e . txt , 0 ) ;
new OutputStrearnWr iter ( outStrearn) ;
29 7
, n n
n n, n
n res/raw/, Resource s . openRawResource ( ) .
Input st rearn, n
. n ,
close ( ) .
0
openFi l e i nput ( ) openFileOutput ( )
( , pathlfileslfi l e . txt) , .
n n, n
, n Editext
n .
Ecl i pse n, n n New Android Proj ect:
D Proj ect name - Cont actEditor;
D Application name - Read-Write File Sample;
L- main.xml,
Editext, n '? 1 4. 1 .
n EditorAct ivity n
n - Open, Save Exit. openFi l e ( )
saveFi le ( ) n , n
. EditorAct ivi t y n 1 4 .2.
298
1 4
101;
102 ;
103 ;
@ Ove rride
pu l i c boolean onC reateOpt ionsMenu ( Menu menu )
menu . add (Menu . NONE , I DM_OPEN , Menu . NONE , " Open " )
. se t i con ( R . drawale . i c_menu_open)
. s etAlphabet icShortcut ( ' o ' ) ;
menu . add ( Menu . NONE , I DM_SAVE , Menu . NONE , " Save " )
. se t i con ( R . drawale . i c_menu_save )
. setAlphabet i c Shortcut ( ' s ' ) ;
menu . add (Menu . NONE , I DM_EXI T , Menu . NONE , "Exit " )
. s e t i con ( R . drawale . i c_menu_ex i t )
. setAlphabeticShortcut ( ' x ' ) ;
return ( supe r . onCreateOpti onsMenu (menu ) ) ;
@ Ove rride
pu l i c boolean onOpt i ons i temSelected (Menu i tem item)
swi tch ( i tem . get itemi d ( ) )
case I DM OPEN :
openFi le ( FI LENAE ) ;
bre a k ;
c a s e I DM SAVE :
saveFi l e ( FI LENAE ) ;
bre a k ;
c a s e I DM EXIT :
finish ( ) ;
brea k ;
defaul t :
return fa l s e ;
return t rue ;
nul l )
Input S t reamReade r s r
new Input StreamReade r ( inSt ream ) ;
Buffe redReade r reade r
new Bu fferedReade r ( s r ) ;
St r ing s t r ;
S t r ingBuffer buffer
whi le ( ( s t r
new StringBu f fe r ( ) ;
reade r . readLine ( ) )
nul l )
catch ( Throwale t )
Toa s t . makeText ( getApp l i cationContext ( ) ,
" Except ion : " + t . toString ( ) , Toa s t . LENGTH_LONG )
299
14
300
. show ( ) ;
0) ;
sw . w r i t e (mEdi t . getText ( ) . t o S t r i ng ( ) ) ;
sw . c l o s e ( ) ;
catch
( Throwa le t )
t . toS t ring ( ) ,
Toast . LENGTH_LONG )
. show ( ) ;
, ,
. 1 4.2.
1 234567890qwerty
. 1 4.2.
n ,
. -
30 1
, ,
, .
1 4. 2 .
-
- .
, ,
, ,
.
( Application
Launcher - Settings).
. 1 4 .3 .
. 1 4. 3 .
302
14
.
:
CJ CheckBox P re ference;
CJ Edi tText Prefe rence;
CJ L i s t P refe rence ;
CJ Ringtone Preference .
O bj e c t
------,
P re fe re n c e
.-----------,
P re fe re n c e G ro u p
1
D i a l o g refe re n c e
1
C h e c k B o x P re f e r e n c e
R i n g to n e P re fe re n c e
P re fe re n c e C a te g o ry
refe re n c e S c re e n
d it e x t P refe r e n
L i s t P refe re n c e
. 1 4.4.
.
,
.
,
Act i vi ty . get Prefe rences ( ) .
( -,
).
1 4. 2. 1 .
Adroid
-. ,
S t ring ( boo lean,
int . .).
303
,
.
.
:
( ) - ,
;
( ) - ,
;
D getSharedPrefe rences
Android.
( ) - Prefe rencesManage r,
,
, .
get Sha redPre ferences ( )
- getPrefe rence s ( ) . getDe faul t Sha redPrefe rences ( )
Context (, ).
Sha redPreference s,
:
D getBoolean ( S t r i ng key , boolean de fVa lue ) ;
D getFloat ( St r ing key , float de fVa lue ) ;
D getint ( St r ing key, int de fVa lue ) ;
D getLong ( S t ring key , l ong de fValue ) ;
D getSt r ing ( S t ring ke y, St ring de fVa lue ) .
get . ( ) - ,
.
, boo lean:
Sha redPre ferences prefs
Pre fe renceManage r . getDefault Sha redPrefe rences ( th i s ) ;
boolean val
,
- ,
, .
1 4. 2 . 2 . CheckBoxPreference
ln- Sha redPrefe rence s .
.
1 4
304
Create New
Project :
D
Sample;
main.xml ,
( . 1 4. 1 ).
.
,
,
res/values/strings.xm l . OpenMode.
, .
strigs.xml ,
, 1 4 . 3 .
< ?xml ve r s i on= " . 0 " encodi ng= "utf-8 " ? >
< re s ource s >
< s t r i ng name= " app_name " > Preferences Sampl e < / s t ring>
< s t ring name= "pr_openmode " >Openode < / s t ring>
< s t ring name="pr_col o r " >Color< / s t ring>
< s t ring name="pr_color_lack" >ColorBlack< / s t r ing>
< s t r ing name="pr_color_red " >ColorRed< / s t ring>
< s t r ing name="pr_color_green " >ColorGreen< / s t ring>
< s t ring name="pr_color_lue " >Col orB l ue < / s t ring>
< s t r ing name= "pr_s i z e " > S i z e < / s t ring>
< s t ring name= "pr_s t yl e " > S t yle< / s t ring>
< s tring name="pr_style_regul a r " >StyleRegular< / s t ring>
<s tring name="pr_style_bold" > StyleBold< / s t ring>
< s t ring name="pr_style_i t a l i c " > Style i t a l i c < / s t ring>
< s t ring name="pr_tone " >Tone< / s t r ing>
< / resources >
,
, ,
. preferences.xml 1 4 .4 .
305
, L-,
.
onC reate ( )
addPrefe rences FromResource ( ) XML
pecypc ( - preferences.xml),
. PreferencesAct ivity
1 4. 5 .
1 1
addPrefe rences FromRe source ( R . xml . pre ferences ) ;
( 1 4 .6).
PreferencesActivity
306
14
<ac t ivi t y
android : name= " . EditorActivity"
android : l abe l= " @ s t ring/ app_name " >
< intent - f i lter>
<action android : name= " android . intent . action . AIN "
<category android : name= " android . intent . category . IAUNCER"
< / i ntent- filter>
< /a c t ivi ty>
<activity
android : name= " . Pre ferencesEdit "
android : l abe l= " @ s t ring/ app_name " >
< /act ivity>
< / appl ication>
EditorActivity n
n ( . 1 4.2). n
Settings, nn.
onOptions i temSelected ( ) switch
case nn:
c a s e I DM PRE F :
I ntent intent
new Intent ( ) ;
n n onResne ( ) .
Android n n,
. n onRe sne ( )
:
Sha redPre fe rence s prefs
P re fe renceManager . getDe faul t Sha redPre ferences ( thi s ) ;
1 1 CheckoxPre fe rence
if (prefs . getBoolean ( ge t S t r ing ( R . st r ing . pr_openmode ) , fa l se ) )
openFi le ( FI LENAE ) ;
getoolean ( ) ( false )
n, n
.
1 4 .7.
EditorActivity
101;
102 ;
103 ;
104 ;
@ Ove rride
pu l i c void onRe surne ( )
supe r . onRe surne ( ) ;
307
14
308
SharedPrefe rence s prefs
Prefe renceManager . getDe faultSha redPre fe rence s ( thi s ) ;
1 1 CheckBoxPreference
@ Ove r ride
pu l i c boolean onCreateOptionsMenu ( Menu menu )
menu . add (Menu . NONE , I DM_OPEN , Menu . NONE , " Open " )
. s e t i con ( R . drawale . i c_menu_open )
. setAlphabeticShortcut ( ' o ' ) ;
menu . add (Menu . NONE , I DM_SAVE , Menu . NONE , " S ave " )
. s e t i con ( R . drawale . i c_menu_save )
. setAlphabetic Shortcut ( ' s ' ) ;
menu . add (Menu . NONE , I DM_PRE F, Menu . NONE , " Sett ings " )
. se t i con ( R . drawale . i c_menu_pre ference s )
. s etAlphabet i c Shortcut ( ' t ' ) ;
menu . add (Menu . NONE , I DM_EXI T , Menu . NONE , " Exit " )
. se t i con ( R . drawale . i c_menu_ex i t )
. s etAlphabeticShortcut ( ' x ' ) ;
return ( supe r . onCreateOpt ionsMenu (menu ) ) ;
@ Ove rride
pul i c boolean onOpt ions i t emSelected (Menuitem item)
swi t ch ( item . geti temi d ( ) )
ca s e I DM OPEN :
openFi le ( FI LENAE ) ;
bre a k ;
c a s e I DM SAVE :
saveFile ( FI LENAE ) ;
break ;
case I DM PREF :
Intent intent
new Intent ( ) ;
de faul t :
return fal s e ;
return t rue ;
nul l )
S t ring s t r ;
S t ringBuffer buffer
nul l )
catch ( Throwale t )
Toas t . makeText ( getApp l i cati onContext ( ) ,
" Except i on : " + t . toString ( ) , Toast . LENGTH_LONG )
. show ( ) ;
309
1 4
310
. s how ( ) ;
.
(. 1 4 .5).
Some text. . .
Sore text...
Sdve
0
Exlt
1 4. 2 . 3 . EditTextPreference
Adro i d
- EditText Prefe rence.
, .
,
-
.
<Edi tTex t P r e fe rence> 1 4. 8 .
31 1
<CheckBoxPreference
android : key= " @ string/pr_ope ode "
android : t i t le= " Open f i l e "
android : summa ry= " To open f i l e at start appl i cation " / >
<Edi tTextPre ference
android : key= " @ s t ring/pr_s i z e "
android : t i t le="Text S i z e "
android : summa r y= " Set text s i ze "
android : de faul tVa lue= " l 4 "
android : di a l ogT itle="Ente r text s i z e ( from 1 0 to 3 2 ) " / >
< / Preference Sc reen>
onRe sume ( )
( 1 4.9).
@ Ove rr ide
pu ic void onResume ( )
supe r . onRe sume ( ) ;
SharedPrefe rences pre fs
Prefe renceManage r . get De fault SharedPrefe rence s ( thi s ) ;
1 1 Chec kBoxPre ference
if ( pre fs . getBoolean ( ge t S t ring ( R . st ring . pr_openmode ) , false ) )
openFi l e ( FI LENAE ) ;
Float . parseFloat (
"20 " ) ) ;
/ 1 EditText
mEdit . se tText S i z e ( fS i z e ) ;
.
(. 1 4 .6).
EditText Prefe rence
, .
,
.
1 4
312
1 4. 2 .4. ListPreference
L i s t P r e fe rence
.
.
,
SharedPrefe rence s .
.
: Regular, Bold, Italic, Bold+Italic.
L i s t Preference arrays.xml
res/values/. 1 4 . 1 .
313
1 4. 1 2 n onRe sume ( ) .
@Ove r ride
pulic void onRe sume ( )
supe r . onRe sume ( ) ;
SharedPrefe rences pre fs
PreferenceManager . getDefaultSharedPre ference s ( thi s ) ;
1 1 CheckoxPre fe rence
if ( pre fs . getBoolean ( ge t S t ring ( R . s t ring . pr_openmode ) , fa l se ) )
openFi le ( FILENAE ) ;
314
1 4
1 1 EditTextPrefe rence
float f S i z e
EditText
mEdi t . s etText S i z e ( fS i z e ) ;
mEdi t . s e tTypeface ( nul l , typeface ) ;
.
TextStyle
(. 1 4.7).
, ,
Cancel. .
Regu l a r
Bold
315
1 4.2.5. RingtonePreference
Ringtone Prefe rence -
.
,
( 1 4 . 1 3 ) .
<Preference S c reen
xmlns : android= " http : / / s cherna s . android . com/ apk/ res / android" >
<CheckoxPrefe rence
android : key= " @ st r ing/pr_openmode "
android : t i t le= "Open f i l e "
android : swnma ry= " T o open f i l e a t start app l i ca t i on " / >
<EditText Prefe rence
android : key= " @ s t ring/pr_s i z e "
android : t i t le= "Text S i z e "
android : swnma ry= " Set text s i ze "
android : de faul tVa lue= " l 4 "
android : di alogT i t l e= " Enter text s i ze ( f rom 1 0 to 3 2 ) " / >
<List Pre ference
android : key= " @ s t ring /pr_styl e "
android : t i t l e = " Text S t y l e "
android : swnmary= " Set t e x t styl e "
android : de faul tValue= " l "
android : ent ries=" @ a r ray/ text_styl e "
android : entryValue s= " @ a r ray/ text_styl e "
android : dialogT i t l e= " Choose text styl e " / >
<Ringtone Prefe rence
android : key= " @ s t r ing/pr_tone "
android : t i t le= "Tone "
android : showDe fault= " t rue "
android : showS i l ent= " t rue "
android : swnmary= " Set tone ( on or o f f ) " / >
< / P referenceSc reen>
1 4
316
Silent.
android : showS i l ent,
t rue,
Defa u l t ri ngtone
. 1 4.8.
1 4. 2 . 6 . PreferenceCategory
,
.
Preferences Framework ,
.
< Pre ferencecat egory> L-
.
,
< P re fe rence sc reen>, L-
<Prefe rencecategory>
<Preferences c reen>,
.
, ,
1 4. 1 4.
31 7
<CheckBoxPrefe rence
android : key= " @ s t ring/pr_openrnode "
android : t itle= " Open f i l e "
android : sUI!U11a ry= " T o open f i l e at s t a r t appl ication " / >
< / Prefe renceCategory>
< P re fe renceCategory android : t it le= "Text p re fe rence s " >
<EditTextPrefe rence
android : key= " @ s t ring /pr_s i z e "
android : t itle="Text S i ze "
android : sUI!U11a r y= " Set text s i z e "
android : de faul tVa lue= " l 4 "
android : dialogTitle="Enter text s i ze ( from 1 0 t o 3 2 ) " / >
<ListPre fe rence
android : ke y= " @ string /pr_styl e "
android : t i t le= " Text Styl e "
android : sUI!U11a r y= " Set text s t yle "
android : de faultValue= " l "
android : entries= " @ a r ray/ text_styl e "
android : entryVa lue s= " @ a r ray/ text_styl e "
android : dialogT i t l e= " Choose text s tyle " / >
< / Pre fe renceCatego ry>
< Prefe renceCategory android : t itle=" Othe r Prefe rences " >
<RingtonePrefe rence
android : key= " @ st r ing/pr_tone "
android : t i tle= " Tone "
android : showDe faul t= " t rue "
android : showS i l ent= " t rue "
android : sUI!U11a r y= " Set tone ( on or o f f ) " / >
< / Pre fe renceCategory>
< / Pre ferenceScreen>
<Prefe rencecategory>
, .
, . 1 4.9.
1 4.2. 7. PreferenceScreen
< P reference Sc reen>
<Pre fe rence sc reen>.
<Pre fe renceScreen> .
<Prefe rence S creen>
n.
1 4
318
. 1 4. 9 . P r e f e renceCategory
preferences.xml
1 4. 1 5 .
319
<CheckBoxPrefe rence
andro i d : key= " @ st ring/pr_color red"
android : t i t le="Red"
android : srunary= " Set red color " / >
<CheckBoxPre ference
android : key= " @ s t r ing/pr_color_green "
android : t it le= "Green"
android : sruna ry= " Set g reen colo r " / >
<CheckBox Prefe rence
andro id : key= " @ st ring/pr_color_lue "
andro id : t it le="Blue "
android : srunary= " Set lue color " / >
< / Prefe renceS c reen>
<Edi tText Prefe rence
android : key= " @ s t ring/pr_s i z e "
android : t i tle= " Text S i z e "
android : sruna ry= " Set text s i z e "
android : de faultValue= " 1 4 "
android : di alogT i t l e= " Enter text s i ze ( from 1 0 t o 3 2 ) " / >
<Li s t Pre ference
android : key= " @ s t r ing /pr_s t y l e "
android : t i tle= " Text S t y l e "
android : sruna ry= " Set text styl e "
android : de faul tVa lue= " l "
android : ent ries= " @ array/ text styl e "
android : ent ryVa lue s= " @ array/ text_styl e "
android : dialogT i t l e= " Choose text style " / >
< / Prefe renceCatego ry>
<Pre ferenceCategory android : t i t l e= " Othe r Prefe rences " >
<Ringtone Prefe rence
android : key= " @ s t ring/pr_tone "
android : t i tle= " Tone "
android : showDe fault=" t rue "
android : showS i l ent= " t rue "
android : srunary= " Set tone ( on or o f f ) '' / >
< / Prefe renceCategory>
< / PreferenceSc reen>
onRe sume ( ) Edi torActi vi ty.
,
.
1 4. 1 6.
onRe sume
( )
1 4
320
@ Override
pul i c void onResume ( )
supe r . onRe sume ( ) ;
SharedPrefe rences prefs
PreferenceManager . getDe faul tSharedPre fe rences ( thi s ) ;
1 1 CheckoxPre fe rence
if ( pre fs . getBoolean ( getSt ring ( R . s t ring . pr_openmode ) , fals e ) )
openFi l e ( FILENAE ) ;
1 1 EditTextPrefe rence
float fS i z e
"20 " ) ) ;
1 1 CheckoxPre ference
11
int color
Co lor . BLACK ;
1 1 L i s t Prefe rence
String regular = prefs . get S t ring ( getSt r ing ( R . st ring . pr_style ) ,
int type face
1 1
32 1
EditText
mEdi t . s e tText S i z e ( fS i z e ) ;
mEdi t . s e tTextColor ( co l o r ) ;
mEdi t . s e t T ype face ( nu l l ,
t ype face ) ;
Txt Color
. 1 4 . 1 .
15
SQLite
-
SQLite
-.
- -
ndrid-,
. - ,
.
,
,
SQLite.
1 5. 1 . SQLite
Android ,
. Adroid
sq l ite ,
, SQL
SQLite.
, SQLite ,
/dataldatalpackage_name/ . SQLite (
, , )
. Android
SQLite.
1 5. 1 . 1 . :
SQLiteOpenHelper
Android SQLit eOpe nHe lper .
SQLit eOpene lper :
324
1 5
( ) - ;
onC reate
onUpgrade
( ) - .
, SQLiteopenHe lper.
,
.
,
. , . .
. ,
Contact:
pul i c static final S t r ing L_N = " contact " ;
pu l i c s t a t i c final S t r ing N
" first_name " ;
pu l i c static final St ring PHONE
"phone " ;
, SQLiteOpenelpe r,
asecolnn s , _I D,
.
ID INTEGER PRIARY AUTOINCREMENT.
AUTOINCREENT .
onCreate ( )
,
:
@ Over ride
pu l i c void onCreate ( SQLiteDatabase d ) {
d . execSQL ( " CREATE L
+ L_N
+
+ COL N +
onUpdate ( )
. onUpgrade ( ) ,
, ( DROP
L ) , onCreate ( )
, , :
@ Override
pul i c vo id onUpgrade ( SQLi teDatabase d , int oldVersion,
{
d . exec SQL ( " DROP L IF EXI ST S
onCreate ( d ) ;
+ L N ) ;
SQLite - t .
325
,
, .
1 5. 1 .2. :
SQLiteDatabase
Android SQLite
SQLit e Databa se. SQLiteDatabase ,
, , .
que ry ( ) :
Cursor que ry ( St r ing tal e , S t ring [ ] columns ,
St ring selection, S t r ing [ ] se lect ionArgs ,
St ring g roupBy, S t r ing having , S t r ing s ortOrde r )
que ry ( )
, ;
tale -
columns -
se lect ionArgs
having -
.
;
nul l
- ;
,
, nu1 1 ;
AVING) .
AVING
so rtOrde r - , ORDER (
ORDER ). nu l l .
Cursor, que ry ( ) ,
.
Cursor
getSt r ing ( ) , get int ( ) getFloat ( ) .
SQLite
insert ( ) :
long insert ( St ring tale , St ring nul l ColumnHack, ContentVa lue s va lues )
insert ( )
, ;
tale -
SQLite
, , -
326
1 5
, ,
nul l ;
( ),
-, -.
,
.
values -
insert ( )
.
_I D
- 1
update ( ) de lete ( ) :
int update ( St ring tale , ContentVa lues values ,
S t ring whe reClaus e , S t ring [ ] whe reArgs )
int de lete ( St ring tale , S t ring whe reClaus e , S t ring [ ] whereArgs )
SQL- WHERE
que ry ( ) .
.
,
.
1 5 . 2 . -
-
, , . - -
Android.
, :
-,
ContentProvide r;
-
,
.
-
- , ,
.
Content Provide r .
- ,
SQLite -
ContentRe s o lve r .
32 7
ContentRe s o lve r
( ) ndrid
ge tContentRe s o l ver
:
ContentRe s o lve r resolver
get ContentRe s o l ve r ( ) ;
ContentRe s o lver
-,
.
Android
, .
Content P rovi der,
.
,
content P rovide r . ,
ContentP rovider,
ContentRe s o lve r .
Cont ent P rovide r
ContentRe s o l ve r .
1 5. 2 . 1 .
- ,
- , -
. _ I D,
.
-
,
.
Cu r s o r, ,
, ,
. ,
. , , , .
1 5. 2.2. U RI
- URI (
u), .
, (
), U RI
. U RI " content :
1 5
328
n -n, n, n
URI . Andro id n URI n,
n. , , URI :
android . p rovide r . CallLog . Ca l l s . CONTENT_URI
URI -n.
ContentRe sol ve r URI n. URI , , ContentResolver
.
URI
, n . 1 5 . 1 .
content://com . s a m ples. contactprovider/contacts/1 33
'----v--1
'---------____..,
. 1 5. 1 .
URI
'--v-i
URI
D - n, , n
-n . n ;
D - URI , -.
n (
), u ;
D - , - , n
. -n
, URI . n
, , URI
, , : contacts /phot os contacts/bi rthday;
D D - . _ n.
, URI n :
content : / /com . samples . contactprovide r/ contacts
1 5 . 3 . -
-, n :
1 . .
2 . Cont entprovide r n
.
3 . -n n.
SQLite -
329
,
:
que ry ( )
contentProvide r,
- ;
insert
( ) - -;
update
( ) - -;
de lete
( ) - -;
getType ( )
- M I M E -.
onCreate ( ) ,
-,
SQLiteDataba s e :
p r ivate SQLiteDatabase d ;
@ Ove rr ide
pu l i c boolean onC reate ( )
d
( new ContactDbHe lper ( getContext ( ) ) ) . getWri tale Databa se { ) ;
return ( d
nul l )
fa l s e
t rue ;
que ry ( )
@ Override
pu l i c Cursor que ry ( U r i uri , S t r ing [ ] proj ect ion ,
S t ring s e l ection, S t r ing [ ] selectionArgs , S t r ing sort )
SQLit eQue ryBuilde r qb
Cont entProvider,
330
1 5
qb . s etTal e s ( ContactDbHelpe r . TALE_NAE ) ;
qb . s et Proj ectionap (mContactMap ) ;
1 1
Cursor
insert
( )
@ Overr ide
pul i c Ur i insert ( Uri url , ContentVa lues i nVa lues )
ContentValues values
1 1
d . insert (
long rowid
insert ( ) - U RI
( ).
update ( )
( ) :
delete
@ Override
pul i c int update ( Uri uri , ContentVa lues values ,
S t r ing whe r e , St ring [ ] whe reArgs )
1 1
d . update (
int retVal
ContactDbHe lpe r . TALE_NAE , va lues , where , whe reArgs ) ;
getContext ( ) . getContentRe s olve r ( ) . noti fyChange ( ur i , nul l ) ;
return retVal ;
SQLite -
33 1
@ Overr ide
pu l i c int de lete ( Uri url , St ring where , S t ring [ ] whe reArgs )
int retVal
n
.
n Content Provide r
ContentRe solver n n,
nn n .
1 5.3.2. -
Android -n,
, <provider>
AndroidManifest.xm l . -n,
, And roid,
n n .
n , n
<provide r> :
<provide r
andro id : name= " . ContactProvide r "
android : autho r i t i e s= " com . sample s . dcontact s . contactprovide r " >
< /provide r>
andro id : name -
n n ContentProvide r.
android : author i t i e s - authority URI,
-n.
<provide r>
n , ,
n, n . .
1 5.4. -
n -n n,
n: URI, n
, n n n n.
332
1 5
,
.
.
1 5.4.1 .
-,
ContentRe solve r . que ry ( ) Activity . managedQue ry ( ) :
Cursor que ry ( Uri uri , S t ring [ ] proj ection,
S t ring selection, S t ring [ ] s e lect ionArgs , Str i ng sortOrde r )
Cursor managedQuery ( Uri uri , St ring [ ] proj ection,
S t ring s e lect ion , S t ring [ ] s e lectionArgs , S t ring sortOrde r )
Cursor. managedQue ry ( )
cursor
.
.
:
- CONTENT_u,
Content Provide r .
URI,
ContentU ris . wi thAppendedid ( ) Uri . wi thAppendedPa th ( ) ,
URI .
ContentU r i s , Uri
;
U RI
, .
- ,
SQL- WERE (
WHERE ). nul l
, U RI .
,
.
. ,
-. -
_ I D,
.
_COUNT.
, SQL
oepaope ORDER . nul l ,
SQLite -
333
.
, SQLiteOpenHelper.
:
1 1 ,
new S t r ing [ ]
Cursor curs or
1 5.4.2.
cursor
. cursor
, Jv- I t e rator.
,
:
( ) - ;
rnoveToFi r s t
rnoveToLa s t
rnoveToNext ( )
rnoveToPrevious ( )
- ;
- ;
get Pos i t i on ( )
( ) - ;
-
, . rnoveToNext ( )
t rue, , false,
;
- .
:
D
i s Fi r s t ( ) ;
i sLast ( )
i s BeforeFi rst ( ) ;
i sAfterLa s t ( ) .
1 5
334
1 5.4. 3 .
-,
- - ContentVa l ue s ,
-,
- .
ContentRe s o lve r . insert ( ) , URI
contentValues .
URI - . . URI -
.
U RI ,
. ,
:
ContentVa lues values
new ContentValues ( 2 ) ;
values . put ( Contact DbHe lpe r . NAE , textName . getText ( ) . toString ( ) ) ;
values . put ( Contact DbHe lpe r . PHONE , text Phone . getText ( ) . toString ( ) ) ;
get ContentRe solve r ( ) . insert ( Contact Provide r . CONTENT_URI , va lues ) ;
curso r . reque ry ( ) ;
1 5.4.4.
,
update ( ) . , :
ContentValues values
va lues . put ( Cont act DbHe lpe r . NAE , textName . getText ( ) . toString ( ) ) ;
values . put ( ContactDbHe lpe r . PHONE , text Phone . getText ( ) . toString ( ) ) ;
getContentResolve r ( ) . update (
ContactProvider . CONTENT_URI , values ,
I D= "
i d , nul l ) ;
1 5.4.5.
,
( ) U RI . , :
ContentResol ve r .
de lete
SQLite -
335
SQL- WHERE,
.
ContentRe sol ve r . de lete ( )
1 5. 5 .
.
1 1 .
,
.
Eclipse Create New Proj ect
:
] Proj ect name - contactEdi tor;
] Application name - Contacts Sample;
] Package name -
. samples . contacteditor;
<provider> -.
1 5 . 1 .
1 5
336
< /activity>
< / appl i cation>
< /man i fe s t >
,
.
Cancel .
n 1 5 .3 .
SQLite - t
337
<Linea rLayout
android : orientation= " hori zonta l "
android : layout_width= " f i l l_parent "
android : layout_he ight= "wrap_content " >
<TextView
android : text= " @ string / f ield_name "
android : l a yout_width= "wrap_content "
android : layout_he i ght="wrap_content "
android : layout_a l i gnPa rentLe ft= " t rue " / >
<EditText
android : i d= " @ + id/name "
android : layout_width= " fi l l_pa rent "
android : layout_he ight= " wrap_content "
android : layout_a l i gnParentRight= " t rue " / >
< /Linea rLayout>
<Linea rLayout
android : o rientation= " ho r i z onta l "
android : layout_width= " f i l l_parent "
android : layout_he i ght=" wrap_content " >
<TextView
android : text= " @ s t r ing / f ield_phone "
android : layout_width= "wrap_content "
android : layout_he i ght="wrap_content "
android : l ayout_a l i gnPa rentLe ft= " t rue " / >
<EditText
android : id= " @ + id/phone "
android : l a yout_width= " fi l l_pa rent "
android : layout_he i ght= "wrap_content "
android : layout_a l i gnPa rentRight= " t rue " / >
< / Linea rLayout >
< / LinearLayout>
strings.xml n n n,
n ( n n
n n
). stri ngs.xml n 1 5 .4.
1 5
338
< s t r ing name= " field_name " >Name : < / s t ring>
< s t r ing name= " field_phone " > Phone : < / s t ring>
< s t ring name= " t it l e_add" >Add new Contact< / s t r ing>
< s t r ing name= " t i t l e_edi t " >Edit Contact < / s t ring>
< s t ring name= " t i t l e de lete " > Delete this Contact ? < / s t r ing>
< s t ring name="menu_add" >Add< / s t r ing>
< s t ring name="menu_edi t " >Edi t < / s t ring>
< s t r ing name= "menu_de l e te " >De lete< / s t r ing>
< s t ri ng name=" toa s t_not i f y " > Please select Contact ! < / s t ring>
< / resources >
"phone " ;
@ Ove rride
pul i c void onCreate ( SQLi teDatabase d )
d . exec SQL ( " CREATE L
+
+ L N
+ N + " ,
"
SQLite -
ContentVa1ues va1ues
va1ues . put ( NAE ,
va1ues . put ( PHONE ,
" 1 6 1 8 63 1 8 7 " ) ;
"8 9 6 4 4 3 65 8 " ) ;
" 68 7 6 9 9 9 9 9 " ) ;
d . in s e rt ( TALE_NAE , N , va 1ues ) ;
va1ues . put ( NAE , "01ivia Lawson " ) ;
va1ues . put ( PHONE ,
" 1 6 1 8 63 1 8 7 " ) ;
" 90 7 8 6 8 7 5 6 " ) ;
339
340
1 5
values . put ( NAE ,
" 68 7 6 9 9 6 93 " ) ;
" 9 6 5 4 67 5 7 5 " ) ;
" 90 7 8 6 5 64 5 " ) ;
@ Ove rride
pu i c void onUpgrade (
SQLiteDatabase d , int oldVers ion, int newVe r s ion)
d . execSQL ( " DROP L I F EX I ST S
+ L_N ) ;
onCreate ( d ) ;
SQLite -
34 1
Uri . parse (
1;
2;
@ Ove rride
pu l i c boo lean onC reate ( )
d
return ( d
nul l )
fa lse
t rue ;
@ Ove rride
pu l i c Cursor que ry ( U ri url , S t ring [ ] proj ection,
String se lection, S t r ing [ ] selectionArgs , S t r ing sort )
1 5
342
S t r ing o rde rBy ;
i f ( TextUt i l s . isEmpty ( so rt ) )
orde rBy = ContactDbHe lpe r . NAE ;
else
sort ;
orderBy
Cu rsor
@ Ove r ride
pu l i c Uri insert ( U ri url , ContentVa lue s inVa lue s )
ContentVa lues va lues
long rowid
d . insert (
@ Ove rride
pu l i c int de lete ( Uri url , S t ring whe re , S t ring [ ] whe reArgs )
int retVal
whereArgs ) ;
+ ur l ) ;
SQLite -
343
@ Ove rride
pu l i c int upda te ( U r i ur1 , ContentVa 1ues va1ue s ,
S t ring whe re , S t r ing [ ] whe reArgs )
int retVa1
@ Ove rride
pu l i c S t ring getType ( U r i uri )
return nu1 1 ;
NewContactActivity
- Add, Edit Delete - ,
NewcontactActivity 1 5 . 7.
101 ;
102 ;
1 5
344
p r ivate Cu rsor mCurso r ;
private Lis tAdapter mAdapter ;
private s t a t i c fina l St ring [ ] mContent
new S t ring [ ]
managedQue ry (
{ R . id . name , R . id . phone } ) ;
s e t L i s tAdapter (mAdapte r ) ;
@ Ove r ride
pu l i c boolean onCreateOpt ionsMenu ( Menu menu )
menu . add ( Menu . NONE , I DM_ADD , Menu . NONE , . R . s t r ing . menu_add )
. s e t l con ( R . drawal e . i c_menu_add )
. s etAlphabet i c S hortcut ( ' a ' ) ;
menu . add (Menu . NONE , I DM_EDIT , Menu . NONE , R . s t r ing . menu_edi t )
. s e t l con ( R . drawal e . i c_menu_edi t )
. s etAlphabe t i cShort cut ( ' e ' ) ;
menu . add (Menu . NONE , I DM_DELETE , Menu . NONE , R . s t ring . menu_de lete )
. s e t i con ( R . drawal e . i c_menu_de lete )
. s etAlphabet i c Shortcut ( ' d ' ) ;
return ( supe r . onCreateOpt i onsMenu (menu ) ) ;
@ Ove rride
pu l i c boo lean onOptions i temSelected (Menultem i tem)
f inal long id
345
SQLite -
case I DM EDIT :
i f ( id > 0 )
Ca l lEdi tContactDialog ( i d ) ;
else
Ca llDeleteContactDia log ( id ) ;
else
Toast . rnakeText ( this , R . s t r ing . toa s t_not i fy ,
Toa s t . LENGTH_SHORT )
. show ( ) ;
brea k ;
return ( supe r . onOpt i ons i ternSe lected ( itern) ) ;
tayoutinflater . frorn ( th i s ) ;
b . s e tView ( root ) ;
b . s e tT i t le ( R . s t ring . t i t l e_add ) ;
b . setPo s i t iveButton (
R . s t ring . btn_o k , new Dia loginterface . OnC l i c kLi s tene r ( )
pu ic void onCl ick ( Dialoginterface di alog , int whi chutton)
ContentVa lues va lues
1 5
346
{}
}) ;
. show ( ) ;
b . s e tView ( root ) ;
b . setTitle ( R . s tring . t i tle_edi t ) ;
b . s e t Pos i t i veButton (
R . s t ring . btn_o k , new Dia loginterface . OnC l i c kL i s tene r ( )
pulic vo id onCl ick ( Dialoginterface dia log , int whichutton)
ContentVa lues value s
I D= " + i d , nul l ) ;
34 7
SQLite -
b . s etNegativeButton (
{}
b . show ( ) ;
I D= "
id, nul l ) ;
mCursor . reque ry ( ) ;
b . s etNegativeButton (
R . st ring . btn_cancel , new Dialoginterface . OnCl ickLi s tener ( )
pulic void onCl ick ( Dia loginterface dialog , int whichutton )
{}
b . show ( ) ;
.
onCreate ( ) ContactDbHe lper, Contacts
,
. 1 5 .2 .
,
,
, . 1 5 .3 .
1 5
348
. 1 5. 2 .
. 1 5.3. ,
16
,
- ndrid-.
, : , , ,
, , . .
res/ assets/.
, .
:
Ll
.
( res/)
;
R,
Ll , assets/ (
),
.
As s etanage r
,
ndrid-, ,
.
,
, .
1 6 . 1 .
Android ,
.
Ll -
,
.
1 6
350
n
RGB lh- ( 6).
n
, n
L-: <>, <i> <u> . ,
L- ,
.
w .
,
n n :
: * .png (
nn n ), * .j pg * .gif;
- ,
. n
res/values/;
Paint Drawale
Android n
. ,
nn , n n.
n n L
.
L- .
.
- n . ,
. n
.
, .
n , n , n Andro id,
Theme . dialog , n
.
1 6 . 2 .
3 , n
n n res/ n. Adroid SDK
aapt n .
351
n n n
.
LJ res/an i m/ - .
LJ res/drawa\e/ - .
LJ resllayout/ - L- .
LJ res/values/ - L-, n
. , res/
.
n , n
:
arrays.xml - ;
colors.xml - ;
d i mens.xml - ;
strings.xm l - ;
styles.xml - .
LJ res/xm l/ - n L- ,
n.
LJ res/raw/ - n , n n
1 6 . 3 .
L- ( )
. n
( ),
n , n, .
n, , n
, ,
:
android : textCo lor= " @ color/opaque_red"
n n @ ,
- n n - .
n, n
n.
n :
andro id : textColor= " @ andro id : color/opaque_red"
1 6
352
1 6 .4.
R ,
. R
, ,
Android,
. R :
R . anim -
res/an im/;
R . array -
res/values/;
arrays.xml
res/values/;
R . bool -
R . color -
colors.xm l res/values/;
R . dimen -
d imens.xm l res/values/;
R . drawale -
res/drawa\e/;
L- res/layout/;
R . id -
arrays.xm l res/values/;
R . intege r -
R . layout R . raw -
res/layout/;
res/raw/;
R . s t r i ng -
R . style -
R . xml -
sty\es.xml res/values/;
res/xml/.
,
. :
R . resource_type . re source_name
.
andro id . R. ,
:
andro id . R . drawale . sym_de f_app_icon
:
andro id . R . s tyle . Theme_Black
353
1 6.4. 1 .
, Eclipse :
D Project name -
S impleValue s ;
S imple;
res/values/
strings.xm l . L- :
D arrays.xm l - ;
D colors.xm l - ;
D d imen .xm l - ;
D drawa\es.xm l - .
,
1 6. 1 -1 6 . 5 .
354
1 6
<color name= "backg roundCo l o r " > # FFO O O O < / col or>
.
mai.xml 1 6.6.
355
S impleValuesActivity n
n n n
R. n 1 6.7.
356
1 6
for ( int i
{
" + digits [ i ] + " \n " ) ;
1 1 ,
final TextView textStyle
( T extView ) fi ndVi ewByid ( R . id . t ext_style ) ;
357
, . 1 6. 1 .
. 1 6. 1 .
1 6
358
1 6.4. 2.
res/raw/ ,
" " . , -
, .
-, 14. ,
( ), 1 4, ,
res/raw/, ,
,
.
res/raw/, :
D Proj ect name -
ContactLauncher;
Launche r Sample;
mai .xml
1 6. 8 .
extView,
,
14. ,
, 1 6 .9.
359
t ry {
S t r ingBuffer sBuffer
new S t r i ngBuffe r ( ) ;
nul l ;
nul l ) {
dat a i O . close ( ) ;
i F i l e . close ( ) ;
text . s e tText ( sBuf fe r . toString ( } ) ;
catch ( Except ion )
text . s etText ( " E rror loading f i l e :
+ e . getMe s s age ( ) ) ;
.
, . 1 6.2.
1 6
360
. 1 6.2. res/raw/
1 6 .4. 3 . XM L
L-, res/layouts/.
L-
. res/menu/.
L- :
- ;
<menu>
<group>
< i t em>
, ;
- , .
I conenuApp;
D Applicatio D Package -
361
L-, ,
- Open, Save, Edit, Help Exit - ,
. 1 0. 1. 1 , options.xm l
res/menu/ . options.xm l
1 6 . 1 0 .
<menu xmlns : android= " http : / / s chernas . android . com/ap k / re s / androi d " >
< i tern
android : id= " @ + id/ open "
android : t i t le= " Open "
android : icon= " @drawale / i c_rnenu open"
android : orde r i nCategory= " l " / >
< i tern
android : id= " @ + id / s ave "
android : t i t l e = " Save "
android : i con= " @drRwa l e / i c rnenu save "
android : orde r i nCategory= " 2 " / >
< i t ern
android : id= " @ + id/edit "
android : t i tle= "Edi t "
android : i con= " @drawa l e / i c rnenu edi t "
android : orde r i nCategory= " " / >
< i tern
android : id= " @ + id/he lp "
android : t i t le="Help"
androi d : icon= " @drawale / i c_rnenu he lp"
android : orde r i nCategory= " 4 " / >
< i tern
android : id= " @ + id/ex i t "
android : t i t le= "Exi t "
android : icon= " @drawale / i c rnenu ex i t "
android : orderinCategory= " 5 " / >
< /rnenu>
1 6 . 1 1 .
TextView
1 6
362
android : layout_width= " f i l l_pa rent "
andro id : layout_he i ght= " fi ll_parent " >
<TextView
andro id : layout_width= " f i l l_parent "
android : layout_he ight = " wrap_content "
android : text = " Press ENU button . .
android : g ravit y= " cente r "
android : tex t S t yle= "bold" / >
< / L inearLayout>
, 1 .
onCreateOpt i onsMenu ( ) L
, :
@ Ove r r ide
pu l i c boolean onC reateOp t ionsMenu ( Menu menu )
ge tMenuinflate r ( ) . inflate ( R . menu . opt ions , menu ) ;
return t rue ;
L-
, , , .
R ,
, .
onOpt ions l t emSele cted ( ) ,
, :
@ Ove r r ide
pu l i c boolean onOp t i ons i temS e lected ( Menui tem i tem)
switch ( i tem . geti temid ( ) )
case R . id . open :
brea k ;
case R . id . s ave :
brea k ;
defaul t :
return fa l s e ;
return t rue ;
363
1 6 . 1 2 .
Re sMenuXmlActivity
1 1 L-
@ Ove r r ide
pu l i c boolean onCreateOptionsMenu ( Menu rnenu )
ge tMenuinflate r ( ) . inflate ( R . rnenu . options , rnenu ) ;
return t rue ;
@ Ove r ride
pu i c boolean onOption s i ternSelected ( Menu i tern itern )
Cha r Sequence rne s s age ;
swi tch ( itern . geti ternid ( ) )
ca s e R . id . open :
rne s s age
brea k ;
case R . id . s ave :
rne s sage
brea k ;
case R . id . help :
rne s s age
brea k ;
364
1 6
case R . id . edit :
me s sage
brea k ;
case R . id . exit :
me s s age
brea k ;
de faul t :
return fa l s e ;
Toa s t toast
.
L- ,
(. 1 6 .3 ) .
Open
5a'le
0
Exlt
. 1 6. 3 . , L-
1 6.4.4. L-
Adroid L-
. -
365
L- ,
res/xml/,
.
L-
. Ec l i pse ,
, L-. New And roid Proj ect
:
D P roj ect name -
F i l e s_Xrnl ;
Sample;
. samples . resxn11 ;
res/ xm l/,
L- . ,
,
contacts.xm l . contacts .xm l
1 6. 1 3 .
<contacts>
<contact f i r s t_name=" Jacob " l a s t_name="Ande rson" phone= " 4 1 2 4 1 2 4 1 1 " / >
<contact f i rst_name= " Emi l y " last_name=" Duncan " phone= " l 6 1 8 6 3 1 8 7 " / >
<contact f i r s t_name= "Michae l " l a s t_name= " Ful l e r " phone= " 4 6 7 6 5 7 5 6 5 " / >
<contact f i r s t_name= " Emma " l a s t_name= " Greenman " phone= " 0 2 5 6 6 5 7 4 6 " / >
<contact f i rst_name=" Joshua " last _name = " Harri s on " phone= " 4 7 5 2 8 9 5 6 8 " / >
<contact f i r s t_name="Mad i s on " last_name=" Johnson" phone= " 8 9 1 8 6 3 1 8 7 " / >
<contact f i r s t_name="Matthew " last_name=" Cotman " phone= " l 6 1 8 6 3 1 8 7 " />
<cont act f i r s t name= " Ol ivia " last_name=" Lawson" phone= " 9 4 3 6 5 7 8 7 5 " / >
<contact f i r s t_name= "Andrew" l a s t_name=" Chapman " phone= " 8 7 6 8 67 8 9 6 " / >
<contact f i r s t_name= " Danie l " last_name=" Honeyman" phone= " 9 8 7 65 4 3 8 8 " / >
<contact f i r s t_name= " I sabe l l a " last_name= " Jackson" phone= " 3 1 2 4 3 9 7 8 7 " / >
<contact f i r s t_name= "Wi l l i am" last_name=" Patterson" phone= " 6 8 7 6 9 9 6 9 3 " />
<contact f i r s t_name= " Joseph" las t_name="Godwin " phone= " 9 6 5 4 6 7 5 7 5 " / >
<contact f i r s t name= " Samantha " l a s t name= " Bus h " phone= " 9 0 7 8 6 5 6 4 5 " / >
<contact f i r s t name= " Chri s " last name= " Gateman" phone=" 8 9 6 8 7 4 5 5 6 " / >
< / contac t s >
main .xml
1 6. 1 4.
1 6
contacts.xml, , :
Xml Pu l l Pa r s e r pars e r
==
Xml Pu l l Pa r s e r . START_TAG
1 6. 1 5 .
ResXmlAct ivity
367
,
import android . widget . ArrayAdapt e r ;
import android . widget . Toas t ;
pu l i c c l a s s ResXmlAct ivi ty extends L i s tAct ivity
@ Ove rride
pul i c void onCreate ( Bundle savedinstance State )
supe r . onCreate ( savedins tance State ) ;
setContentView ( R . layout . ma in ) ;
ArrayL i s t < S t r ing> l i s t
try
Xml Pul l Pa r s e r pa r s e r
wh i l e . ( pa rs e r . getEventType ( ) !
i f ( parse r . getEventType ( )
" \n "
cat ch ( Throwale t )
+ t . t o S t r ing ( ) ,
4000)
. show ( ) ;
, ,
. 1 6.4.
368
1 6
. 1 6.4. L-
1 6 . 5 .
,
.
- , , . .
Android ,
.
.
1 6 . 5 . 1 .
-
,
L- .
, ,
,
. L-:
< ? xrnl ve r s ion= " . 0 " encoding= " u t f- 8 " ?>
<re source s >
<style name= " CustomText " parent= " @ s tyle /Text " >
< item name=" android : text S i z e " > l B sp</ item>
369
,
< item name=" android : textColor " > # 0 0 8 < / i tem>
< / s t yle>
< / re s ources >
, < item>,
. n
,
.
parent < s t yl e > .
, u
. ,
. u,
, , Android.
,
.
TexView:
s t yle.
<TexView
s t yle= " @ style/CustomText "
android : id= " @ + id/ text l "
andro id : layout_width= " f i l l_pa ren t "
android : layout_he ight= "wrap_conten t "
android : text= " He l l o , World ! "
Textview
L-.
Customext,
1 6 .5.2.
- ,
. , ,
,
.
. , ,
L- <s tyle>,
. ,
<appl icat ion>
<act ivity> , . .
.
3 70
1 6
:
< ? xml ve r s i on= " l . O " encoding= " ut f- 8 " ? >
<resources >
< s t y l e narne= " CustomTheme " >
< i t em narne= " andro i d : windowNoT i t l e " >t rue< / i t em>
< i tem narne="windowFrarne " > @ drawale / s creen frarne < / i t em>
< i tem
narne= "windowBac kground " > @ drawa l e / s creen_bac kground_whi te< / i tem>
< i t em narne= "panel ForegroundColor " > # FFO O O O O O < / i tem>
< i tem narne= "panelBackgroundCo lor " > # FFFFFFFF< / i t em>
< i t em narne= "panelTextColor " > ?pane l ForegroundCo lor< / i t em>
< i tem narne = " panelText S i z e " > l 4 < / item>
< i tem narne= "menu i t emTextCo lor " > ?panelTextColor < / i tem>
< i tem narne= "menuitemText S i z e " > ?panelTextS i z e < / i t em>
< / s t yl e >
< / res ource s >
,
AndroidMan ifest.xm l <appl i cat ion>,
andro i d : theme :
android : theme= " @ s t yle / theme_custom"
,
,
<activity> .
, Android
, . ,
Theme . Dia log,
. ' Andro i d :
<activity andro id : theme= " @ android : s t yl e /Theme . Dialog " >
1 6. 5 . 3 .
,
.
D styles.xml res/val ues/.
<resource s > .
D < s t y l e >
, narne= " Cus tomTheme " , -
371
. ,
,
.
<style>
< i t ern> . < i t ern> - .
, . Ecl ipse
New Android Project :
and Therne s ;
res/values/ L-,
- Specia lText - Stars,
styles.xm l . 1 6 . 1 6.
< ?xml ve r s i on= " l . O " encoding= " ut f-8 " ?>
<resources>
<style name= " SpecialTex t " >
< i t em name= " android : text S i z e " > 2 8 sp< / i t em>
< i t em name= " android : t extCol o r " > # FFO O O O < / i tem>
< i t em name= " android : background " > # O O O O O O < / i t em>
< / style>
< s tyle name= " S tars " >
<item name= " android : windowBackground " > @ drawal e / stars < / i tem>
< i t ern name= " andro id : windowNoT i t l e " > t rue < / i t ern>
< / style>
< / resources >
main.xml 1 6. 1 7 .
372
1 6
, android : theme :
android : theme= " @ s t y l e / Stars "
1 6. 1 8 .
< ?xml ver s i on= " l . O " encoding= " ut f - 8 " ? >
<mani fest xmlns : andro id= " http : / / s chemas . android . com/apk/ res /android"
package= " com . s amples . stylesandtheme s "
android : vers ionCode= " l "
android : ve r s i onName= " l . O " >
<app l i ca t i on
andro i d : i con= " @ drawa l e / i con"
andro i d : label= " @ s t ring/app_name "
andro i d : theme= " @ style / Stars " >
<activity
android : name= " . StylesAndThemesActivity"
andro id : labe l= " @ s t r i ng /app_name " >
< intent - f i l t e r>
<action android : name= " andro id . intent . act ion . AIN " / >
<category andro id : name= " android . intent . category . LAUNCHER" / >
< / intent - fi lter>
373
< /activity>
< / appl i cation>
<use s - sdk android : minSdkVe rsion= " "
< /man i fe s t >
. 1 6. 5 .
. 16.5.
1 6 . 6 .
- ,
assets/. res/
. ,
assets/ R.
.
- , assets/.
As s etManager. ,
res/,
.
: ,
,
Android.
, -
1 6
374
. , assets/
.
,
, Create New
Project :
Application name -
Create Activity -
FontsActivity.
3 75
,
<TextView
android : text= " a font "
android : id= " @ + id/text "
android : layout_width= "wrap_content "
androi d : layout_he ight="wrap_content "
android : text S i ze= " s p "
android : padding= " l O sp " / >
<TextVi ew
android : text= "A Yummy Apo logy"
android : id= " @ + id/text 4 "
android : layout_width= " wrap_content "
android : layout_he ight= "wrap_content "
android : textS i ze= " sp"
android : padding= " l O sp " / >
< / LinearLayout >
:
Linu- Libertine ,
http://www . urbanfonts.com/free-fonts.htm .
extVi ew,
Typeface,
, .
:
extView
TextView text
Typeface face
Typeface . c reateFromAs s e t (
FontsActi vi ty
1 6 .20.
1 6
3 76
pu l i c c l a s s FontsActivity extends Act ivity
@ Ove rride
pu l i c void onC reate ( Bundl e s avedins tanceState )
supe r . onCreate ( savedinstanceState ) ;
setContentView ( R . layout . main ) ;
/ 1
( TextView ) f indViewByid ( R . i d . text l ) ;
. 1 6.6. , assets/
377
, ,
. 1 6.6.
1 6 . 7 .
Android
. ,
, , ,
, ,
.
Android
,
.
,
, ,
.
1 6 . 7 . 1 . ,
, (
), ,
Android
res/values/strings.xm l .
,
.
, ,
res/values/strings.xm l
. ,
: ,
, , - ,
, . .
. ,
. ,
.
,
- -
, ,
.
1 6
3 78
1 6 . 7 . 2 .
,
,
. . 1 6. 1 .
1 6. 1 .
stri n g s .x m l
Defa u l t
E n g l i s h 1 U n ited Kingdom
r e s /va lues /
ru-rRU
Russian 1 R u ssia
de-rDE
German 1 Ge rmany
res /values-de /
j a-rJ P
res /value s - j a /
fr-rFR
French 1 France
r e s /va l ue s - f r/
fr-rCA
French 1 Canada
res /value s - fr /
e n-rCA
E n g l i s h 1 Canada
res /values /
en-rUS
,
,
res/val ues-en/.
,
. res/
.
res/ values-ru/ -
.
, . 1 6.7.
,
, , ,
1 6 .2 1 . res/values-ru/.
i3
res
ii! i1:7 dr w
iiJ GiJ, l ayout
i3 i1:7 values
, u
3 79
, .
,
.
. Application Launcher
Settings 1 Language & keyboard 1 Select locale 1
, . \ 6 . 8 .
. 1 6.8.
1 6
380
Application Launcher.
Application Launcher r .
. .
,
, . 1 6 .9.
. 1 6. 9 . n
17
ndrid-
. Drawale
.
:
D View ;
D .
view ,
,
,
.
1 7 . 1 . Drawa/e
Android
- andro id . graphics . drawale.
Drawale
. . D rawale
, BitmapDrawale, ShapeDrawale,
PictureDrawale, Layerorawale . Android
. 1 7 . 1 .
Drawale:
D , res/drawale/;
D L-,
Drawale.
1 7
382
Object
Drawae
BitmapDrawae
Clip Drawae
ColorDrawae
DrawaeContainer
1
1
1
1
An imationDrawae
LeveiListDrawae
State ListDrawae
G radientDrawae
lnsetDrawae
LayerDrawae
N inePathDrawae
PictureDrawae
RotateDrawae
ScaleDrawae
Shape Drawae
1
1
1
1
1
1
1
1
1
TransitionDrawa e
PaintDrawae
. 1 7. 1 .
-
. :
D PNG - ;
D J PEG - ;
D GIF - .
,
.
.
, res/drawaie/
,
aapt. ,
. -
383
(* .bmp, aapt
) , res/raw/,
aapt.
-
.
1 7 2 Drawa/e
.
Drawale. , Drawale,
:
Re sources res
mContext . getResources ( ) ;
Drawale myimage
,
. ,
orawale ,
, alpha ( ),
Drawale, .
. , ,
XML. Drawa e,
,
, Drawale XML - .
, Drawae
,
Drawale XML,
.
Drawale L-,
res/drawae/ .
, Resource s . getDrawae ( )
L-. ,
Drawa e, inflate ( ) ,
XML .
Drawa e .
1 7
384
1 7 .2. 1 . TransitionDrawa/e
Drawale -
.
Trans i tionDrawale .
Ecl ipse New Android Proj ect:
Proj ect name - Trans i t ion;
Application name - Trans i tiori Sample;
Package name -
ImageView,
1 7 . 1 .
res/drawaie/ , photo J .j pg
photo2.j pg. -,
Resources/, Adroid SDK - ,
, adroid-sdk-widows/platforms/adroid2/samples/ApiDemos/res/drawae/.
res/drawaie/ tra sitio .xm l ,
. transition.xml
1 7 .2.
385
LoadimageAc t i vi ty onC l i c k ( ) ,
1 :
t rans i t ion . s ta rtTrans i t ion ( l O O O ) ;
LoadimageAct ivity
1 7 .3 .
1 7
386
Resources res
mTrans i t i on
R . drawae . t rans i t i on ) ;
1 1 n
11 1
@ Ove r ride
pu l i c void onC l i c k (View v )
. (
)
(. 1 7 .2).
. 1 7.2.
1 7 2 2 ShapeDrawa/e
.
,
ShapeDrawale .
ShapeDrawae
.
387
Android
, Shape :
PathShape;
RectShape ;
Arc Shape ;
Ova l Shape;
RoundRectShape.
. 1 7 .3 .
Object
-----,
L___--1
Shape
1
1
_,,_
,--------,
RectShape
PathShape
1
1
1
--------,
ArcShape
OvaiShape
Round RectShape
. 1 7.3.
ova l Shape ( ):
Shape Drawale d
d . s e t i nt r in s i cHeight ( l O O ) ;
d . set int r insicW idth ( l 5 0 ) ;
d . get Paint ( ) . s etColor ( Colo r . RE D ) ;
1 7
388
( Round.Rect )
. Round.Rect
:
Round.Rect Shape ( float [ ] oute rRadi i , Rect F inset , float [ ] inne rRadi i )
RoundRect Shape :
D
. ,
.
, nul l ;
Rect F,
.
Rect F :
.
, nul l;
innerRadi i -
. -
,
. ,
nul l .
oute rRadi i -
inset -
,
:
float [ ] outR
new float [ ]
rectF
RectF
float [ ] inR
, , , , , , ,
new RectF ( 8 , 8 , 8 , 8 ) ;
new float [ ]
Shape Drawale d
{ , , , ,
new ShapeDrawal e (
Path ,
,
. ( )
moveTo ( ) l ineTo ( ) . , Path
:
Path
new Path ( ) ;
p . moveTo ( 5 0 , 0 ) ;
p . l ineTo ( 2 5 , 1 0 0 ) ;
389
p . l ineTo ( l 0 0 , 5 0 ) ;
p . l ineTo ( 0 , 5 0 ) ;
p . l ineTo ( 7 5 , 1 0 0 ) ;
p . l ineTo ( 5 0 , 0 ) ;
ShapeDrawale d = new ShapeDrawale ( new PathShape ( p , 1 0 0 , 1 0 0 ) ) ;
d . set int r i n s i cHe i ght ( 1 0 0 ) ;
d . s e t int rinsicW idth ( 1 0 0 ) ;
d . getPaint ( ) . setColor ( Colo r . YELLOW ) ;
d . getPaint ( ) . s e t S t yle ( Paint . Style . STROE ) ;
ArcShape .
:
ArcShape ( float sta rtAngl e ,
float sweepAngle )
- ,
- .
ShapeDrawal e, Drawale,
android . graph i c s . drawale,
, s etAlpha ( ) -
, setColorFil ter ( ) . .
ShapeDrawale Shape
, IrnageView
. Ecl ipse
New Android Project:
D Project name -
ShapeDrawale;
S ample;
IrnageView,
< ?xrnl ve rs ion= " 1 . 0 " encoding= " utf-8 " ?>
<LinearLayout xrnlns : andro id= " http : / / s cherna s . andro id . com/apk / r e s / andro i d "
android : id= " @ + id/ root "
andro id : layout_width= " fi l l_pa rent "
andro id : l ayout_he i ght= " fi l l_parent "
android : or i entation= " ho r i z ontal "
android : layout_gravit y= " center_ve r t i ca l l cente r_ho r i z ontal " >
1 7
390
< IrnageView
android : id= " @ + id/ image "
android : layout_width= "wrap_content "
android : layout_he ight = " wrap_content "
android : minHe i ght= " 9 0px "
android : minWidth= " 9 0px "
android : layout_rnargin= " l l 5px " / >
< /L inearLayout >
DrawCanvasActivity
1 . Line - .
2 . Oval - .
3. Rectangle - .
4 . RoundRect. Fill - .
5 . Path - .
6. Arc - .
Imagevi ew
.
onC reateOpt i onsMenu ( )
. DrawCanvasActivity
1 7. 5 .
391
import android . view . Menu ;
import android . vi ew . Menui tem;
import android . widget . ImageView;
pu l i c c 1 a s s Shape DrawaleActivity extends Act ivi ty
11
pr ivate s t a t i c fina1 int I DM LINE
101 ;
102 ;
103 ;
104 ;
105;
106;
@ Ove r ri de
pu i c boo1ean onC reateOpt ionsMenu ( Menu menu )
menu . add (Menu . NONE , I DM_LINE , Menu . NONE ,
menu . add ( Menu . NONE , I DM_ROUNDRECT , Menu . NONE , " Round Rect . Fi 1 1 " ) ;
menu . add ( Menu . NONE , I DM_STAR , Menu . NONE , " Path" ) ;
menu . add ( Menu . NONE , I DM_ARC , Menu . NONE , "Arc " ) ;
return ( supe r . onCreateOpt ionsMenu (menu ) ) ;
@ Ove rride
pul i c boo 1ean onOptions i t emSe1ected ( Menu i tem i tem)
ShapeDrawae d
nu1 1 ;
392
1 7
d . set intrins i cWidth ( 1 5 0 ) ;
d . get Paint ( ) . s etCo 1 o r ( Co1or . AGENTA ) ;
bre a k ;
cas e I DM OVAL :
d
float [ ] inR
d
, , , , ,
new Rect F ( 8 , 8 , 8 , 8 ) ;
new f1oat [ ]
rectF
new float [ ]
, ,
, , , , , , ,
new Path ( ) ;
p . moveTo ( 5 0 , 0 ) ;
p . 1 ineTo ( 2 5 , 1 0 0 ) ;
p . 1 ineTo ( 1 0 0 , 5 0 ) ;
p . 1 ineTo ( 0 , 5 0 ) ;
p . 1 ineTo ( 7 5 , 1 0 0 ) ;
p . 1 ineTo ( 5 0 , 0 ) ;
d
393
d . s et i ntrins i cWidth ( l O O ) ;
d . getPaint ( ) . s etColor ( Colo r . YELLOW ) ;
bre a k ;
mimage . s etBac kgroundD rawal e ( d ) ;
return t rue ;
.
ImageView
(. 1 7 .4).
. 1 7.4.
1 7 3
.
,
. ,
.
- .
:
D , ,
Vi ew,
1 7
394
inva l i date ( )
onDraw ( ) ;
Surfaceview.
canvas ,
, drawBi tmap ( ) , drawRect ( ) , drawext ( )
. , ,
draw ( ) . , Drawae
. Drawae draw ( ) ,
canvas .
,
.
View . onDraw ( ) , Canva s .
Canvas Sur faceHolder .
loc kCanvas ( ) , SurfaceView.
Android onDraw ( ) .
, ,
inval idate ( )
, Android onDraw ( )
.
ShapeDrawae draw ( ) ,
View, Shape Drawa e
View . onDraw ( ) :
,
@ Ove rride
protected void onDraw ( Canvas canvas )
1 1
mDrawae
new Shape Drawale ( new Rect Shape ( ) ) ;
mDrawae . s e t intrins i cHe i ght ( 2 ) ;
mDrawale . s et intrins i cWidth ( l 5 0 ) ;
mDrawa le . get Paint ( ) . s etColor ( Color . AGENTA) ;
/1
,
Imageview, , -
395
. Ecl ipse
New Android Project:
o n Canvas Sarnple;
canvas ;
DrawCanvasActivity
DrawCanvasView, View,
.
DrawCanvasView
.
ShapeDrawale
DrawCanvasView .
DrawCanvasActivity 1 7 .6.
1 1
pu l i c s t at i c f inal int I DM LINE
101 ;
102 ;
103 ;
104 ;
396
1 7
pu i c static fina1 int I DM STAR
pu l i c s t a t i c fina1 int I DM ARC
105 ;
106;
11
11
n - ,
rnView
setContentView (rnView ) ;
@ Ove r ride
pu 1 i c boo1ean onCreateOpt ionsMenu ( Menu rnenu )
rnenu . add (Menu . NONE , r o_LINE , Menu . NONE , " Line " ) ;
rnenu . add ( Menu . NONE , I DM_OVAL , Menu . NONE , " Ova 1 " ) ;
rnenu . add ( Menu . NONE , I DM_RECT , Menu . NONE , "Rectang1e " ) ;
Fi l l " ) ;
@ Ove r ride
pu 1 i c boo 1ean onOptions iternSe1ected ( Menuitern i tern)
Shape Drawae d
case I DM L INE :
d
d . s e t i nt r i n s i cHeight ( l O O ) ;
d . s e t i nt rins i cWidth ( l 5 0 ) ;
d . getPaint ( ) . s etCo1o r ( Co1or . RE D ) ;
bre a k ;
397
case I DM RECT :
d
d . s etintrinsicHei ght ( 1 0 0 ) ;
d . s et i n t r ins i cWidth ( 1 5 0 ) ;
d . getPa int ( ) . s etColor ( Color . BLUE ) ;
bre a k ;
c a s e I DM ROUNDRECT :
float [ ] outR
Rec t F
float [ ) inR
d
new float [ ]
6, 6, 6, 6, 6, 6, 6, 6
new Rect F ( 8 , 8 , 8 , 8 ) ;
rectF
new float [ ]
{ 6,
6, 6, 6, 6, 6, 6, 6
new Path ( ) ;
p . moveTo ( 5 0 , 0 ) ;
p . l ineTo ( 2 5 , 1 0 0 ) ;
p . l ineTo ( 1 0 0 , 5 0 ) ;
p . l ineTo ( 0 , 50 ) ;
p . l ineTo ( 7 5 , 1 0 0 ) ;
p . l ineTo ( 5 0 , 0 ) ;
d
d . s e t intrinsicHeight ( 1 0 0 ) ;
d . set int r ins i cWidth ( 1 0 0 ) ;
d . getPaint ( ) . s etColor ( Color . YELLOW ) ;
d . getPaint ( ) . s e t S t yle ( Pa int . Style . STROE ) ;
bre a k ;
c a s e I DM ARC :
d
11
Shape Drawale
mView . s etDrawale ( d ) ;
return t rue ;
inR ) ) ;
398
1 7
D rawCanvasView
J-! 1 7 . 7 .
11
10;
10 ;
11
, Shape Drawale
11
shape ;
ShapeDrawale
) ;
399
inval idate ( ) ;
11
@ Ove r r i de
protected vo id onDraw ( Canvas canva s )
mDrawal e . draw ( canvas ) ;
.
.
(. 1 7 .5).
Rau11d Rct.
Pth
Arr
. 1 7.5.
18
ndrid-
Android SDK,
android . view . animationpackages .
Android :
LJ Tween Animation - ;
LJ Frame Animation - .
1 8 . 1 .
-
, , View. ,
, extView , ,
. TextView ,
. android . view .
animationpa c kages ,
. . 1 8 . 1 .
:
LJ Animat ionset - , ,
. Animat ionSet -
, , ;
LJ AlphaAnimation - ,
;
LJ RotateAnimat ion - ,
402
1 8
LJ ScaleAnima t i on - ,
;
LJ T rans lateAnimat ion - ,
.
Object
------,
Ani mation
Ani mationSet
AlphaAnimation
RotateAni mation
ScaleAnimation
TranslateAnimation
1
1
1
1
1
. 1 8. 1 . Android
,
.
. ,
( ,
,
. .), (,
).
, .
,
.
L-
. L
( L-),
,
.
1 8. 1 . 1 . L-
L- res/an im/ .
:
<a lpha>, <scale>, <t rans late>, <rotate> < s e t >,
(
<set> ). L-
. 1 8 .2.
403
<set>
<set>
1
<scale>
<set>
<translate>
<scale>
<translate>
<alpha>
<alpha>
<scale>
<translate>
<rotate>
1
1
1
1
1
1
1
1
. 1 8. 2 . L-
.
, s t a rtOffset
, :
andro id : s tartOffset= " O O O "
. .
<a lpha>, <scale>, < t rans late>, <rotate> <set>
, Animation:
Ll durat ion - ;
Ll s t a rtOffset - ,
;
Ll f i l lBe fore - t rue,
;
Ll f i l lAfter - t rue,
;
Ll repeatCount - ;
Ll repeatMode - .
:
;
1 8
404
LJ zAdj ustment - Z ,
(, );
LJ interpolator - ,
,
, .
,
R . s t yl ea le, :
android : int e rpolator= " @ android : anim/dece lerate_interpo lator"
<set>
< s e t > - , .
Animat ionset. shareinterpolator,
.
<alpha>
.
:
AlphaAnima t i on.
LJ fromAlpha - ;
LJ toAlpha - .
1 ,
.
<sca/e>
< s ca l e >
ScaleAnima t i on.
( ),
. < s c a l e >
:
LJ fromXSca le - ;
LJ toXScale - ;
LJ fromYS ca le - ;
LJ toYScale - ;
LJ pivotx - - ;
LJ pivotY - - .
405
<translate>
<t ran s late> -
. rans lateAnima t i on
:
LJ fromXDe l t a - ;
LJ toXDel ta - ;
LJ fromYDe l t a - ;
LJ toYDe l t a -
:
1 . .
2. - 1 00% 1 00% .
3 . - 1 % 1 %,
.
<rotate>
<rotate>
Rotat eAnimation.
LJ fromDegree s - ;
LJ toDegrees - ;
LJ pi votx - ;
LJ pi votY - .
1 8. 1 . 2 .
,
.
- ,
, . Ecl ipse
New Android Project:
LJ Proj ect name - TweenAnimat ionShape s ;
LJ Application name - Tween Anima t i on Sample;
LJ Package name - com . s ample s . tweenanimat ionshape s ;
LJ Create Activity - TweenAnimat ionActivity.
res/anim/ L- :
alpha.xml, rotate.xml, scale.xml, translate.xml, -
406
1 8
, total .xml,
<alpha>, <scale>, <t rans late>,
<rotate> .
alpha.xml, rotate.xml, scale.xml, translate.xml total .xm l
1 8 . 1 - 1 8 . 5 .
android : pivotY= " S O % "
android : fromXS cale= " l . O "
android : fromYScale= " l . O "
android : toXScale= " 2 . 0 "
android : toYScale= " 2 . 0 "
android : dura t i on= " 2 5 0 0 "
<scale
android : st a rt0ffset= " 2 50 0 "
android : durat i on= " 2 5 0 0 "
android : pivotX= " S O % "
android : pivotY= " S O % "
android : fromXS cale= " l . O "
android : fromYScale= " l . O "
androi d : toXScale= " O . S "
android : toYScale= " O . S "
</set>
407
18
408
android : toAlpha= " l . O "
android : st a rtOffset= " O "
android : durat i on= " S O O O " / >
<scale
android : durat i on= " 2 5 0 0 "
android : pivotX= " S O % "
android : pivotY= " S O % "
android : fromXSca l e= " l . O "
android : fromYScale= " l . O "
android : toXScale= " 2 . 0 "
android : toYScale= " 2 . 0 "
< rotate
android : fromDegrees=" O "
android : toDegrees= " 3 6 0 "
android : pivotX= " S O % "
androi d : pivotY= " 5 0 % "
android : durat i on= " S O O O "
<translate
android : toYDe l t a= " - 1 0 0 "
android : fi l lAfter= " fa l s e "
android : dura t i on= " 2 5 0 0 "
<scale
android : durat i on= " 2 5 0 0 "
android : st a rt0ffset=" 2 5 0 0 "
android : p ivotX= " S O % "
android : pivotY= " S O % "
android : fromXS cale= " l . O "
android : fromYScale= " l . O "
android : toXS cale= " O . S "
android : toYScale= " O . S "
< t ranslate
android : toYDe l t a= " l O O "
android : fi l lAfter= " fa l s e "
android : durat ion= " 2 5 0 0 "
android : start0ffset=" 2 5 0 0 "
< / set>
, ,
L-, shape.xml
res/drawae/. ShapeDrawale
. shape.xm l
1 8.6.
409
ImageView, 1 8 .7.
:
Animat i on
AnimationU t i l s . loadAnimation ( )
L- . <;
View . s t a rtAnima t ion ( ) , Animat ion:
,
ImageView. image
( ImageView)
findViewByid ( R . i d . image ) ;
1 8
410
Animat ion
:
D
onAnimat i onEnd ( ) ;
onAnima t i onRepea t ( ) ;
onAnimat ionStart ( ) .
,
. ,
, -
:
@ Ove r ride
pulic void onAnima t i onStart ( Anima t i on animation)
mimage . s etVi s i bi l i ty (View . VI S IBLE ) ;
@ Overr ide
pulic void onAnima t i onEnd (Animation animat ion )
mimage . s etVi s ibi l i t y ( View . INV I S I BLE ) ;
@ Ove rride
pulic void onAnimat i onRepeat ( Animat ion animat ion )
mimage . s etVi sibi l i t y ( Vi ew . VI S IBLE ) ;
,
, - Alpha, Scale, Translate, Rotate
Total.
L- ,
onOpt ions i t emSe lected ( ) , .
1 8 . 8 .
TweenAnima t i onActivity
411
import android . view . anima t i on . An imat ion ;
import android . view . anima t i on . Animat ionUt i l s ;
import android . view . anima t i on . An imat ion . Animat ionLi s tene r ;
import android . widget . ImageView;
import android . widget . Toa s t ;
pu l i c c l a s s TweenAnimationAc t ivity extends Activi ty
implements An imationListene r {
p rivate ImageView m!mage ;
private Anima t i on anima t i on ;
@ Ove rride
p rotected void onCreate ( Bundle savedins tanceState )
s upe r . onCreate ( savedinstance State ) ;
setContentView ( R . layout . main ) ;
mimage
@ Ove r ride
pu l i c boolean onCreateOpt ionsMenu ( Menu menu )
menu . add ( Menu . NONE , R . anim . a lpha , Menu . NONE , "Alpha " )
. setAlphabet icSho rtcut ( ' a ' ) ;
menu . add ( Menu . NONE , R . anim . s ca l e , Menu . NONE , " Scale " )
. setAlphabeticShortcut ( ' s ' ) ;
menu . add ( Menu . NONE , R . anim . t rans late , Menu . NONE ,
@ Ove rride
pu l i c boolean onOpt i ons i temSel ected ( Menuitem i tem )
11
L-
animation
1 8
412
m!mage . s ta rtAnima t i on ( an irna t i on ) ;
return t rue ;
11
@ Ove rride
pul i c void onAnirnat i onEnd (An imation animation )
mirnage . setVi s ibi l i t y (View . INVI S IBLE ) ;
@ Ove rride
pul i c void onAnirna t ionRepeat (An irna t i on anima t i on )
m!rnage . setVi s ib i l i t y ( View . V I S IBLE ) ;
@ Ove r ride
pul i c void onAnimationStart (An irna t i on anirnat i on )
m!mage . setVi s ibi l i ty ( View . VI S IBLE ) ;
. ,
.
. 1 8 .3 .
. 1 8.3. L-
413
1 8. 1 . 3 .
, n,
n.
n , ImageView.
Eclipse n n n New And roid
Project:
Animation Sample;
Resources/Iages/ n-
adroid_ d.png ( , n
n ).
L- no
n n: n <sca l e >
<set>. <set> n
, <scale> < rotate>,
Vi ew, res/anitn/ n
i adroid.xm l .
a im_adroid.xl n 1 8 .9.
18
414
<scale
android : fromXScale= " 1 . 4 "
android : t oXScale= " O . O "
android : fromYScale= " O . "
android : toYScale= " O . O "
android : pivotX= " 5 0 % "
android : pivotY= " 5 0 % "
android : s ta rt0ffset= " 3 0 0 0 "
android : dura t i on= " 2 0 0 0 "
androi d : f i l lBe fore= " fa l s e "
< rotate
android : frornDegre e s= " O "
android : t o Degree s= " - 4 5 "
android : toYScale= " O . O "
androi d : pivotX= " S O % "
android : pivotY= " S O % "
android : s ta rtOffset= " O O O "
android : dura t i on= " 2 0 0 0 " / >
</set>
< / set>
n
ImageView, n 1 8. 1 .
415
<Linea rLayout
android : id= " @ + id/layout_anim"
android : layout_he i ght= " fi l l_parent "
android : gravity= " cente r "
android : layout_width= " fi l l_parent " >
< ImageView
android : id= " @ + i d / image "
android : l ayout_width= "wrap_content "
android : layout_he i ght="wrap_content "
android : minHe i ght= " l O Opx "
android : minWidth= " l O Opx "
androi d : layout_ma rgin= " l O Opx "
android : s rc= " @drawa e / androidd " / >
< / Linea rLayout >
< / LinearLayout>
1 8 . 1 1 .
( But ton )
( ImageView)
findViewByid ( R . id . btn_s t a rt ) ;
findViewByi d ( R . id . image ) ;
416
1 8
Anirnat ion anim
An ima tionUt i l s . loadAn imat i on (
thi s , R . anim . interpolator ) ;
image . sta rtAnimation ( anim) ;
.
Start ,
,
. n . 1 8 .4.
. 1 8.4.
View
n
L- .
, n
, Vi ew, ,
:
.
,
.
417
1 8. 1 .4.
,
. , Linea rLayout,
,
. , IrnageView
extView, . Eclipse
New And roid Project:
Project name - TweenAnima t i onVi ew;
Application name - Tween Anirnat ion Sample;
L- ,
- <a lpha>
<rotate>
<scale> <rotate> .
res/anim/ circle.xm\.
circle.xml 1 8. 1 2 .
18
418
androi d : toDegrees = " O "
android : pivotX= " S O % "
android : pivotY= " 5 0 % "
android : durat i on= " S O O O "
< s ca l e
android : durat i on= " 2 5 0 0 "
android : st a rt0ffset= " 2 5 0 0 "
android : pivotX= " S O % "
android : pivotY= " 5 0 % "
android : frornXScale= " l . O "
android : fromYScale= " l . O "
android : toXScale= " 0 . 5 "
androi d : toYScale= " O . S "
< s ca l e
android : durat i on= " 2 5 0 0 "
android : st a rt0ffset= " 5 0 0 0 "
android : pivotX= " S O % "
android : pivotY= " S O % "
android : frornXScale= i' l . "
android : fromYScale= " l . O "
android : toXScale= " 1 . 2 5 "
android : toYScale= " 1 . 2 5 "
< s ca l e
android : durat i on= " 2 5 0 0 "
android : sta rt0ffset= " 7 5 0 0 "
android : pivotX= " S O % "
android : pivotY= " S O % "
android : frornXS cale= " l . O "
android : fromYScale= " l . O "
android : toXScale= " O . B O "
andro i d : toYScale= " O . B O "
< / set>
n n
LinearLayout, ImageView
n n n extView n
" Hello, Adroid ! " n
:
android : id= " @ + i d / l ayout_anim,
n n
. n 1 8 . 1 3 .
419
n , . 1 8 . 5 .
n n n,
, n
, nn :
420
1 8
layout
anima t i on
1 8 . 1 4.
. 18.5.
421
layout
animation
( Button )
.
. 1 8 .6.
. 1 8.6.
1 8 . 2 .
( ) - ,
.
AnimationDrawale.
422
1 8
, , L
res/an im/
Android.
1 8. 2 . 1 . XM L
L-
<animation- l i s t > < i t em>,
, :
L- :
<animation- l i s t xmlns : android= " http : / / schemas . android . com/ apk/res /android"
android : oneshot= " t rue " >
< i t em android : drawae= " @ drawa e / f i l e l
< i t em android : drawa e= " @ drawa e / f i l e 2 " android : durat ion= " 2 0 0 "
< i t em andro id : drawa e= " @ drawale / f i le " android : dura t i on= " 2 0 0 "
< /animation- l i s t >
.
android : oneshot t rue
. android : oneshot
false, . L-,
res/anim/ n,
.
.
Ecl i pse New And roid Proj ect:
Application name -
Create Activity -
423
1 8
424
< i tern
android : drawale= " @ drawa l e / android2 "
android : durat ion= " O O " />
< i tem
android : drawale= " @ drawa l e / android "
android : durat i on= " O O " />
< / animat ion- l i s t >
:
ImageView image
AnimationDrawale
start ( ) s top ( )
Anima t i onDrawale
1 8 . 1 7 .
FrameAnimat ionActivity
( Button )
425
pu i c void onC l i c k ( View v )
mAnim . s t a rt ( ) ;
( Button )
f i ndViewByi d ( R . i d . btn_s t op ) ;
pu i c vo id onC l i c k ( View v )
mAnim . s t op ( )
.
, (. 1 8 . 7).
. 1 8 .7.
1 8. 2 . 2 .
,
-
.
Ecl i pse ,
.
1 8
426
D Create Activity -
1 8 . 1 5
. FrameAnima t i onActivity
, start ( ) s t op ( ) ,
onC l i c k ( ) Start Stop.
s tart ( ) .
Drawale,
. Drawale:
Bi tmapDrawa le frame 1
( Bi tmapDrawale ) getResources ( ) . getDrawale ( R . drawale . android1 ) ;
Bi tmapDrawae frame2
( Bi tmapDrawale ) getResources ( ) . ge t Drawale ( R . drawale . android2 ) ;
Bi tmapDrawale frame
( Bi tmapDrawale ) getResources ( ) . getDrawale ( R . drawale . android ) ;
Bi tmapDrawale
addFrame ( ) . addFrame ( )
: ( Drawale)
.
:
AnimationDrawae
1/
new AnimationDrawale ( ) ;
mAnimation . s etOneShot ( fa l se ) ;
mAnimation . addFrame ( f rame 1 , 1 0 0 ) ;
mAnimation . addFrame ( frame2 , 1 0 0 ) ;
mAnimat ion . addFrame ( frame , 1 0 0 ) ;
/1
ImageView
1 1 Drawae
mAnimation . s etVi s ile ( t rue , t rue ) ;
mAnimation . s t a rt ( ) ;
1 8 . 1 8 .
FrameAnima t i onActivity
427
300 ;
nul l ;
( Button )
findViewByid ( R . id . btn_s t a rt ) ;
s t a rt ( ) ;
( Button )
s t op ( ) ;
private void s t a rt ( )
{
Bi tmapDrawale frame l
( B i tmapDrawale ) getResource s ( ) . get Drawale ( R . drawale . androidl ) ;
428
1 8
B i trnapDrawal e frame2
( B itrnapDrawal e ) getRe sources ( ) . getDrawale ( R . drawale . android2 ) ;
B i trnapDrawal e frame
( B i trnapDrawale ) getRe s ou rce s ( ) . getDrawale ( R . drawale . android ) ;
mAnimat ion
new AnirnationDrawale ( ) ;
L- ( . . 1 8 .7 ).
-
-
- : Samples/ Resources/.
Samp les/ , .
Resources :
l m ages/ -
Meu_lcos/ - (
8) , ( 9) , ( 1 0-18).
Ga l l e r y
( 7) ;
Android SDK.
.
Samples/ - ,
. n Ecl ipse
. n File 1 Switch
Workspace 1 Other Workspace Launcher
,
(. 1 . 1 ).
Adroid S DK. n
Window 1 P references. Preferences
And roid . SDK Location
, Adroid SDK. -
430
Select
workspace
!;_ 5engs
. 1 . 1 . Eclipse
General
Build
DDMS
Launch
LogCat
Usage Stats
Ant
Data Management
Google
Help
Instaii/Update
Java
Java
Plugin Development
Remote Systems
Run/Debug
Server
Tasks
Team
Terminal
lf Usage Data Collector
Validation
Web
Web Services
X Doclet
...... ' i. X.oi L...
Android Preferences
:::;i.;:ki..i
Adrid .i :;
Andro1d 1 . 5
1 Android 1 .
' Android 2 . 0
Android 2 . 0 . 1
Google AP!s
. Google AP!s
1 Google AP!s
1 Google AP!s
.. ......
Vendor
..
. 1 . 2 . Android S D K
'l'
''
.
:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . : . . . . . . . . .
' Platforin
. . ,
1.1
1.5
1 .
2.0
3
4
5
2.0. 1
1.5
1 .
2.0
3
4
5
2.0. 1
431
Select
\i
D
. . .. . .. : .. : = .: : =
9- General
@; Archive File
9 F1le System
:.: .. ;. Preferences
(;- cvs
!i fe, EJB
Java
i2i7 Run/Debug
[:), Tasks
Team
(& Web
it i2;} Web serv1ces
i
- L
Next >
Cancel
. 1 . 3 .
: ,
, ,
Ch09_CustomDialog,
CustomDialog.
.
432
Import Projects
ChO:;IIAdid(F:\S; i;\cho_ellod;;d---
ChOS_FrameLayout (F:\Samples\ChOS_FrameLayout)
ChOS_L1nearLayout (F: \Samples\ChOS_Linearlayout)
ChOS_RelativeLayout (F:\Samples\ChOS_RelatlveLayout)
ChOS_TaieLayout (F:\Samples\ChOS_TaieLayout)
ChO_Button (F:\Samples\ChO_Button)
ChO_Checi<ox (F:\Samples\ChO_Checi<ox)
ChO_Chronometer (F :\Samples\ChO_Chronometer)
!1
Refresh
'---":;.;'n;_....J.; 1
Cancel
({;1
<
Back
. 1 .4.
AbsListView 1 47
Activity Manager 1 3
AdapterV i ew 1 46, 1 47
Adnroid S D K 1 8
Anal ogC lock 1 3 3
A ndro i d Development Too l s 1 6- 1 9
A ndroid Power Management 9
Andro i d SDK 1 7
A ndro i d V i rtual Device 2 1 , 23
AndroidMan i fest. x m l 50
Applicati o n Launcher 3 8 0
A rrayAdapter 1 47
VD Manager 1 8 , 23 , 24, 26
F
Frame an i m ation 40 1
FrameLayout 6 8 , 92
FreeType 1 2
G
Gal lery 1 46, 1 6 1
G r i d V i ew 1 46 , 1 5 3 , 1 5 7
CheckBox 9 8 , 1 0 9
CheckBoxPreference 3 0 2
H i e rarchy V i ewer 2 3 , 8 1
Chronometer 1 3 3 , 1 3 5
H om e screen 26
Content Providers 1 3
H orizontai Scro i i V i ew 92
ContentProvider 1 3 , 3 2 8
Context M e n u 225
Cursor 1 47, 325
l mageB utton 98, 1 1 5
I mage V i ew 95
l ntent 25 5
l ntentF i lter 27 1
lterator 3 3 3
434
Resource M anager 1 3
R i ngtonePreference 3 02
L
Layout Ed itor 63
s
Scro i ! V iew 92
SecurityExcepti on 279
Layout V i ew 8 2
SeekBar 1 20 , 1 25
I ayoutopt 23
SGL 1 2
Iayouts 6 1
Shared Preferences 3 0 3
LinearLayout 6 8 , 1 00
S i mp1eAdapter 1 47, 1 70
Li stActivity 1 4 7
S i mpleCursorAdapter 1 70
Li stPreference 3 0 2
S 1 i d ingDrawer 1 46 , 1 6 5
ListView 1 46- 1 48
Spi nner 1 46 , 1 49
Loupe Y i ew 83
SQLite 1 1
sq1 ite3 23
M e d i a Fram ework 1 1
mksdcard 23
S Q L iteDatabase 3 2 5
SQLiteOpenHelper 3 2 3
SSL 1 2
Status Notification 1 7 5
Stri ng 90
N
Normal broadcasts 2 8 5
Normal Y i ew 8 3
TabH ost 1 1 7
Ta \ eLayout 6 8 , 74
Tab W i d get 1 1 7
Text V i ew 8 5 , 86, 95
on Draw() 3 9 4
Toast Notificati o n 1 7 5- 1 7 8
OpenGL ES 1 1
ToggleB utton 9 8 , 1 1 2
Ordered broadcasts 2 8 5
Package M anager 1 3
Pai ntDrawa \e 3 5 0
Traceview 23
Tween A n imation 40 1 , 40 5 , 4 1 3 , 4 1 7
u
U R I 2 5 5 , 327
P i xel Perfect V i ew 8 1 , 8 3
Preferences 295
ProgressBar 1 2 0- 1 22
Properties Y iew 8 3
v
Y i ew 6 1 , 66
Y i ew System 1 4
R
Rad i oB utton 9 8 , 1 06
RatingBar 1 20 , 1 3 3
Y i ewGroup 6 1 , 66
RelativeLayout 6 8 , 7 8
WebKit 1 2
Request Layout 8 3
W i n dow Manager 1 3
435
1 3 9
)> 275
226
46
226
349, 373
1 3 9
3 5 0
1 8 5
404, 405
S Q L ite 3 2 3
B i o i c 1
1 3
1terator 3 3 3
O C 1 ick L i steer 9 8
OCreateCotext M en u L i steer 9 8
On FocusChangeLi stener 9 8
OnKey L i steer 9 8
On LongC i ickListener 9 8
OnTouch L i stener 9 8
8 5
47
Dalvik 1 2
1 7 5
226, 227
N i n e Patch 3 5 0 , 3 5 1
3 5 0
6 1
4 0 5 , 408
244
6 1
2 5 5
4 3 , 249
1 9 1
X M L 3 6 4
3 6
2 5 5
A l ertDialog 1 93
A 1 phaA i mation 40 1
A al ogC iock 1 3 3 , 1 34
A rcShape 3 8 7 , 3 8 9 , 3 9 0 , 392, 3 9 5 , 3 9 7
A rrayAdapter 1 40
A rray Li s t 1 70
A utoCo m p l eteTextV i ew 1 40
Budle 253
B utton 99
Calen dar 2 1 6
CheckBox 1 09
CheckBoxPreferece 302
Chronom eter 1 3 3 , 1 3 5
I P C 9
2 5 6
226
Cotext 1 3 9 , 295
3 2 7 , 332
Cursor 325
436
(.)
S i m p l eC u rsorAdapter 1 47 , 1 70
CursorA d apter 1 47
S l i d i ngDrawer 1 5 7, 1 64
S p i nner 1 49
D i g itaiC iock 1 3 3 , 1 3 4
S Q L iteDatabase 3 2 5
S Q L iteOpen H e l per 3 2 3
EditText 8 5
String 90
EditTextPreference 3 0 2
Tab H ost 1 1 7
Tab W i dget 1 1 7
F i l e l n putStream 295
F i leOutputStream 296
F rameLayo ut 92
G a l l ery 1 5 7 , 1 6 1
GridV iew 1 5 3
H ashMap 1 7 1
1 m ageB utto n 1 1 5
l m ageY i ew 95
I ntentF i lter 27 1
Layoutlntlater 1 79
L i stAct i v ity 1 47 , 1 4 8
L i stAdapter 1 4 7
L i stPreference 3 0 2
L i st Y i ew 1 47
1 70, 3 2 6 , 3 3 4
M e n u l tem 226
M u l t i A utoCo m p l eteTextY i ew 1 43
Notificati o n 1 83
NotificationManager 1 83
PaintDrawa ie 3 5 0
Path 3 8 8
ProgressBar 1 20 , 1 2 1
Text V i ew 8 5
TimePickerDialog 2 1 6
Toast 1 79
ToggleB utton 1 1 2
Typeface 3 7 5 , 3 76
Y i ew 6 1 , 3 9 8
Y i ewGroup 6 1
R 3 9 , 349, 3 5 2
1 6 1
402
A CT I ON_C A L L 2 5 5
ACTI ON_DATE_C H A N G E D 2 8 7
ACTI ON_E D I 2 5 5
A C T I ON M A I N 2 5 5
A C T I ON - S C R E EN -O F F 2 8 7
A C T I ON S YN C 2 5 5
A C T I ON _T I M EZON E_C H A N G E D 2 8 7
Rad i o B utton 1
CATEGORY _B RO W S A B L E 2 5 6
RatingBar 1 20 , 1 29
CATEGORY _ 2 5 6
C A T E G O R Y_LA UNC H ER 2 5 6
R i n gtonePreference 3 0 2
LENGTH LONG 1 76
Rotate A n i mation 40 1
L ENGTH_SH ORT 1 76
NOTI F I C A T I ON_S E R V I C E 1 84
395, 397
R E S U LT_CAN C E L E D 2 5 8
R E S U LT_F I R ST_U S E R 2 5 8
Scro i i Y i ew 92
R E S U L _ 2 5 8
1 46
SeekBar 1 20 , 1 25
409
S i m p leA dapter 1 4 7
1 76 , 1 84
437
2 2 5 , 2 3 7
-n 1 3 , 4 3 , 4 5 , 3 2 3 , 326
onCreateDialog() 1 92 , 207
onCreateOptionsMenu() 226
3 7 7
377
40, 369, 3 70
n 3 72
o n l temSel ected() 1 5 5
on Pause() 2 5 0 , 2 5 2
onPrepareDialog() 1 93
on ProgressChanged() 1 26
onReind() 277
onRestart() 2 5 0 , 25 1
on Restore l nstanceState() 2 5 3
on Resume() 2 5 0 , 25 1
on Save l n stanceState() 2 5 3
1 3
n 1 4
1 3
n 1 3
n 1 , 1 1
on Start() 2 5 0 , 25 1 , 275
1 3
on StartTrackingTouch() 1 26
1 4
onStop() 2 5 3
2 2 5 , 3 5 0 , 3 60-3 64, 3 6 9
on StopTracki ngTouch() 1 26
onUnind() 277
n 226
onUpgrade() 3 2 4
2 2 5 , 2 3 0 , 23 1
addCategory() 256
q uery() 3 2 5 , 3 2 6 , 329, 3 3 2
removeCategory() 2 5 6
send B roadcast() 2 8 8
getCategories() 2 5 6
setltems() 200
getOnChronometerTickLi stener( ) 1 3 5
getPosition() 3 3 3
setOnChronometerTickLi stener() 1 3 5
getext( ) 90
setO n C i i ck L i stener() 1 1
getype() 3 2 9
i nval idate() 3 94
showDial og() 1 92
makeText() 1 76
startServi ce() 2 7 5
m oveTo F i rst() 3 3 3
m oveTo Last() 3 3 3
moveToNe xt() 3 3 3
moveToPosition() 3 3 3
m oveToPrevious() 3 3 3
onActivityResu lt() 2 5 8
onBind() 277
2 5 7
onContextlte m S e l ected() 2 3 7
2 8 5
4 9 , 2 5 4
438
n
394, 3 9 8 , 399
1 40
r A DT 20, 35
1 4
240
333
42 1
/> 2 7 5
92
2 8 5
2 9 5 , 3 0 1
6 1 , 3 9 8
402
43 , 44, 2 8 5
1 75
1 82
API 2 1
r 7, 9
45
244
47
6 2
22 5 , 2 3 3 , 2 3 6
349, 3 7 7
3 9 3
1 3 9
47
1 4
r 9
1
1 3 9
R .j ava 3 8
5 0
27 1
2 5 6
1 83
47
/++ 1 0
2 8 8
4 3 , 44
2 5 7
1 47
A ndroid 7
2 5 4
3 5 0 , 3 6 8
Go
d
r
nr
N
6n
.n11
All nn
nn Google Android
r ano All e ceii .. . . . . " . . . .. ,
.
,
.
J 60 000 did- ,
. . . . , . . .
Android ! 2 20 1 2
,
, ,
-
- .
Goog le Ad roid .
C D - R O M
. ,
, ,
J ava #. N .
i$jiiei0jkl
I S B N 978-5-9775-0562-8
-d
-111&,
1 90005,
-,
., 29
E-mail: maillibhv.ru
lntemet: www.bhv.ru