Программирование Под android 2014
TRANSCRIPT
-
7/23/2019 Android 2014
1/591
-
7/23/2019 Android 2014
2/591
-
7/23/2019 Android 2014
3/591
-
7/23/2019 Android 2014
4/591
. , .
Android.
.
.
.
. . . .
32.973.2-018.2 004.451 . , .20 Android. . .: , 2014.
592 .: . ( ).
ISBN 978-5-496-00502-9
, - Android. IT- Big Nerd Ranch, - Android, API .
Android- , Flickr, -, , -, . , Android .
12+ ( 12 . 29 2010 . 436-.)
ISBN 978-0321804334 . Authorized translation from the English language edition,entitled Android Programming: The Big Nerd Ranch Guide;ISBN 0321804334; by Hardy, Brian; and by Phillips, Bill;published by The Big Nerd Ranch Guide, Inc.
The Big Nerd Ranch, Inc., 2013ISBN 978-5-496-00502-9 , 2014 ,
, 2014
All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic ormechanical, including photocopying, recording or by any information storage retrieval system, without permission fromPearson Education, Inc.
The Big Nerd Ranch, Inc. .
.
, , , -. , , , .
, 192102, -, . (. ), . 3, , . 7.
005-93, 2; 95 3005 . 30.10.13. 70100/16. . . . 47,730. 1700.
. 432049, , . , .27.
-
7/23/2019 Android 2014
5/591
............................................................................................................16
Android ........................................................................................................18
1. Android .......................................................................24
................................................................................................. 24
Android ........................................................................................................... 25
Eclipse ..................................................................................................................... 28
..................................................................... 29
............................................................................................................. 33
..................................................................................................................... 34
android:layout_width android:layout_height ............................................................................. 34
android:orientation .................................................................................................................... 34
android:text .............................................................................................................................. 35
..................................................................................................... 35
............................................................................................. 36 XML View ............................................................................................... 36
.......................................................................................... 37
........................................................................................... 40
.................................................................................................................. 40
.................................................................................................... 41
.............................................................................................................. 41
.................................................................................................... 42
............................................................................................................................... 43
.......................................................................................................................... 44
............................................................................................................. 45
: Android .................................................. 47
Android ....................................................................................... 49
2.Android MVC ...............................................................................................51
.............................................................................................................. 52
get- set- ............................................................................................... 53
-- Android ..................................................... 55
MVC ..................................................................................................................... 56
............................................................................................. 57
................................................................................................. 59
................................................................................................................... 63
......................................................................................................... 63
..................................................................................... 64
-
7/23/2019 Android 2014
6/591
6
...................................................................................................................... 64
.................................................................................................... 65
XML ......................................................................................................... 66
................................................................................................................................ 67
. TextView ................................................................... 68. .............................................................................. 68
. Button ImageButton ...................................................................................... 68
3. Activity.............................................................................71
Activity .......................................................................... 72
............................................................................................... 72
LogCat ................................................................................................................. 74
..................................................................................... 78
................................................................... 78
.............................................................................. 79
....................................................................................... 82 onSaveInstanceState(Bundle) ............................................................................ 83
Activity ................................................................................................ 84
: onSaveInstanceState(Bundle) ................................................ 85
: .................................................................. 87
4. Android .....................................................................89
DDMS ...................................................................................................................... 90
................................................................................................. 91
.................................................................................................. 92
................................................................................................. 94
....................................................................................................... 95 ................................................................................................... 99
File Explorer ................................................................................................................................100
Android .....................................................................................................101
Android Lint .............................................................................................................................101
R ............................................................................................................103
5. .....................................................................................104
......................................................................................................105
.........................................................................................................106
......................................................................................109
....................................................................................110 Cheat QuizActivity ......................................................................................111
......................................................................................................................113
........................................................................................113
......................................................................................................115
.......................................................................................115
.............................................................................................................116
......................................................................118
..............................................................................................................119
.............................................................................................................120
.............................................................................................................121 Android ....................................................................................123
...............................................................................................................................125
-
7/23/2019 Android 2014
7/591
7
6. Android SDK .......................................................126
Android SDK ....................................................................................................................126
Android ..............................................................................127
Honeycomb ..........................................................................................................128
SDK (Minimum Required SDK) ..................................................................129 SDK (Target SDK) ............................................................................................130
SDK (Compile With) .............................................................................130
API ...................................................130
Lint ..................................................................................133
Android ..........................................................................................134
. ......................................................................................136
7. UI- FragmentManager ........................................................... 138
....................................................................................139
.........................................................................................................140
CriminalIntent ...............................................................................................141 ...........................................................................................................143
..........................................................................................144
Crime ............................................................................................................146
UI-...............................................................................................................147
...................................................................................................148
.........................................................................................149
...........................................................................149
UI- ..........................................................................................................151
CrimeFragment .......................................................................................151
CrimeFragment ..............................................................................................152 .................................................................153
......................................................................................155
UI- FragmentManager ........................................................................156
..........................................................................................................157
FragmentManager .....................................................................159
.................................................................161
: Honeycomb, ICS, Jelly Bean . . ....................................161
8. ..................................................................................... 163
Crime ......................................................................................................................163
.................................................................................................................164 ..............................................................................................................166
XML .....................................................................................167
, ...................................................................................................167
, dp sp ...................................................................................................168
Android ........................................................169
..................................................................................................................170
.......................................................................................................................170
................................................................................171
...................................................................................................173
...................................................................................174
...........................................................................175 ..............................................................................176
android:layout_weight .........................................................................................177
-
7/23/2019 Android 2014
8/591
8
.............................................................................................178
.............................................................178
. ...........................................................................................179
9. ListFragment ..................................................................180
CriminalIntent ..................................................................................181
........................................................................182
ListFragment ................................................................................................................184
.......................................................................185
...........................................................................185
Activity ......................................................................................................186
.......................................................................................188
CrimeListActivity ...................................................................................................189
ListFragment, ListView ArrayAdapter ..........................................................................................191
ArrayAdapter .........................................................................................................193
..................................................................................................195 ......................................................................................................196
...........................................................................................196
.................................................................................................198
10. ...........................................................................202
................................................................................................202
...........................................................................................................203
.................................................................................................................204
CrimeFragment Crime .....................................................205
...................................................................................................206
...........................................................................................................206
.................................................................................207
............................................................................................................208
...............................................................................................................208
................................................................210
11.ViewPager .................................................................................................. 212
CrimePagerActivity ........................................................................................................213
...........................................................................214
..................................................................................214
ViewPager PagerAdapter ........................................................................................................215
CrimePagerActivity ....................................................................................................216
FragmentStatePagerAdapter FragmentPagerAdapter ................................................................219 : ViewPager............................................................................220
12. ..................................................................................... 223
DialogFragment ............................................................................................................225
DialogFragment ..................................................................................................226
............................................................................228
....................................................................................229
DatePickerFragment .....................................................................................230
CrimeFragment .......................................................................................232
............................................................................................233
...................................................................................234 DialogFragment ..................................................................236
. .........................................................................................238
-
7/23/2019 Android 2014
9/591
9
13. MediaPlayer ................................................... 239
.................................................................................................................240
HelloMoonFragment ....................................................................................242
.........................................................................................................243
HelloMoonFragment .......................................................................................244 ...........................................................................................244
..............................................................................................................246
...............................................................248
. .............................................................................248
: ...........................................................................249
. HelloMoon ......................................................................249
14. .........................................................................250
...............................................................................................................250
.........................................................................................251
: ? ................................................................253 onSaveInstanceState(Bundle) ....................................................................................254
: ........................................................256
15. .............................................................................................257
................................................................................................................258
...........................................................................................................258
.........................................................................259
...........................................................................................259
.......................................................................................260
.............................................................................................261
.......................................................................................262
...................................................................................262
...............................................................................................263
....................................................................263
......................................................................................................................264
................................................................................................264
.....................................................................................264
16. ......................................................................................266
........................................................................................................................267
XML ....................................................................................268
.........................................................................................270
....................................................................................................270 ...........................................................................................................273
........................................................................................275
...............................................................................................275
Up ..............................................................................................................277
...................................................................................................279
.................................................................................280
...............................................................................................281
, ....................................................................................................................281
. .........................................................................283
17. ...........................................285
CriminalIntent ...........................................................................285
JSON ................................................................................287
-
7/23/2019 Android 2014
10/591
10
CriminalIntentJSONSerializer ...........................................................................287
JSON Crime .......................................................................288
Crime CrimeLab ......................................................................................289
onPause() ..........................................................................290
...................................................................................291. .....................................................................293
: Android - Java ........................293
..........................................................................................294
18. ............................295
...................................................................................296
..................................................................................................296
..................................................................................................297
.............................................................................................297
...............................................................................................................299
.................................................................................300 .....................................................................................................301
.....................................301
................................................................................304
.........................................305
: ? ..................................................................................306
. CrimeFragment ...................................................................................307
: ActionBarSherlock .....................................................................................307
. ActionBarSherlock ...........................................................................310
ABS CriminalIntent .................................................................................310
............................................................................................311
.....................................................................................311
19. I: Viewfinder ................................................................................313
.......................................................................................................315
CrimeCameraFragment ......................................................................................316
CrimeCameraActivity ..........................................................................................317
..........................................................317
API .........................................................................................................318
..........................................................................................318
SurfaceView, SurfaceHolder Surface ........................................................................................320
..................................................323
CrimeCameraActivity CrimeFragment ..........................................................................325 ........................................................................328
: .................................................329
20. II: ....................................... 331
......................................................................................................................332
.......................................................................................333
..........................................................................................336
CrimeFragment ................................................................................................337
CrimeCameraActivity ..........................................................337
CrimeCameraFragment ......................................................................338
CrimeFragment ...............................................................................339 ........................................................................................................340
Photo ........................................................................................................340
-
7/23/2019 Android 2014
11/591
11
Crime .........................................................341
........................................................................................342
CrimeFragment .............................................................................342
ImageView ...........................................................................................................343
.........................................................................................................345 ImageView ......................................................345
............................................................................................................347
DialogFragment ...................................................349
. Crime ..........................................................................351
. ...........................................................................................351
: Android ..........................................................352
21. ..................................................................................... 354
....................................................................................................................355
.........................................................................................357
......................................................................................................................357 ..............................................................................................359
................................................................................................359
.....................................................................................................................360
Android .......................................................................................................362
..................................................................................364
............................................................................................................365
.......................................................................................366
. .........................................................................................366
22. ..................................................................367
.........................................................................................................................368 SingleFragmentActivity ...........................................................................................369
..................................................................370
- .......................................................................................371
....................................................................................372
: ........................................................................................373
............................................................................374
CrimeFragment.Callbacks .......................................................................................378
: .............................................381
23. .......................................................... 383
NerdLauncher ...........................................................................................383 ......................................................................................................385
.......................................................................387
..............................................................................................................389
NerdLauncher .......................................................391
. , .........................................................................392
: ...................................................................................392
24. ..................................................................................394
RemoteControl ................................................................................................395
RemoteControlActivity ...............................................................................................395
RemoteControlFragment ............................................................................................396 .................................................................................................399
.................................................................................................................401
-
7/23/2019 Android 2014
12/591
12
: include merge ........................................................................................404
. ............................................................................................405
25. .............................................................................406
XML .........................................................................................................407 .......................................................................................................................409
...........................................................................................................411
9- ...............................................................................................................413
26. HTTP ..........................................................................420
PhotoGallery .............................................................................................421
........................................................................................................424
................................................................................................425
AsyncTask ....................................................426
......................................................................................................427
...........................................................................................................428
XML Flickr .................................................................................................................429
XmlPullParser ...................................................................................................433
AsyncTask .................................................................................................435
: AsyncTask ...........................................................................438
AsyncTask ..............................................................................................................439
. ...........................................................................................439
27. Looper, Handler HandlerThread .............................................................440
GridView ...............................................................................440
...........................................................................................................443
...........................................................................................443
.....................................................................................................444 ........................................................................................446
...............................................................................................................447
............................................................................................................447
.................................................................................................448
Handler ....................................................................................................................451
: AsyncTask ..................................................................................455
. ............................................................455
28. .........................................................................................................457
Flickr ..............................................................................................................................457
.............................................................................................................459 ...........................................................................................459
............................................................................................................462
......................................................................................................464
.................................................................................................................465
...........................................................................................465
........................................467
SearchView Android 3.0 ................................................................470
...............................................................................................................................472
29. .....................................................................................474
IntentService ................................................................................................................474 .................................................................................................................477
..................................................................477
-
7/23/2019 Android 2014
13/591
13
...........................................................................................................478
AlarmManager ...................................................................................480
PendingIntent ..........................................................................................................................482
PendingIntent ..........................................................482
.................................................................................................................483 .................................................................................484
...........................................................................................................................486
: ...............................................................................488
( ) ..................................................................................488
........................................................................................................488
........................................................................................................488
............................................................................................................489
................................................................................................................489
...............................................................................................490
.................................................................................................491
30. ...............................................................492
.........................................................................................................493
............................................................................493
....................................................................................................495
.................................................................................496
................................................................................496
......................................................................497
............................................................................................................500
................................................................................................502
..............................503
......................................................................................507
31. - WebView ..........................................................508
Flickr ...................................................................................................508
: .............................................................................................510
: WebView ............................................................................................510
WebChromeClient ...........................................................................................................514
WebView ..................................................................................................................516
: JavaScript .................................................................517
32. .........................519
DragAndDraw ..................................................................................................520 DragAndDrawActivity ......................................................................................520
DragAndDrawFragment ..................................................................................521
...................................................................................522
BoxDrawingView ............................................................................................522
.......................................................................................................524
......................................................................525
onDraw() ..................................................................................................527
: ...............................................................................................................530
33. .........................................531
RunTracker ...............................................................................................531 RunActivity ........................................................................................................533
RunFragment .....................................................................................................................533
-
7/23/2019 Android 2014
14/591
14
...................................................................................................................534
...........................................................................................................................534
RunFragment .................................................................................................534
LocationManager ...........................................................................................535
...................................................537 .....................................539
: ........................................................543
..............................................................544
34. SQLite ..........................................................547
..................................................................................547
..........................................................................................554
CursorAdapter .................................................................556
................................................................................................................559
..............................................................................................561
: .....................................................................................567
35. ................................................................568
Loader LoaderManager ..............................................................................................................568
RunTracker....................................................................................570
................................................................................................................570
.................................................................................................................574
..........................................................................................577
36. ........................................................................................................579
Maps API RunTracker .........................................................................579
....................................................................................579 Google Play services SDK ...............................................................579
Google Maps API ..........................................................................................580
RunTracker .........................................................................................580
..........................................................................581
.........................................................................................................................585
........................................................................589
: .......................................................................................590
37. .............................................................................................591
..............................................................................................................591
.........................................................................................................592 ......................................................................................................................................592
-
7/23/2019 Android 2014
15/591
. , , .
. .
-
7/23/2019 Android 2014
16/591
, . , . .
(Chris Stewart) (Owen Matthews) .
- (Christopher Moor) , .
(Bolot Kerimbaev) (AndrewLunsford) .
(Frank Robles), (Jim Steele), (Laura Cassell), (Mark Dalrymple)
(Magnus Dahl) . (Aaron Hillegass).
, . - . ( , .)
(Susan Loper) , . . , -
. NASA.
.
-
7/23/2019 Android 2014
17/591
17
( Ellie Volckhausen), .
IntelligentEnglish.com - , EPUB Kindle. DocBook
. , Facebook
.
. , , , . - , . .
-
7/23/2019 Android 2014
18/591
Android
Android . Android : - , . , - , . , , .
Android . c Java, Java . Android, . , - .
. , Big Nerd Ranch, , Android :
Android;
, . . - Android. Android, . - , - - , , .
, ,
. , - , Android-.
-
7/23/2019 Android 2014
19/591
19
, Java, , , , , , - , .
, - 2. Java . ; .
- -, Java , . Java (, ).
Java , .
Big Nerd Ranch. , . , -. , .
, .
, , , , - .
, . , . :
.
, .
forums.bignerdranch.com.
Android, .
Android. , . ,
-
7/23/2019 Android 2014
20/591
20 Android
. , .
GeoQuiz
Android, , .CriminalIntent . , - , , , , .
HelloMoon . , , .
NerdLauncher -
.RemoteControl , , .
PhotoGallery Flickr - Flickr. , - , - . .
DragAndDraw - .
RunTracker . , SQLite, .
. , .
. - .
, forums.bignerdranch.com.
?
. -
, . -, , .
-
7/23/2019 Android 2014
21/591
21
, - Android.
. . , . - , . , - .
7 - . . Android- , . ,
. , - , .
, Gingerbread Froyo. - Android Ice Cream Sandwich Jelly Bean, Key Lime Pie. , Froyo Gingerbread. ( Android 6.)
, -
, Froyo Gingerbread. , , Android . , , Gingerbread 40 % .
ADT Bundle
ADT (Android Developer Tools) Bundle. :
Eclipse Android. Eclipse Java, PC, Mac - Linux. Eclipse , , .
Android Developer Tools Eclipse. ADT 21.1. , .
Android SDK Android SDK.
-
7/23/2019 Android 2014
22/591
22 Android
Android SDK .
Android
.
ADT Bundle Android zip-.
1. http://developer.android.com/sdk/index.html.
2. zip- , Eclipse .
3. eclipse Eclipse.
Windows Eclipse , , Java Development Kit ( JDK6), www.oracle.com.
, http://developer.android.com/sdk/index.html .
SDK
ADT Bundle SDK
. Android .
Android SDK Manager. Eclipse WindowAndroidSDKManager.
, Android 2.2 (Froyo), :
SDK Platform;
;
Google API.
, .
Android SDK Manager An-droid , .
-
7/23/2019 Android 2014
23/591
23
.Android SDK Manager
. Android . , - .
, ,
[email protected]( , ). !
- http://www.piter.com .
-
7/23/2019 Android 2014
24/591
Android
, - Android. , - .
., , GeoQuiz. , . - , True False, GeoQuiz .
. 1.1 False.
GeoQuiz (activity) (layout):
Ac-tivity Android SDK. .
, - ,
. 1.1.,
1
-
7/23/2019 Android 2014
25/591
Android 25
Activity. ; .
GeoQuiz ,
Activity QuizActivity. QuizActivity - , . 1.1.
- . , XML. , (, ).
GeoQuiz activity_quiz.xml. XML , . 1.1.
QuizActivity activity_quiz.xml . 1.2.
. 1.2.QuizActivity , activity_quiz.xml
, .
Android
Android. Android , . , Eclipse
File
NewAndroidApplicationProject, .
GeoQuiz(. 1.3). . PackageName com.bignerdranch.android.geoquiz.
: DNS, - . Google Play.
Android. GeoQuiz ,
-
7/23/2019 Android 2014
26/591
26 1. Android
. Android 6.
. 1.3.
Android , . ; . ( , , . . forums.bignerdranch.com, .)
Next.
Createcustomlaunchericon(. 1.4). - GeoQuiz .
, Createactivity .
-
7/23/2019 Android 2014
27/591
Android 27
. 1.4.
Next.
(. 1.5) . Blank Activity( ).
. 1.5.
-
7/23/2019 Android 2014
28/591
28 1. Android
Next.
QuizAc-tivity(. 1.6). Activity .
, , .
. 1.6.
activity_quiz -
. , ; , . , , .
NavigationType None Finish. Eclipse .
Eclipse
Eclipse (workbench window), . 1.7. ( Eclipse, - Eclipse, .)
-
7/23/2019 Android 2014
29/591
29
PackageExplorer. , .
. -
, Eclipse activity_quiz.xml. . , X (. 1.7). . , .
. 1.7.
, Eclipse. , ; , .
Eclipse activity_quiz.xml Android,
. , XML, , .
-
7/23/2019 Android 2014
30/591
30 1. Android
XML, activity_quiz.xml .
activity_quiz.xml
. , XML , 1.1.
1.1.
, activity_quiz.xml :
ADT 21 Android., .
(widgets): RelativeLayout TextView.
, . , - . , , .
Android SDK , -
. View (, TextView Button).
. 1.8 , RelativeLayout TextView, 1.1.
, . QuizActivity :
LinearLayout;
TextView;
LinearLayout;
Button.
. 1.9 , QuizActivity.
-
7/23/2019 Android 2014
31/591
31
. 1.8.
(-
)
(-
)
. 1.9.
-
7/23/2019 Android 2014
32/591
32 1. Android
activity_quiz.xml.
activity_quiz.xml, 1.2. - XML, , ,
XML . - .
, ; , . : , .
, android:text, . . .
1.2. XML (activity_quiz.xml)
-
7/23/2019 Android 2014
33/591
33
android:text="@string/false_button" />
XML , . 1.9. - XML. .
XML. - .
, , .
View, . . 1.10 XML 1.2.
. 1.10.
Lin-
earLayout. XML Androidhttp://schemas.android.com/apk/res/android.
-
7/23/2019 Android 2014
34/591
34 1. Android
LinearLayout View ViewGroup. ViewGroup . , .
ViewGroup
FrameLayout
,TableLayout
RelativeLayout
. ViewGroup, (child) ViewGroup. LinearLayout : TextView LinearLayout. LinearLayout Button.
, .
android:layout_width android:layout_height android:layout_widthandroid:layout_height, , . , match_parent wrap_content:
match_parent .
wrap_content .
( fill_parent. match_parent.)
LinearLayout match_parent. LinearLayout , -, Android .
wrap_content. . 1.9 , .
TextView - android:padding="24dp". , - . (, dp? ,
(density-independent pixels), 8.)
android:orientation
android:orientation LinearLayout, . Lin-earLayout ; LinearLayout- .
. LinearLayout, , - . LinearLayout
-
7/23/2019 Android 2014
35/591
35
. ( - , ; .)
android:text
TextViewButton android:text. ,
: -, .
, XML, - . (,android:text="True"), .
, . 15 , .
, activity_quiz.xml, . .
strings.xml.
PackageExplorer res/values, strings.xml.
; strings.xml - .
. hello_world .
1.3. (strings.xml)
GeoQuiz Hello, world! Constantinople is the largest city in Turkey. True False Settings
( menu_settings . menu_settings , .)
@string/false_button XML GeoQuiz "False" .
-
7/23/2019 Android 2014
36/591
36 1. Android
strings.xml. activity_quiz.xml , - , . ( , , - .)
strings.xml, . . res/values/, resources string, .
.
, . - activity_quiz.xml GraphicalLayout .
. 1.11. (activity_quiz.xml)
XML View
XML activity_quiz.xml View? QuizActivity.
-
7/23/2019 Android 2014
37/591
37
GeoQuiz Activity - QuizActivity. QuizActivity src( Java- ).
PackageExplorer src, com.bignerdranch.android.geoquiz. QuizActivity.java - ( 1.4).
1.4. QuizActivity (QuizActivity.java)
package com.bignerdranch.android.geoquiz;
import android.app.Activity;import android.os.Bundle;import android.view.Menu;
public class QuizActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz); }
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_quiz, menu); return true; }
}
( import, import, .)
Activity: onCreate(Bundle)onCreateOptionsMenu(Menu).
onCreateOptionsMenu(Menu). 16.
onCreate(Bundle) -. , -. , Activity:
public void setContentView(int layoutResID)
(inflates) . , - . , , .
. , , , XML . .
-
7/23/2019 Android 2014
38/591
38 1. Android
res. PackageExplorer, activity_quiz.xml res/layout/. , , res/values/.
. R.layout.activity_quiz.
GeoQuiz, PackageExplorer gen. R.java. Android, , .
1.5. GeoQuiz
/* AUTO-GENERATED FILE. DO NOT MODIFY....
*/
package com.bignerdranch.android.geoquiz;
public final class R { public static final class attr { } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int menu_settings=0x7f070003;
} public static final class layout { public static final int activity_quiz=0x7f030000; } public static final class menu { public static final int activity_quiz=0x7f060000; } public static final class string { public static final int app_name=0x7f040000; public static final int false_button=0x7f040003; public static final int menu_settings=0x7f040006; public static final int question_text=0x7f040001;
public static final int true_button=0x7f040002; } ...}
, R.layout.activity_quiz activity_quiz layout R.
. , :
setTitle(R.string.app_name);
Android , activity_quiz.xml.
-
7/23/2019 Android 2014
39/591
39
. , .
,
android:id. activity_quiz.xml android:id .
1.6. (activity_quiz.xml)
: + android:id, - android:text. , , .
activity_quiz.xml. R.java , R.id .
1.7. (R.java)
public final class R { ... public static final class id { public static final int false_button=0x7f070001; public static final int menu_settings=0x7f070002;
public static final int true_button=0x7f070000; }...
-
7/23/2019 Android 2014
40/591
40 1. Android
, , - QuizActivity. .
QuizActivity.java. ( ; .) .
1.8. (QuizActivity.java)
public class QuizActivity extends Activity {
private Button mTrueButton; private Button mFalseButton;
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_quiz);
}...}
, m ( ). Android, .
. -
: Button (Button cannot be resolvedto a type).
, android.widget.Button QuizActivity.java. :
import android.widget.Button;
.
Eclipse , Java Android SDK. , .
, :
Command+Shift+O Mac;
Ctrl+Shift+O Windows Linux.
. ( , XML.)
-. :
-
7/23/2019 Android 2014
41/591
41
View;
, -.
, Activity:
public View findViewById(int id)
View.
QuizActivity.java - . , View Button.
1.9. (QuizActivity.java)
public class QuizActivity extends Activity {
private Button mTrueButton; private Button mFalseButton;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button)findViewById(R.id.true_button); mFalseButton = (Button)findViewById(R.id.false_button); }...}
Android (event-driven).
, , -. ( , , , .)
, , . , , (listener). .
Android SDK , -
. - , View.OnClickListener.
-
7/23/2019 Android 2014
42/591
42 1. Android
True. QuizActivity.java onCreate() .
1.10. True (QuizActivity.java)
... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_quiz);
mTrueButton = (Button)findViewById(R.id.true_button); mTrueButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // , ! }
});
mFalseButton = (Button)findViewById(R.id.false_button);}
}
( View cannot be resolved to a type, - Command+Shift+O Ctrl+Shift+O View.)
1.10 , But-ton mTrueButton. setOnClickListener(OnClickListener) , OnClickListener.
. , - ; : , , setOnClickListener(OnClickListener). , .
mTrueButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // , ! } });
-. , , , .
OnClickListener, - onClick(View).
-
7/23/2019 Android 2014
43/591
43
onClick(View), . , onClick(View) , , .
( , , Java , , .)
False.
1.11. False (QuizActivity.java)
... mTrueButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // , !
} });
mFalseButton = (Button)findViewById(R.id.false_button); mFalseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // , !
} }); }
- . - (toast) , - , , . , (. 1.12).
strings.xml ,
.
1.12. (strings.xml)
GeoQuiz Constantinople is the largest cityin Turkey. True False Correct! Incorrect!
Settings
-
7/23/2019 Android 2014
44/591
44 1. Android
. 1.12.
Toast:public static Toast makeText(Context context, int resId, int duration)
Context Activity(Activity - Context). , . Context Toast . Toast, .
, Toast.show(), .
QuizActivity makeText() - ( 1.13). , Eclipse.
, -.
1.13. Toast, Toast.
-
7/23/2019 Android 2014
45/591
45
, Tab, . ( -, . Tab
.) makeText(Context,int,int). .
; QuizActivity.this. Tab, -, , 1.13.
1.13. (QuizActivity.java)
... mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { Toast.makeText(QuizActivity.this,
R.string.incorrect_toast,Toast.LENGTH_SHORT).show();
} });
mFalseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(QuizActivity.this,
R.string.correct_toast,Toast.LENGTH_SHORT).show();
} });
makeText() QuizActivity Context. , this. - , this View.OnClickListener.
, Toast . - , .
Android -. Android, .
Android (AVD, Android Virtual Device), WindowAndroidVirtualDeviceManager. AVDManager, New... .
- . Galaxy Nexus GoogleAPIs
-
7/23/2019 Android 2014
46/591
46 1. Android
(GoogleInc.)APILevel17, . 1.13. Windows, , AVD (RAM) 1024 512. OK.
Windows
512
1024
. 1.13. Android
,
GeoQuiz. PackageExplorer GeoQuiz. RunAsAndroidApplication. Eclipse , - . Eclipse , LogCat .
, , - GeoQuiz . . ( , , , AVD . AVD -
.)
-
7/23/2019 Android 2014
47/591
: Android 47
GeoQuiz , LogCat. ; . Text
, .
. 1.14. NullException 21
. .
; , , . Back(U- ), Eclipse, .
, -. 2 GeoQuiz , .
: Android
, , - Android. , Eclipse , . - Android , AndroidManifest.xml( ) .apk. , .
( .apk , . Android http://http://developer.android.com/tools/publishing/preparing.html.)
activity_quiz.xml View ? - aapt(Android Asset Packaging Tool) . - .apk. , setContentView() onCreate() QuizActivity, QuizActivity LayoutInflater
View, .
-
7/23/2019 Android 2014
48/591
48 1. Android
( XML. , -
, SDK; 3.)
, XML, , 8.
Java
-
Java (.class)
-
dalvik
-
dalvik (.dex)
-
-
apk
Android
(.apk)
. 1.15. GeoQuiz
-
7/23/2019 Android 2014
49/591
Android 49
. 1.16. activity_quiz.xml
Android
, , Eclipse. ADT - Android (, aapt), Eclipse.
, - - Eclipse. - . maven
ant. Ant , . :
, ant .
, tools/ platform-tools/ Android SDK .
:
$ android update project -p .
-
7/23/2019 Android 2014
50/591
50 1. Android
Eclipse build.xml ant. - build.xml ; .
. .apk,
:$ ant debug
. .apk, - bin/--debug.apk. .apk , :
$ adb install bin/--debug.apk
, .
-
7/23/2019 Android 2014
51/591
Android MVC
GeoQuiz .
. 2.1. !
GeoQuiz TrueFalse.
/. TrueFalse, QuizActivity.
2
-
7/23/2019 Android 2014
52/591
52 2.Android MVC
PackageExplorer com.bignerd-ranch.android.geoquiz NewClass. TrueFalse, java.lang.Object Finish.
. 2.2. TrueFalse
TrueFalse.java :
2.1. TrueFalse
public class TrueFalse { private int mQuestion;
private boolean mTrueQuestion;
public TrueFalse(int question, boolean trueQuestion) { mQuestion = question;
mTrueQuestion = trueQuestion; }}
-
7/23/2019 Android 2014
53/591
get- set- 53
mQuestion int, String? ( int) . . mTrueQuestion
, . get- set-. - Eclipse .
get- set-
Eclipse m - is get .
Eclipse ( Eclipse Mac, WindowsPreferences
Windows). Java CodeStyle. Conventionsforvariablenames: Fields(. 2.3). Edit m . s - . ( GeoQuiz s , .)
, Use'is'prefixforgettersthatreturnboolean -. OK.
. 2.3. Java
-
7/23/2019 Android 2014
54/591
54 2.Android MVC
? Eclipse get- mQuestion, getQuestion()getMQuestion() isTrueQuestion() isMTrueQuestion().
TrueFalse.java, SourceGenerateGettersAndSetters... SelectAll, get- set- .
()
. 2.4. GeoQuiz
OK. Eclipse .
2.2. get- set-
public class TrueFalse { private int mQuestion;
private boolean mTrueQuestion;
public TrueFalse(int question, boolean trueQuestion) { mQuestion = question; mTrueQuestion = trueQuestion; }
public int getQuestion() { return mQuestion; }
public void setQuestion(int question) {
mQuestion = question; }
-
7/23/2019 Android 2014
55/591
-- Android 55
public boolean isTrueQuestion() { return mTrueQuestion; }
public void setTrueQuestion(boolean trueQuestion) { mTrueQuestion = trueQuestion; }}
TrueFalse. QuizActivity TrueFalse, , GeoQuiz .
QuizActivity TrueFalse. TextView Button .
-- Android
, , . 2.4 : -, . Android , --, MVC (Model-View-Controller). MVC, - , .
-. - , , , , / . . ; .
Android . .
GeoQuiz TrueFalse.
, . : - , .
Android . . - .
GeoQuiz , - activity_quiz.xml.
; - . ,
-
7/23/2019 Android 2014
56/591
56 2.Android MVC
, .
Android Activity, Fragment
Service. ( 7, 29.) GeoQuiz QuizActivity.
. 2.5 - , . : ; -, .
-
-
,
. 2.5. MVC
MVC
, - . ; , .
, - ; , .
GeoQuiz ,
. GeoQuiz - Next. TrueFalse.
-
7/23/2019 Android 2014
57/591
57
MVC . , , .
, TrueFalse , /. TrueFalse . , -, , .
MVC GeoQuiz Next.
Android XML . GeoQuiz activity_quiz.xml. , . 2.6. ( , .)
. 2.6.
, :
android:textTextView. .
TextView android:id. , QuizActivity.
Button LinearLayout.
activity_quiz.xml .
2.3. TextView (activity_quiz.xml)
-
7/23/2019 Android 2014
58/591
58 2.Android MVC
2.3()
android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp"
android:text="@string/question_text" />
...
activity_quiz.xml. , .
res/values/strings.xml . .
2.4. (strings.xml)
... GeoQuiz Constantinople is the largest city
in Turkey. True False Next Correct!
...
strings.xml, , .
2.5. (strings.xml)
... Incorrect!
Settings The Pacific Ocean is larger than
the Atlantic Ocean. The Suez Canal connects the Red Sea
and the Indian Ocean. The source of the Nile River is in Egypt. The Amazon River is the longest river
in the Americas.
Lake Baikal is the world\'s oldest and deepestfreshwater lake.
...
-
7/23/2019 Android 2014
59/591
59
\' - . , \n
. . activity_quiz.xml .
, GeoQuiz. QuizActivity.
GeoQuiz QuizActivity
. , activity_quiz.
xml, .
, , QuizActivity GeoQuiz.
QuizActivity.java. TextView Button. TrueFalse .
2.6. TrueFalse (QuizActivity.java)
public class QuizActivity extends Activity {
private Button mTrueButton; private Button mFalseButton; private Button mNextButton; private TextView mQuestionTextView;
private TrueFalse[] mQuestionBank = new TrueFalse[] { new TrueFalse(R.string.question_oceans, true), new TrueFalse(R.string.question_mideast, false), new TrueFalse(R.string.question_africa, false) new TrueFalse(R.string.question_americas, true),
new TrueFalse(R.string.question_asia, true), };
private int mCurrentIndex = 0; ...
TrueFalse - TrueFalse.
( . . .)
mQuestionBank, mCurrentIndex TrueFalse .
-
7/23/2019 Android 2014
60/591
60 2.Android MVC
TextView - .
2.7. TextView (QuizActivity.java)
public class QuizActivity extends Activity { ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView)findViewById(R.id.question_text_view); int question = mQuestionBank[mCurrentIndex].getQuestion(); mQuestionTextView.setText(question);
mTrueButton = (Button)findViewById(R.id.true_button); ... }}
. GeoQuiz. TextView.
Next. , - View.OnClickListener. TextView.
2.8. (QuizActivity.java)public class QuizActivity extends Activity { ...
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView)findViewById(R.id.question_text_view); int question = mQuestionBank[mCurrentIndex].getQuestion(); mQuestionTextView.setText(question);
... mFalseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show(); } });
mNextButton = (Button)findViewById(R.id.next_button); mNextButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
-
7/23/2019 Android 2014
61/591
61
int question = mQuestionBank[mCurrentIndex].getQuestion(); mQuestionTextView.setText(question); } });
}}
mQuestionTextView . , 2.9. - mNextButton onCreate(Bundle) .
2.9. updateQuestion() (QuizActivity.java)
public class QuizActivity extends Activity { ...
private void updateQuestion() { int question = mQuestionBank[mCurrentIndex].getQuestion(); mQuestionTextView.setText(question); }
@Override protected void onCreate(Bundle savedInstanceState) { ... mQuestionTextView = (TextView)findViewById(R.id.question_text_view); int question = mQuestionBank[mCurrentIndex].getQuestion(); mQuestionTextView.setText(question);
mNextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length; int question = mQuestionBank[mCurrentIndex].getQuestion(); mQuestionTextView.setText(question); updateQuestion(); } });
updateQuestion(); }}
GeoQuiz Next.
, . - , .
, QuizActivity, :
private void checkAnswer(boolean userPressedTrue)
, ,
: True False. TrueFalse. , .
-
7/23/2019 Android 2014
62/591
62 2.Android MVC
QuizActivity.java checkAnswer(boolean), 2.10.
2.10. checkAnswer(boolean) (QuizActivity.java)
public class QuizActivity extends Activity { ... private void updateQuestion() { int question = mQuestionBank[mCurrentIndex].getQuestion(); mQuestionTextView.setText(question); }
private void checkAnswer(boolean userPressedTrue) { boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
int messageResId = 0;
if (userPressedTrue == answerIsTrue) { messageResId = R.string.correct_toast; } else { messageResId = R.string.incorrect_toast; }
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT) .show(); }
@Override protected void onCreate(Bundle savedInstanceState) {
... }}
checkAnswer(boolean), - 2.11.
2.11. checkAnswer(boolean) (QuizActivity.java)
public class QuizActivity extends Activity { ... @Override
protected void onCreate(Bundle savedInstanceState) { ...
mTrueButton = (Button)findViewById(R.id.true_button); mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { Toast.makeText(QuizActivity.this,
R.string.incorrect_toast,Toast.LENGTH_SHORT).show();
checkAnswer(true);
} });
-
7/23/2019 Android 2014
63/591
63
mFalseButton = (Button)findViewById(R.id.false_button); mFalseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
checkAnswer(false); } });
mNextButton = (Button)findViewById(R.id.next_button); ... }}
GeoQuiz . .
, GeoQuiz .
. Mac, . Windows adb(Android Debug Bridge). Windows adb, .
, , Devices. DDMS, DDMS Eclipse. Devices . AVD, .
, Java .
, - adb. Devices , , . . Resetadb. , - .
, - Android. http://developer.android.com/tools/device.
html forums.bignerdranch.com .
-
7/23/2019 Android 2014
64/591
64 2.Android MVC
, - Google Play.
Android 4.1 Settings Applications. , Unknownsources.
Android 4.2 SettingsSecurity Unknownsources.
USB.
Android 4.0 SettingsApplicationsDevelopment USBdebugging.
Android 4.0 4.1 SettingsDeveloperoptions.
Android 4.2 Developeroptions . , SettingsAboutTablet/Phone BuildNumber7 . Settings, Developeroptions USBdebugging.
, . , http://developer.android.com/tools/device.html.
GeoQuiz , . Eclipse
, . ; GeoQuiz . ( , GeoQuiz , , - .)
GeoQuiz , - , Next , .
(http://www.bignerdranch.com/solutions/AndroidProgramming.zip). Eclipse .
02_MVC/GeoQuiz/res . drawable-hdpi, res/drawable-mdpi drawable-xhdpi.
.
mdpi (~160 dpi)
hdpi (~240 dpi)
xhdpi (~320 dpi)
-
7/23/2019 Android 2014
65/591
65
( ldpi, .)
arrow_right.png arrow_left.png.
, . - , - . , .
GeoQuiz. PackageExplorer res -, .
PackageExplorer.
. 2.7. drawable GeoQuiz
, , - . 2.8. CopyFiles, .
-
7/23/2019 Android 2014
66/591
66 2.Android MVC
. 2.8. ( )
. .png,
.jpg .gif, res/drawable, - . (, .)
, , gen/R.java R.drawable. - : R.drawable.arrow_left R.drawable.arrow_right.
, ; - .
, . Android 3 . .
XML
. - Next, . XML?
, . activity_quiz.xml Button.
2.12. Next (activity_quiz.xml)
... ...
-
7/23/2019 Android 2014
67/591
67
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next_question_button" android:drawableRight="@drawable/arrow_right"
android:drawablePadding="4dp"/>
XML . @string/. @drawable/.
res , - 3.
GeoQuiz . , , .
, GeoQuiz . GeoQuiz Next, , . ( , Control+F12/Ctrl+F12.)
. - ?
- 3.
. - , . , .
. , - Android Android.
- , , . , forums.bignerdranch.com. - , , .
, Eclipse .
PackageExplorer, Copy, .
-
7/23/2019 Android 2014
68/591
68 2.Android MVC
, Package Explorer .
. TextView Next, , - TextView.
. TextView View.OnClickListener , Button, TextView - View.
.
. , . 2.9.
. 2.9. !
. , .
. Button ImageButton
, , .
-
7/23/2019 Android 2014
69/591
69
. 2.10.
ImageButton( Button).
ImageButton ImageView Button, TextView.
. 2.11. ImageButton Button
text
drawable
Next
ImageView
:
-
7/23/2019 Android 2014
70/591
70 2.Android MVC
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next_question_button" android:drawableRight="@drawable/arrow_right"
android:drawablePadding="4dp" android:src="@drawable/arrow_right" />
, QuizActivity, ImageButton.
ImageButton, Eclipse - android:contentDescription. . , , ( - ).
, android:contentDescription Image-Button.
-
7/23/2019 Android 2014
71/591
Activity
Activity . - : -, . Activity , - . . 3.1 , .
Activity , - . 3.1, .
onCreate(Bundle). , .
, onCreate() :
(setContentView(int));
;
;
.
, onCre-ate() Activity . - , Android .
-
-
(
)
( )
( )
. 3.1. Activity
3
-
7/23/2019 Android 2014
72/591
72 3. Activity
Activity
, QuizActivity. .
Android android.util.Log . Log . :
public static int d(String tag, String msg)
d debug () .( Log .) , .
TAG, . .
QuizActivity.java TAG QuizActivity.
3.1. TAG (QuizActivity.java)
public class QuizActivity extends Activity {
private static final String TAG = "QuizActivity";
...}
onCreate() Log.d() .
3.2. OnCreate() (QuizActivity.java)
public class QuizActivity extends Activity { ... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate(Bundle) called");
setContentView(R.layout.activity_quiz);
...
Eclipse , Log, Command+Shift+O(Ctrl+Shift+O) . Eclipse ; android.util.Log.
QuizActivity.
-
7/23/2019 Android 2014
73/591
Activity 73
3.3. (QuizActivity.java)
} // onCreate(Bundle)
@Overridepublic void onStart() {super.onStart();Log.d(TAG, "onStart() called");}
@Overridepublic void onPause() {super.onPause();Log.d(TAG, "onPause() called");}
@Overridepublic void onResume() {super.onResume();Log.d(TAG, "onResume() called");}
@Overridepublic void onStop() {super.onStop();Log.d(TAG, "onStop() called");}
@Overridepublic void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy() called");}
}
-. , - onCreate(); .
, @Override. , ., :
public class QuizActivity extends Activity {
@Override public void onCreat(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz); }
...
-
7/23/2019 Android 2014
74/591
74 3. Activity
Activity onCreat(Bundle), . , QuizActivity.onCreat(Bundle).
LogCat
, LogCat , - Android SDK.
LogCat, - WindowShowViewOther...
Android LogCat, OK(. 3.2).
LogCat , . , LogCat .
, LogCat - -
(. 3.3).
LogCat , . ; LogCat .
Eclipse , . 3.4. LogCat, ( Eclipse).
GeoQuiz; LogCat . . . TAG LogCat TAG, QuizActivity.
( , , LogCat . WindowShowViewOther... De-vices. , , LogCat.)
, TAG. LogCat + ;
. QuizActivity QuizActivity byLogTag:(. 3.5).
. 3.2. LogCat
-
7/23/2019 Android 2014
75/591
LogCat 75
. 3.3. LogCat
. 3.4. Eclipse LogCat
-
7/23/2019 Android 2014
76/591
76 3. Activity
. 3.5. LogCat
OK; , QuizActivity(. 3.6). GeoQuiz QuizActivity.
. 3.6. GeoQuiz ,
( , QuizActivity LogCat.)
. Back, - LogCat. onPause(),onStop() onDestroy().
. 3.7. Back
Back, Android: , . Android , .
-
7/23/2019 Android 2014
77/591
LogCat 77
GeoQuiz. Home LogCat. onPause() onStop(), onDestroy().
. 3.8. Home
. Recents Home(. 3.9). Recents Home.
Recents
Home
Back
. 3.9. Home, Back Recents
GeoQuiz LogCat. , .
Home Android: , . Android , .
-
7/23/2019 Android 2014
78/591
78 3. Activit