You are on page 1of 17

Bt u vi Hello World

MultiUni Trn V Tt Bnh

To mt project mi
mn hnh eclipse, bm File New Project Android Android Project Next Hoc trn thanh cng c bm vo icon nh trong hnh

To mt project mi
mn hnh k nhp liu vo cc nh sau:
Project name: HelloWorld (tn project hin tr trn eclipse cng s l th mc ch ng dng trong workspace) Application name: Hello World (tn ng dng s hin th trn in thoi) Package name: org.multiuni.android.hellowrold (tn ca package s cha source code, tng t trong java http://en.wikipedia.org/wiki/Java_package#Package_n aming_conventions)

To mt project mi
Create activity: HelloWorldActivity (to mt lp con ca lp Activity, dng hin th mt mn hnh y l mn hnh HelloWorld ca chng ta) Min SDK version: 4 (tc SDK 1.6, y chng ta khai bo l ng dng ny c th p ng c phin bn SDK c nht l phin bn no)

Sau khi hon tt bm Finish, project mi c to.

Cu trc mt project
Th mc src cha source code ng dng. Gm cc package v cc class. Th mc gen cha cc file t ng pht sinh (m thng gp nht l R.class) Th mc res cha cc resource dng trong ng dng (thng qua ID) Th mc assets cha cc resource file m ng dng cn dng (di dng file)

Cu trc mt project
File Manifest l file khai bo thng tin v ng vi h thng (nh ng dng gm nhng mn hnh no, c service no xin cc quyn g, phin bn bao nhiu, dng t SDK phin bn no) Ngoi ra cn c file th vin.

Chy ng dng Hello World


Vi ng dng va to nu c bo li th bn sa li thng s cho ng dng nh sau: bm chut phi vo project Properties Java compiler Compiler compliance settings: 1.6

Chy ng dng Hello World


Bm Ctrl + F11 chy ng dng, s hin ln emulator

To UI trc tip t code


Code ban u ca ng dng:
public class HelloWorldActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }

To UI trc tip t code


Sa li nh bn di ri chy li (nu thy bo li thiu th vin, th bm Ctrl + Shift + O)
public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android"); setContentView(tv); } }

Th x l s kin
code bn trn, i TextView li thnh Button.
Button bt = new Button(this);

Sau , implement phng thc x l s kin khi user click vo nt ny


bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_LONG).show(); } });

Mt s phm tt
import cc th vin cn thiu bm Ctrl + Shift + O xa mt dng code bm Ctrl + D hin th bng suggestion bm Ctrl + Space comment mt dng code Ctrl + /

Th mt cht v xoay mn hnh


Cc bn to 1 project mi tn l Orientaion. Sau , bm phi chut vo th mc res new Folder nhp tn folder l layoutland (nh nhp chnh xc nh)

Th mt cht v xoay mn hnh


Trong th mc layout ban u c file main.xml, cc bn copy file , cho vo th mc layout-land nh, vy l mi th mc u c mt file main.xml mi file main trong 2 th mc , u c on ny:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=@string/hello" />

Th mt cht v xoay mn hnh


Cc bn sa ci ni dung android:text trong on xml li, cho 2 file main c 2 ni dung text khc nhau l c. Chy chng trnh, sau khi mn hnh hin th, cc bn bm Ctrl + F11 (Ctrl bn tri nh) th xem kt qu nh.

Kt thc
V nh, cc bn xem li bi hm nay, c th tham kho bn ting Anh ti link ny: http://developer.android.com/resources/tutorial s/hello-world.html Cc bn c th th cc view khc nhau v x l mt s s kin khc nh s kin long click, s kin touch, s kin focus trn cc i tng view nh Button, TextView, ImageView, ImageButton Cc bn coi trc phn HelloView v Hello Localization

Hi p

You might also like