huong_dan_lap_trinh_voi_android_9
TRANSCRIPT
-
8/7/2019 huong_dan_lap_trinh_voi_android_9
1/5
B5:Time to coding. Ti th mc src/Example.java v thay i ni dung file nhsau:
M:
package at.exam;
import java.util.ArrayList;
import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;
public class Example extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.main);
//To mng cha String ni dung cng vic vgi
final ArrayList arrayWork = newArrayList();
//Adapter dng kt ni mng vi List Viewfinal ArrayAdapter arrayAdapter = new
ArrayAdapter(this,android.R.layout.simple_list_item_1,
arrayWork);
-
8/7/2019 huong_dan_lap_trinh_voi_android_9
2/5
-
8/7/2019 huong_dan_lap_trinh_voi_android_9
3/5
builder.setPositiveButton("Continue", newDialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog, int which) {// TODO Auto-generatedmethod stub
}});builder.show();
}//Ly ni dung cng vic v thi gian
ra t Edit Text v a vo listelse {
String str =workEnter.getText().toString() + " - "
+hourEdit.getText().toString() + ":"
+minuteEdit.getText().toString();
arrayWork.add(0,str);
arrayAdapter.notifyDataSetChanged();workEnter.setText("");hourEdit.setText("");minuteEdit.setText("");
}}
};
//set Listener cho buttonbutton.setOnClickListener(add);
}}
Mnh ch thch y v on code cng kh d hiu. Tuy nhin cn lu 2vn y.- Khi to i tng ArrayAdapter: Cc bn thy i s truyn vo l (this,
android.R.layout.simple_list_item_1, arrayWork) . This l i s ca lp Context
-
8/7/2019 huong_dan_lap_trinh_voi_android_9
4/5
( y chnh l activity Example). Bn s gp Context trong rt nhiu khi to cclp v nn hiu Context c ngha g. Mnh xin a ra gii thch ca anh Gip(thank mr giaplv):
Quote:
Context thucandroid.content (android.content.Context).
L mt Interface (lp giao tip) cha hu ht thng tin v mi trng ng dng caandroid, c ngha l mi thao tc, tng tc vi h iu hnh iu phi qua lpny.
N l mt lp abstract (tru tng) cung cp cho nhng lp khc cc phng thc tng tc vi h thng Android.
N cho php truy cp ti cc ngun ti nguyn (resources) c nh ngha vcc lp khc. V d nh n c th khi to v chy cc activities, cc broadcast v
cc intents,... Chng ta coi nh Contex l mt lp mc ng dng (Applicationlevel- lin quan ti h thng).
Tm li context gip chng ta d dng truy cp v tng tc ti cc ti nguyn cah thng, cc thng tin, cc dch v (services), cc thng s cu hnh, database,wallpaper, danh b, cuc gi, kt ni, ch rung (vibrator),...
***s d hu ht cc lp c lin quan ti UI (layout, button, textview, imageview,listview,...) u pi super ti Context v bn thn n m nhim vic truy cpresource (R.id, R.layout,....). Nu chng ta khng tham chiu ti Context class thng nhin khng th dng ti cc resources m chng ta to ra.Tip theo l android.R.layout.simple_list_item_1, i ny nh ngha cch th hinitem ( y l String) trong List View. Cc bn hy ghi nh android.R.* l cc tinguyn (resource) c snca Android cho php bn truy cp v s dng. Sau nykhi hng dn to custom View cho List View mnh s cp li vn ny.Cui cng arrayWork chnh l mng cn c bind ca adapter.
- AlertDialog l lp cho php a ra 1 hp thoi, thng dng a ra thng tin
hoc cnh bo n gin. Trong code mnh to 1 builder, to tiu (title) cho n,a ra thng bo (message) v cui cng l to 1 positive button (nhng khngnh ngha x l khi nhn nt ny, v vy nu bn nhn nt th dialog s ch ngin thc hin vic ng li).
B6:Tin hnh chy th chng trnh. Run as -> Android Application. Enjoy
-
8/7/2019 huong_dan_lap_trinh_voi_android_9
5/5
yourself