㳿 . ' - ', , . ˳ Android iPhone. - .
, , .
: ;
;
; .
ϳ , , . . , .
1.
ղ
: Android, ; , .
Android , :
1. Java Development Kit (JDK) Java, Java (javac), Java, , , Java Runtime Environment, JRE (:http://java.sun.com/javase/downloads/index.jsp).
2. Eclipse IDE Java, Android- ( :http://www.eclipse.org/downloads/).
3. Android SDK (Software Development Kit) Android-, ( ࠠ ::http://developer.android.com/sdk/index.html). Android SDK :
API Android SDK API- Android, ;
SDK;
Development Tools , , ;
|
|
Sample Code , Android.
4. Android Development Tools (ADT) , Android, Eclipse.
ADT Eclipse :
New Project Wizard, Android- ;
Layout Editor ;
, XML-.
, *. xe. ϳ JDK, Eclipse IDE Android SDK ADT. Eclipse Help | Install NewSoftware.
, , Add. location https://dl-ssl.google.com/android/eclipse/. ϳ Eclipse.
' Eclipse Android SDK. Eclipse Window | Preferences, Android SDK location , Android SDK.
1.2
, SDK :
android , , , , AndroidSDK, 蠠 ;
Dalvik Debug Monitor Service (DDMS) Dalvik Virtual Machine, Android, , . , , , ' , , ;
Hierarchy Viewer , , . ³ View, ;
Layoutopt , . , ;
Draw 9- patch , NinePatch- ;
Sqlite3 SQLite, Android-;
Traceview , ;
Mksdcard , ' (, SD).
|
|
, Android SDK, : Path Android SDK (, C:\eclipse\adt\sdk\platform-tools C:\eclipse\adt\sdk\tools).
1.3 Android-
Android Virtual Device ( Android) ,'.
, .
Android- , AVD. AVD , .
AVD :
1. android.
2. Android SDK and AVD Manager. Android SDK Eclipse, Window | Android Virtual Device Manager. Android VirtualDevices New
Create new Android Virtual Device ( 1.1):
Name ;
Device ;
Target Android SDK, Android-;
Hardware keyboard present , , ';
Display a skin with hardware controls (home, menu, back ..). .
Memory Options , ;
Internal Storage ' ;
SD Card ', Size ;
Emulator Option Use Host GPU (Graphics processing unit) , Snapshot , ' , .
1.1 AVD
Android Virtual Device Manager Device Definitions NewDevice.
Name ';
Screen Size Resolution ( ) ( ) . ;
Sensors : (Accelerometer), (Gyroscope), GPS (ProximitySensor);
Cameras : Front , Rear
;
Input : Keyboard , DPad
, Trackball ;
RAM ' ;
Size (). Android4: small (<
3.55 in), normal (3.55 5 in), large (5 7.5in), xlarge (> 7.5 in);
Screen Ratio : long (, WQVGA, WVGA, FWVGA), nolong (, QVGA, HVGA, VGA);
Density . dp. ' . , 100×100px , 100×100dp .
7 : nodpi dp 1px, , ldpi ~ 120dpi, mdpi ~ 160dpi, hdpi ~ 240dpi, xhdpi ~ 320dpi, xxhdpi >400dpi, tvdpi mdpi hdpi (~213dpi);
|
|
Buttons (home, menu ): hardware ( ) software ( ).
Device State (Portrait Landscape ) (DPad Trackball).
Android
Android SDK 1.2.
蠠 Android Linux, , , .
1.2 Android SDK
, , /++, OpenGL, WebKit, FreeType, Secure Sockets Layer (SSL), (libc), SQLite Media. , Berkeley Software Distribution (BSD), , Linux. PocketVideo OpenCORE. ֳ -.
Surface Manager 2D 3D. WebKit . FreeType . SQLite , . , SQLite , ' Android. , SQLite, Android.
Dalvik Virtual Machine (Dalvik VM), Android . Dalvik VM. Java , .
Java , , , , (), ( , ..). Java. Android Google Skia, 2D , /++ 3D OpenGL ES, . , Android , . ' Android API Bluetooth, EDGE, 3G, Wi-Fi ' (GSM), .
Java Android
, Android SDK: android.app ( ), android.bluethooth, android.content ( ), android.database ( ), android.gesture ( ), android.graphics, android.hardware ( ),
android.net (API ), android.location ( '), android.media, android.opengl, android.os ( : , , ), android.preference ( ), android.provider ( , android.content), android.sax ( API XML), android.speech ( ), android.telephony ( ), android.text (, , ), android.units, android.view, android.webkit (, ), android.widget ( ), com.google.android.maps ( Google). SDK .
|
|
Android-
Android-
Eclipse File|New|Project..., Android|Android Application Project Next. New Android Project :
Application name (' , ) SimpleProject;
Project name (' ' , ) SimpleProject;
Package name (' ) com.example.simpleproject;
Minimum Required SDK , API;
Target SDK API ( SDK);
Compile With Android;
Theme .
, Next
.
Create custom launcher icon, ;
Createactivity;
Mark this project as library, ;
Create Project in Workspace . .
. ϳ Next Finish.
䳿, Run ( 1.3). Android-.
1.3 ³ SimpleProject
1.6.2 Android-
ADT Android- , - Java- ( 1.4):
scr , ;
assets ;
res , ;
drawable , ;
anim , XML , , ;
layout , ( XML , );
menu , XML , , ;
values , , (, , ), XML ;
xml , XML , ;
raw , , ;
AndroidManifest.xml Android. , , (intent receiver) . , , , . , (instrumentation detail), .
MainActivity.java. package com.example.simpleproject; importandroid.os.Bundle;
import android.app.Activity; importandroid.view.Menu;
public class MainActivity extends Activity {
1.4 SimpleProject
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true;
}
}
( Package Name).
. , (extends) Activity. onCreate()
|
|
, .
super.onCreate (savedInstanceState) , .
setContentView (R.layout.activity_main) . ' res/layout/ ( activity_main.xml).
' lab.xml, : setContentView (R.layout.lab);
, .
activity_main.xml: ' ' ().
() , /res/values: strings.xml Add. , ' .
AndroidManifest.xml
Android- , . .
<manifest> , : xmlns:android ( Android) 젠 http://schemas.android.com/apk/res/android, package ( ), android:versionCode ( ) android:versionName ( );
<uses-permission> , . ʳ . , <uses-permission android:name = "android.permission. RECEIVE_SMS"/>;
<permission> , , , ,: android:name ( ), android:label (' , ), android:description (), android:icon ( ), android:permissionGroup ( ) android:protectionLevel ();
<permission-tree> ' ();
<permission> ();
<instrumentation> ;
<uses-sdk> Android ( minSdkVersion);
<uses-configuration> ;
<uses-feature> , ;
<supports-screens> , ;
<application> , , . / , , , , , .. : <activity> android:name, android:label ( <activity> <Intent-filter>, -볺 Intent , , ,<action>,
<category>, <data>), <activity-alias> Activity,
<receiver> intent filter ( , , ), <service> intent - filter ( ), <provider>- , ,
<uses-library> .
Android- , : , , . 1.5.
1.5 ³
, ;
;
;
;
.
1. Android.
2. Dalvik VirtualMachine.
3. ³ API.
4. Java Android.
5. AVD ().
6. .
7. /res/values/.
8. AndroidManifest.xml.
2
Բ . Ͳ ² IJ
: , , .
2.1
(Activity). ( , View) , . , , , , , .
android- ᒺ View ViewGroup. View ViewGroup ᒺ View. ᒺ View . View () , . ViewGroup , , (layouts). ֳ View ViewGroup ( 2.1). Activity View ViewGroup.
. , -. onCreate() setContentView()
ࠠ . , mail.xml:
ViewGroup |
| |||||
|
| ViewGroup |
| |||
| ||||||
|
| View | ||||
| ||||||
| ||||||
| View | |||||
| ||||||
| ViewGroup |
| ||||
| ||||||
|
| View | ||||
| ||||||
| ||||||
| View | |||||
| ||||||
| ||||||
| View | |||||
| ||||||
| View |
| ||||
| ||||||
| ||||||
| View |
| ||||
| ||||||
2.1 View ViewGroup Activity
//
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);
}
, ViewGroup:
FrameLayout;
LinearLayout;
TableLayout;
RelativeLayout.
.
FrameLayout
FrameLayout 젠 . , View ViewGroup. (2.2).
2.2 FrameLayout
. , . , : layout_width= match_parent layout_height=match_parent. - : match_parent ( ) wrap_content ( ). , : android:id, android:layout_width, android:layout_height, android:text.
FrameLayout , . . , , ( , .),
(, , , .).
, FrameLayout, 2.2.
<FrameLayout xmlns:android=http://schemas.android.com/ apk/res/android
android:layout_android:layout_height=match_parent>
<Button
android:id=@+id/b1 android:layout_heigth=match_parent android:layout_android:text=Button1
/>
<Button
android:id=@+id/b2 android:layout_heigth=wrap_content android:layout_android:text=Button2
/>
</FrameLayout>
LinearLayout
LinearLayout , android:orientation: horizontal, vertical.
, LinearLayout, 2.3.
2.3 LinearLayout
<LinearLayout xmlns:android=http://schemas.android.com/ apk/res/android
android:layout_android:layout_height=match_parent android:orientation=vertical>
<Button
android:id=@+id/b1 android:layout_heigth=wrap_content android:layout_android:text=Button1
/>
<Button
android:id=@+id/b2 android:layout_heigth=wrap_content android:layout_android:text=Button2
/>
</LinearLayout>
LinearLayout android: layout_weight, , ( , ).
, - . Android-.
TableLayout
TableLayout . TableLayout . TableLayout . . ᒺ TableRow. ᒺ View ViewGroup.
TableLayout , LinearLayout. TableLayout , ( 2.4).
, TableLayout, 2.4.
2.4 TableLayout
<TableLayout xmlns:android=http://schemas.andro id.com/apk/res/android
android:layout_android:layout_height=match_parent>
<TableRow androd:id=@+id/TR1
android:layout_heigth=wrap_content android:layout_width=match_parent>
<Button
android:id=@+id/b1 android:layout_heigth=wrap_content android:layout_android:text=1
/>
</TableRow>
<TableRow androd:id=@+id/TR2
android:layout_heigth=wrap_content android:layout_width=match_parent>
<Button
android:id=@+id/b4 android:layout_heigth=wrap_content android:layout_android:text=4
/>
</TableRow>
</TableLayout>
RelativeLayout
RelativeLayout ᒺ . RelativeLayout , , . XML-, , ᒺ , , . RelativeLayout , . , TextView button2, : android:layout_toLeftOf=@id/Button2. : android:layout_above, android:layout_below, android:layout_toLeftOf, andr oid:layout_toRightOf. : android:layout_ alignBaseline, android:layout_alignBottom, android: layout_alignLeft, android:layout_alignRight, andro id:layout_alignTop. : android:layout_alignParentBottom, android:layout_ alignParentLeft, android:layout_alignParentRight, android:layout_alignParentTop, android:layout_ centerHorizontal, android:layout_centerInParent, android:layout_centerVertical.
2.4RelativeLayout
2.3
ᒺ View, 䳿 . . Android , , , , , , , .
. : id, layout_width, layout_height, text, gravity.
android:id=@+id/view1, view1 id ; @ , XML ; + , , R.java, Android .
android:( ) android:( , - , ).
android:width wrap_content match_parent .
ࠠ android:text=something 蠠 android:text= @string/res_str, res_str ( /res/values).
android:gravity(left, right, center .).
2.3.1 ³ TextView
TextView , ( 2.5). ³ TextView , (, , ). ,:
android:textSize( px, dp, sp, in, pt, mm);
android:textStyle(normal, bold, italic);
android:textColor ( #RRGGBB #AARRGGBB);
android:autoLink ( web, phone, email, map, all,none).
2.5 TextView
( *.java):
// id 1
TextView T1=(TextView) findViewById(R.id.T1);
// T1.setText(something);
2.3.1 ³EditText
EditText ( 2.6). getText() EditText. android:hint=Enter text . setSelection(). TextView.
2.6 ³ EditText
⠠ Android ScrollView HorizontalScrollView, ViewGroup. ֳ FrameLayout, , . , , ᒺ. LinearLayout .
³ ScrollView , . ScrollView , HorizontalScrollView .
// main.xml
<ScrollView
android:id=@+id/s_ver android:layout_android:layout_height=wrap_content >
<HorizontalScrollView android:id=@+id/s_hor android:layout_android:layout_height=match_parent >
// (, TextView)
<TextView
android:id=@+id/t android:layout_android:layout_height=wrap_content android:isScrollContainer=true />
</HorizontalScrollView>
</ScrollView>
// project.java
import android.app.Activity; import android.os.Bundle; importandroid.widget.TextView;
public class Project extends Activity{
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);
TextView t=(TextView)findViewById(R.id.t); t.setText(); //
} }
2.3.3 ³
2.3.4 ³ ImageView. ImageView , . (
*.java):
setImageResource(int resId) ;
setImageURI(Uri im_uri) URI;
seyImageBitmap(Bitmap bitmap) .
ImageView setMaxHeight(), setMaxWidth(), getMinimumHeight(), getMinimumWidth(), getScaleType(), setScaleType().
XML- android:scr. /res/drawable/.
// 1 . ( pic1 *.xml)
<ImageView
android:id=@+id=image1 android:layout_android:layout_height=wrap_content android:scr=@drawable/pic1>
// 2 . ( *.xml, pic2 *.java)
// *.xml
<ImageView
android:id=@+id=image2 android:layout_android:layout_height=wrap_content>
// *.java
import android.widget.ImageView;
public class Progect extends Activity{
final ImageView image2 =(ImageView) findViewById (R.id.image2);
image2.setImageResource(R.drawable.pic2);
}
2.4 .
ϳ , . .
View , OnListener(), . . Android, View, , .
View :
OnClickListener;
OnLongListener;
OnFocusChangeListener;
OnKeyListener;
OnTouchListener;
OnCreateContextMenuListener.
, , (Activity) (, OnClickListener) (, onClick()), 䳿, 䳿 , , setOnClickListener.
Button
Button () ( 2.7). 䳿 onClick.
2.7 ³ Button
// *.xml
<Button
android:id=@+id/b1 android:height=wrap_content android:width=match_parent
android:text=Button 1>
// 1 . ( *.java)
import android.widget.*; import android.view.View;
public class Project extends Activity{ public void onCreate(Bundle state){
super.onCreate(state); setContentView(R.layout.main);
final Button b1 = (Button) findViewById(R.id.b1); final Button b2 = (Button) findViewById(R.id.b2); b1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// 䳿 1
}
});
b2.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){
// 䳿 2
}
});
}
}
// 2 . ( *.java)
import android.widget.*; import android.view.View;
public class Project extends Activity{ public void onCreate(Bundle state){
super.onCreate(state); setContentView(R.layout.main);
final Button b1 = (Button) findViewById(R.id.b1); final Button b2 = (Button) findViewById(R.id.b2); b1.setOnClickListener(b1_click); b2.setOnClickListener(b2_click);
}
public OnClickListener b1_click=new OnClickListener(){ public void onClick(View v){
// 䳿 1
}
};
public OnClickListener b2_click=new OnClickListener(){ public void onClick(View v){
// 䳿 2
}
};
}
// 3 . ( *.java)
//
import android.widget.*; import android.view.View;
import android.view.View.OnClickListener;
public class Project extends Activity implements OnClickListeber{
public void onCreate(Bundle state){ super.onCreate(state); setContentView(R.layout.main);
final Button b1 = (Button) findViewById(R.id.b1); final Button b2 = (Button) findViewById(R.id.b2); b1.setOnClickListener(this); b2.setOnClickListener(this);
}
public void OnClick (View v){ swith(v.getId()){
case R.id.b1:
// 䳿 1 break;
case R.id.b2:
// 䳿 2 break;
}
}
}
CheckBox
CheckBox ()
( 2.8). CompoundButton.OnCheckedChangeListener.
2.8 ³ CheckBox
// *.xml
<CheckBox
android:id=@+id/cb1 android:height=wrap_content
android:android:text=CheckBox 1>
// *.java
import android.widget.CheckBox; importandroid.widget.CompoundButton;
public class Project extends Activity implements CompoundButton.OnCheckedChangeListener{
public void onCreate(Bundle state){ super.onCreate(state); setContentView(R.layout.main);
final CheckBox cb1 = (CheckBox) findViewById (R.id.cb1);
final CheckBox cb2 = (CheckBox) findViewById (R.id.cb2);
cb1.setOnCheckedChangeListener(this); cb2.setOnCheckedChangeListener(this);
}
public void OnCheckedChange (CompoundButton v, Boolean isChecked){
swith(v.getId()){ case R.id.cb1:
if(isChecked){ // 䳿 CheckBox 1} break;
case R.id.cb2:
if(isChecked){ // 䳿 CheckBox 2} break;
} }}
RadioButton
³ RadioButton () RadioGroup. RadioGroup ViewGroup , , , LinearLayout. . toggle(), . , , , isChecked() setChecked().
// *.xml
<RadioGroup Android:orientation=vertical
Android:layout_Android:layout_height=match_parent>
<RadionButton Android:id=@+id/r1
Android:layout_Android:layout_height=wrap_content Android:text=Mode 1>
</RadioGroup>
//*.java
import android.widget.*; import android.view.View;
import android.view.View.OnClickListener;
public class Project extends Activity implements OnClickListeber{
public void onCreate(Bundle state){ super.onCreate(state); setContentView(R.layout.main);
final RadioButton r1 = (RadioButton) findViewById(R.id.r1);
final RadioButton r2 = (RadioButton) findViewById(R.id.r2);
final RadioButton r3 = (RadioButton) findViewById(R.id.r3);
r1.setOnClickListener(this); r2.setOnClickListener(this); r3.setOnClickListener(this);
}
public void OnClick (View v){ swith(v.getId()){
case R.id.r1:
// 䳿 1 break;
} }}
Android- , ( RadioGroup) . EditText. LinearLayout. 2.9.
2.9
, ;
;
;
;
.
1. Android.
2. . .
3. Activity View.
4. .
5. ?
6. TextView.
7. .
8. ImageViw.
9. Android-.
10. Button.
11. CheckBox.
12. RadioButton.
3 Բ .
: - .
3.1 ³-
, 璺 . 璺 ArrayAdapter<T>, :
String[] db = {s1, s2, s3}; ArrayAdapter<String> adapter = new ArrayAdapter
<String>(this, android.R.layout.simple_list_item_1, db);
ArrayAdapter :
ᒺ Context , Activity;
, , android.R.layout (simple_list_item, simple_spinner_ dropdown_item, simple_gallery_item, simple_list_item_ checked);
List<T>ᒺ .
3.1.1 ³AutoCompleteTextView
³ AutoCompleteTextView . ³ EditText, . ³ , , android:completionThreshold.
砠 젠 setAdapter().
// *.xml
<LinearLayout xmlns:android = "http://schemas.android. com/apk/res/android"
android:layout_android:layout_height="wrap_content" android:orientation="vertical" >
<TextView
android:id="@+id/t1" android:layout_android:layout_height="wrap_content" android:text="Choice:" />
<Button
android:id="@+id/b1" android:layout_android:layout_height="wrap_content" android:text="OK" />
<AutoCompleteTextView android:id="@+id/mylist" android:layout_android:layout_height="wrap_content" android:completionThreshold="3" />
</LinearLayout>
// *.java
import android.os.Bundle; import android.app.Activity; import android.view.View;
import android.view.View.OnClickListener; import android.widget.*;
import android.text.*;
public class MainActivity extends Activity implements TextWatcher, OnClickListener{
private TextView t1;
private AutoCompleteTextView mylist;
private String[] db = {"Jacob Anderson", "Joseph Godwin", "Joshua Harrison", "Emma Lawson"};
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
t1=(TextView)findViewById(R.id.t1);
mylist = (AutoCompleteTextView) findViewById(R.id.mylist);
mylist.addTextChangedListener(this); mylist.setAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_dropdown_item_1line, db));
Button b1 = (Button) findViewById(R.id.b1); b1.setOnClickListener(this);
}
public void afterTextChanged(Editable s){
// TODO Auto-generated methodstub
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0.getId()==R.id.b1) t1.setText(mylist.getText());
}
}
3.1 AutoCompleteTextView
3.1.2 ³ MultiAutoCompleteTextView
³ MultiAutoCompleteTextView , 頠
AutoCompleteTextView. ³ , . setTokenizer():
MultiAutoCompleteTextView mylist = (MultiAutoCompleteTextView) findViewById(R.id.mylist); mylist.addTextChangedListener(this);
mylist.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_dropdown_item_1line, db));
//
Mylist.setTokenizer(new MultiAutoCompleteTextView. CommaTokenizer());
3.1.3 ³ListView
³ . ListView ᒺ ListAdapter. AutoCompleteTextView ListActivity Activity. 璺 ᒺ ListActivity , ListAdapter. Android : SimpleAdapter ( 璺 ), SimpleCursorAdapter ( ).
// *.xml
<LinearLayout xmlns:android = "http://schemas.android. com/apk/res/android"
android:layout_width = "match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<TextView
android:id="@+id/t1" android:layout_android:layout_height="wrap_content"/>
<ListView
android:id="@android:id/list" android:layout_android:layout_height="wrap_content" />
</LinearLayout>
// *.java
import android.os.Bundle; import android.app.Activity;
import android.view.Menu;
import android.app.ListActivity; import android.widget.*;
import android.view.View;
public class MainActivity extends ListActivity {
private String[] db = {"Jacob Anderson", "Joseph Godwin", "Joshua Harrison", "EmmaLawson"};
private TextView t1;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); t1=(TextView)findViewById(R.id.t1);
setListAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1,db));
}
public void onListItemClick(ListView p, View v, int position, longid){
t1.setText(db[position]);
}
}
3.2 ListView
3.1.4
- , . . 璺 SimpleAdapter SimpleCursorAdapter. SimpleAdapter :
SimpleAdapter(this, list, R.layout.main, new String[] {items.item1, items.item2, }, newint[]
{R.id.item1, R.id.item2, })
. -, ϲ . main.xml TextView name phone ( ). , , Items.java.
// Items.java
package com.samples.ui.lab; import java.util.HashMap;
public class Items extends HashMap<String, String>{ private static final long serialVersionUID=1L; public static final String NAME = name; public static final String PHONE = phone; public Items(String name, String ph