You are on page 1of 65

13/11/2011

Trung Tm Tin Hoc - H KHTN


H QG TP.HCM
1
Lp trnh trn Android
Phn 3 - Cc di tung giao din
G/v: Trjnh Quc Hng
http://t3hand.googlecode.com/svn/trunk/lessions/
13/11/2011
Trung Tm Tin Hoc - H KHTN H
QG TP.HCM

i dung
ndroid's User nterface
Layout
Widgets
Events on U
U Components
Text, Button
ListView, GridView, DatePicker, MapView, ..
LinearLayout, FrameLayout, .
&
Trong ng dung ndroid, U duoc xy dung t View, ViewGroup
C nhiu thnh phn ca U duoc k tha t lp View
View duoc s dung nhu thnh phn co s ca Widget, ViewGroup l
thnh phn co s ca Layout
H thng cp bc View
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM 3
Layout
Phuong php ph don gin l djnh ngha layout, biu din theo H thng cp bc View,
s dung tp tin XML layout.
Cu trc d hiu, tuong tu HTML
Tn ca thnh phn XML tuong ng vi lp Java trong code.
V du:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, am a Button" />
</LinearLayout>
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
4
Widgets
i tuong View, duoc s dung nhu l giao din tuong
tc vi ngui dng
Mt s Widgets d duoc djnh ngha bi ndroid:
Button, Checkbox, EditText, TextView, DatePicker,
Clock,.
C th djnh ngha cc di tuong View ring bng cch
k tha hoc t hop nhng widget d tn tai.
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
5
Cc s kin trn di tung giao din
tuong tc gia ngui dng vi cc di tuong View,
cn thuc hin:
jnh ngha/ng k event listener v dng k trong di
tuong View
OnClickListener, OnDragListener, OnKeyListener, .
Override cc phuong thc d tn tai cho di tuong
View.
onClick(..), onDrag(..), onKey(..), .
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
6
[nh ngha Layout
Layout trong ndroid l k thut cho php djnh ngha, dn xp cc
di tuong giao din ln mn hnh thit bj, trong lp ctivity.
jnh ngha Layout theo 2 cch:
Khai bo thnh phn U trong XML file
Tao di tuong layout t Javacode
Vj tr Layout
Hnh dang ca View di tuong l hnh ch nht, vj tr duoc biu din bi
cp toa d, left/top v 2 kch thuc width, height.
S dung phuong thc getLeft(), getTop(), getWidth(), getHeight() nhn
cc gi trj ny
Size,Padding, Margins
Size: duoc biu din bi gi trj Width, Height. C 2 cp gi trj
GetMeasuredWidth()/GetMeasuredHeight() v GetWidth()/GetHeight
Padding duoc biu din cho phn bn tri/phi, trn/dui ca View, s
dung phuong thc setPadding(int,int,int,int)
Margins chi h tro trong ViewGroup bi ViewGroup.MarginLayoutParams
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
7
Text ControIs
C cc dang Widget: TextView, EditText, utoCompleteTextView,
MultiutoCompleteTextView
TextView dng d hin thj ni dung Text, khng th thay di
Thuc tnh quan trong: android:autoLink,android:autoText,android:digits, android:phoneNumber
Link tham chiu: http://developer.android.com/reference/android/widget/TextView.html
EditText: l widget k tha t TextView, duoc dng thay di ni dung text, cha tt
c thuc tnh ca TextView.
Linhk tham chiu: http://developer.android.com/reference/android/widget/EditText.html
utoCompleteTextView: l TextView soan tho hin thj d ngh hon thnh text khi
ngui dng dang nhp.
private static final String[] COUNTRES = new String[] {
"Belgium", "France", "taly", "Germany", "Spain"
};
rraydapter<String> adapter = new rraydapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRES);
utoCompleteTextView textView = (utoCompleteTextView)
findViewByd(R.id.countries_list);
textView.setdapter(adapter);
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
8
Text ControIs
MultiutoCompleteTextView l widget k tha t utoCompleteTextView, c th
hin thj li d nghj hon thnh, cho chui k tu con ca text m ngui dang nhp
thay v ton b text
Link tham chiu:
http://developer.android.com/reference/android/widget/MultiutoCompleteTextView.
html
public class Countriesctivity extends ctivity {
protected void onCreate(Bundle savednstanceState) {
super.onCreate(savednstanceState);
setContentView(R.layout.autocomplete_7);
rraydapter<String> adapter = new rraydapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRES);
MultiutoCompleteTextView textView = (MultiutoCompleteTextView)
findViewByd(R.id.edit);
textView.setdapter(adapter);
textView.setTokenizer(new MultiutoCompleteTextView.CommaTokenizer());
}
private static final String[] COUNTRES = new String[] {
"Belgium", "France", "taly", "Germany", "Spain"
};
}
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
9
Button ControIs
C 3 loai : Button co bn,image button,toggle button
Button
<Button android:id="@+id/ccbtn1"
android:text="@string/basicBtnLabel"
android:typeface="serif" android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
10
Button ControIs
Java code:
Button btn = (Button)this.findViewByd(R.id.ccbtn1);
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//do some thing
}
});
Link:
http://developer.android.com/reference/android/widget/Button.html
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
11
2ageButton ControIs
mageButton: l button cung cp image, cch s dung ging nhu
button
<mageButton android:id="@+id/imageBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
mageButton btn = (mageButton)this.findViewByd(R.id.imageBtn);
btn.setmageResource(R.drawable.icon);
Hoc
<mageButton android:id="@+id/imageBtn"
android:src="@drawable/btnmage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Link:
http://developer.android.com/reference/android/widget/mageButton.ht
ml
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
1
Button ControIs
toggleButton: ging nhu checkbox, radio button. Trong button ny lun c 2
trang thi: On, Off.
<ToggleButton android:id="@+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"/>
final ToggleButton togglebutton = (ToggleButton) findViewByd(R.id.togglebutton);
togglebutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
if (togglebutton.isChecked()) {
Toast.makeText(HelloFormStuff.this, "Checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(HelloFormStuff.this, "Not checked",
Toast.LENGTH_SHORT).show();
}
}
});
Link: http://developer.android.com/reference/android/widget/ToggleButton.html
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
13
CheckBox ControIs
CheckBox: Ging ToggleButton miu t 2 trang thi "checked v "unchecked
<CheckBox android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check it out" />
final CheckBox checkbox = (CheckBox) findViewByd(R.id.checkbox);
checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(HelloFormStuff.this, "Not selected",
Toast.LENGTH_SHORT).show();
}
}
})
Link: http://developer.android.com/reference/android/widget/CheckBox.html
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
14
RadioButton ControIs
RadioButton: C 2 trang thi "checked hoc "unchecked. RadioButton
duoc s dung trong nhm RadioGroup, khi c vi RadioButton, mt khi 1
thnh phn duoc "checked th nhng thnh phn khc s "unchecked
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<RadioButton android:id="@+id/radio_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
15
RadioButton ControIs
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
Toast.makeText(HelloFormStuff.this, rb.getText(),
Toast.LENGTH_SHORT).show();
}
};
radioBtn1.setOnClickListener(radio_listener);
Link: http://developer.android.com/reference/android/widget/RadioButton.html
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
16
ListView
L ViewGroup biu din nhng tems trong danh sch theo chiu thng dng.
Cc items duoc gn vo s dung Listdapter
S dung lp Listctivity
@Override
public void onCreate(Bundle savednstanceState) {
super.onCreate(savednstanceState);
setListdapter(new rraydapter<String>(this, R.layout.list_item, COUNTRES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOntemClickListener(new OntemClickListener() {
public void ontemClick(dapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getpplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
Link: http://developer.android.com/reference/android/widget/ListView.html
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
17
GridView
L ViewGroup biu din items theo Grid theo khng gian 2 chiu.
tems duoc gn tu dng vo layout s dung Listdapter
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
18
GridView
public void onCreate(Bundle savednstanceState) {
super.onCreate(savednstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewByd(R.id.gridview);
gridview.setdapter(new magedapter(this));
gridview.setOntemClickListener(new OntemClickListener() {
public void ontemClick(dapterView<?> parent, View v, int position,
long id) {
Toast.makeText(HelloGridView.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
19
DatePicker
L Widget cho vic lua chon ngy thng, nm.
new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
}
};
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM 0
Ti2ePicker
L Widget cho vic lua chon thi gian ca ngy, trong 24h hoc ch d M/Pm
new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
// the callback received when the user "sets" the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
};
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM 1
AnaIogCIock
L Widget hin thj dng h vi kim gi,
pht.
<LinearLayout
xmlns:android="http://schemas.android.com/ap
k/res/android" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<AnaIogCIock android:id="@+id/clock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM

DigitaICIock
L Widget hin thj dng h vi gi, pht, giy
<DigitaICIock android:id="@+id/clock2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
3
MapView
L control ca package com.google.android.maps duoc dng d
hin thj bn d
C th djnh ngha t XML file hoc Java code, ctivity duoc s
dung phi l Mapctivity
XML file:
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="Your Maps API Key"
/>
s dung MapView phi cha apiKey, duoc cung cp bi
Google
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
4
GaIIery
L layout duoc s dung d biu din nhng tem trong danh sch
theo chiu ngang v vj tr ca tem hin thi lun trung tm ca
View
XML file:
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Co ch lm vic ging vi GridView widget
S dung gallerytemBackground nhu l nn cho mi tem ca
Gallery.
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
5
WebView
L View duoc s dung hin thj trang Web, l class co bn
dua vo n c th dua vo trnh duyt Web, hoc hin thj
vi ni dung online trong ctivity
ctivity truy cp nternet, v ti trang web vo WebView, cn
cung cp quyn truy cp nternet trong Manifest file:
<uses-permission
android:name="android.permission.NTERNET" />
XML file:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
6
Cc Loai Layout
LinearLayout
RelativeLayout
TableLayout
FrameLayout
Tab Layout
bsoluteLayout
http://developer.android.com/guide/topics/ui/layout-objects.html
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
7
LinearLayout
L Layout m cc tem con duoc sp xp ct don hoc hng don.
Hung tuyn tnh duoc thit lp bi hm setOrientation() hoc
thuc tnh "orientation trong XML
Vic chinh thng hng ca cc tem con bi hm setGravity()
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
Nu c nhiu LinearLayout duoc lng vo nhau, nn s dung
RelativeLayout thay th
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
8
ReIativeLayout
L layout m vj tr ca tem con duoc xc djnh bi mi lin quan
vi item con khc hoc vi chnh layout
RelativeLayout l cng cu hu dung cho thit k giao din, bi v
n loai trung hop nhng ViewGroup duoc lng vo nhau.
Khng c su l thuc vng trn gia kch thuc ca
RelativeLayout v vj tr ca tem con.
Thuc tnh Height ca RelativeLayout khng th l
WRP_CONTENT
tem con khng th thit lp thuc tnh LGN_PRENT_BOTTOM
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
9
ReIativeLayout
XML file
<RelativeLayout
xmlns:android="http://schemas.android.com
/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:"/>
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawa
ble/editbox_background"
android:layout_below="@id/label"/>
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
30
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="OK" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
TabIeLayout
Sp xp cc tem con theo hng v ct
XML file
<TableLayout
xmlns:android="http://schemas.android.com/ap
k/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_column="1"
android:text="Open..."
android:padding="3dip" />
<TextView
android:text="Ctrl-O"
android:gravity="right"
android:padding="3dip" />
</TableRow>
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
31
<TableRow>
<TextView
android:layout_column="1"
android:text="Save..."
android:padding="3dip" />
<TextView
android:text="Ctrl-S"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
Fra2eLayout
uoc dng d biu din cc tem con
theo cc layer. Cc tem ca layer bn
dui s bj che khut bi cc tem nm
trn
XML file
<FrameLayout
xmlns:android="http://schemas.androi
d.com/apk/res/android"
android:id="@+id/frmLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<mageView
android:id="@+id/onemgView"
android:src="@drawable/one"
android:scaleType="fitCenter"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
3
<mageView
android:id="@+id/twomgView"
android:src="@drawable/two"
android:scaleType="fitCenter"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" />
</FrameLayout>
Tab Layout
tao tab U,cn s dung TabHost v
Tabwidget. TabHost l node gc ca Layout
cha c Tabwidget cho hin thj tab v
FrameLayout d hin thj ni dung.
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
33
Tab Layout
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
34
Lin kt d Iiu vi AdapterView
dapterView l lp con ca ViewGroup trong d nhng View con
duoc xc djnh bi dapter lin kt vi d liu
Gallery,ListView,GridView,Spinner l lp con ca dapterView
daper l di tuong nterface, nhu cu ni gia dapterView vi
d liu trong n.
Lp thuc thi ca dapter: Basedapter, rraydapter,
Cussordapter, Simpledapter
i tuong dapterView c 2 trch nhim chnh:
ua d liu vo layout
Bt su kin selection t ngui dng
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
35
ua d Iiu vo Iayout
S dung rraydapter:
// Get a Spinner and bind it to an rraydapter that
// references a String array.
Spinner s1 = (Spinner) findViewByd(R.id.spinner1);
rraydapter adapter = rraydapter.createFromResource(
this, R.array.colors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spin
ner_dropdown_item);
s1.setdapter(adapter);
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
36
ua d Iiu vo Iayout
S dung SimpleCursordapter:
private static String[] PROJECTON = new String[] {
People._D, People.NME
};
Spinner s2 = (Spinner) findViewByd(R.id.spinner2);
Cursor cur = managedQuery(People.CONTENT_UR, PROJECTON, null, null);
SimpleCursordapter adapter2 = new SimpleCursordapter(this,
android.R.layout.simple_spinner_item, // Use a template
// that displays a
// text view
cur, // Give the cursor to the list adapter
new String[] {People.NME}, // Map the NME column in the
// people database to...
new int[] {android.R.id.text1}); // The "text1" view defined in
// the XML template
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setdapter(adapter2);
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
37
Bt s kin t ngui dng
// Create a message handling object as an anonymous class.
private OntemClickListener mMessageClickedHandler = new
OntemClickListener() {
public void ontemClick(dapterView parent, View v,
int position, long id)
{
// Display a messagebox.
Toast.makeText(mContext,"You've got an
event",Toast.LENGTH_SHORT).show();
}
};
// Now hook into our object and set its ontemClickListener member
// to our class handler object.
mHistoryView = (ListView)findViewByd(R.id.history);
mHistoryView.setOntemClickListener(mMessageClickedHandler);
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
38
Menus
L phn quan trong ca giao din ngui dng, c 3 loai
Menus
Options Menu
Context Menu
SubMenu
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
39
ption Menu
Tao Options Menu, goi phuong thc override t ctivity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Menunflater inflater = getMenunflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
Hoat dng ca ngui dng trn Menu
@Override
public boolean onOptionstemSelected(Menutem item) {
// Handle item selection
switch (item.gettemd()) {
case R.id.new_game:
return true;
case R.id.help:
return true;
default:
return super.onOptionstemSelected(item);
}
} 11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
40
Context Menus
Tao Options Menu, goi phuong thc override t ctivity
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenunfo menunfo) {
super.onCreateContextMenu(menu, v, menunfo);
Menunflater inflater = getMenunflater();
inflater.inflate(R.menu.context_menu, menu);
}
Hoat dng ca ngui dng trn Menu
@Override
public boolean onContexttemSelected(Menutem item) {
dapterContextMenunfo info = (dapterContextMenunfo) item.getMenunfo();
switch (item.gettemd()) {
case R.id.edit:
return true;
case R.id.delete:
return true;
default:
return super.onContexttemSelected(item);
}
}
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
41
$ubMenus
L Menu m ngui dng c th goi khi chon vo tem ca Menu
khc
SubMenu rt hu dung khi ng dung c nhiu chc nng cn
duoc t chc.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/file"
android:icon="@drawable/file"
android:title="@string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="@+id/create_new"
android:title="@string/create_new" />
<item android:id="@+id/open"
android:title="@string/open" />
</menu>
</item>
</menu>
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
4
Tao Menu t Java code
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
//call the base class to include system menus
super.onCreateOptionsMenu(menu);
menu.add( 0 // Group
,1 // item id
,0 //order
,"append"); // title
menu.add(0,2,1,"item2");
menu.add(0,3,2,"clear");
//t is important to return true to see the menu
return true;
}
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
43
Tao Menu Group t Java code
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
//Group 1
int group1 = 1;
menu.add(group1,1,1,"g1.item1");
menu.add(group1,2,2,"g1.item2");
//Group 2
int group2 = 2;
menu.add(group2,3,3,"g2.item1");
menu.add(group2,4,4,"g2.item2");
return true; // it is important to return true
}
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
44
Tao $ubMenu t Java code
//Secondary items are shown just like everything else
int base=Menu.FRST + 100;
SubMenu sm = menu.addSubMenu(base,base+1,Menu.NONE,"submenu");
sm.add(base,base+2,base+2,"sub item1");
sm.add(base,base+3,base+3, "sub item2");
sm.add(base,base+4,base+4, "sub item3");
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
45
$ Thng bo trong Android
Khi cn thng bo cho ngui dng v nhng su kin
xy ra trong ng dung.
Vi trung hop yu cu tr li, hoc chi thng bo cho
ngui dng
Vi loai thng bo trong ndroid
Toast Notification
Status Bar Notification
Dialog Notification
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
46
Toast otification
L thng dip duoc hin thj
trn b mt ca mn hnh.
Tu dng xut hin v n di,
khng c kh nng tuong tc
vi ngui dng.
Toast c th duoc tao t
Background, nn c th xut
hin thm ch ng dung khng
c giao din.
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
47
Toast otification
Tao Toast
Context context = getpplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Khng cn tao di tuong Toast
Toast.makeText(context, text, duration).show();
Vj tr ca Toast
aToast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
48
Toast otification
Tao custom Toast
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#D"
>
<mageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
49
Toast otification
Tao custom Toast
Layoutnflater inflater = getLayoutnflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewByd(R.id.toast_layout_root));
mageView image = (mageView) layout.findViewByd(R.id.image);
image.setmageResource(R.drawable.android);
TextView text = (TextView) layout.findViewByd(R.id.text);
text.setText("Hello! This is a custom toast!");
Toast toast = new Toast(getpplicationContext());
toast.setGravity(Gravity.CENTER_VERTCL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
50
$tatus Bar otifications
uoc s dung cho trung hop cc
Service cn thng bo cho ngui
dng v nhng su kin m cn
ngui dng tuong tc.
Cn thm biu tuong v thng dip
m rng trong ca s notification.
Cu hnh notification d thng bo
ngui dng s dung m thanh, d
rung dng, nh sng trong thit bj.
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
51
Tao $tatus Bar otifications
Cc thnh phn cu thnh
con cho status bar
Message m rng
Pending ntent cn duoc goi khi notification duoc lua chon
Cc thnh phn b sung
Ticker message
m thanh
rung dng
nh sng
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
5
Tao $tatus Bar otifications
int icon = R.drawable.notification_icon; // icon from resources
CharSequence tickerText = "Hello"; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getpplicationContext(); // application Context
CharSequence contentTitle = "My notification"; // expanded message title
CharSequence contentText = "Hello World!"; // expanded message text
ntent notificationntent = new ntent(this, MyClass.class);
Pendingntent contentntent = Pendingntent.getctivity(this, 0,
notificationntent, 0);
// the next two lines initialize the Notification, using the configurations
above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventnfo(context, contentTitle, contentText,
contentntent);
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
53
Tao $tatus Bar otifications
S dung m thanh
Default
notification.defaults |= Notification.DEFULT_SOUND;
S dung file m thanh khc
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
S dung Media trong ndroid
notification.sound =
Uri.withppendedPath(udio.Media.NTERNL_CONTENT_UR, "6");
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
54
Tao $tatus Bar otifications
S dung d rung dng
Default
notification.defaults |= Notification.DEFULT_VBRTE;
jnh ngha
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
S dung nh sng
Default
notification.defaults |= Notification.DEFULT_LGHTS;
jnh ngha
notification.ledRGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLG_SHOW_LGHTS;
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
55
DiaIog
L ca s nh, xut hin pha truc ctivity hin thi.
ctivity nm dui s mt focus, Dialog dng tt c su
tuong tc ca ngui dng.
Lp Dialog l lp co s trong vic tao Dialog.
Lp con ca Dialog:
lertDialog
ProgressDialog
DatePickerDialog
TimePickerDialog
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
56
Tao DiaIog
S dung hm callback onCreateDialog() t ctivity d tao
Dialog
static final int DLOG_PUSED_D = 0;
static final int DLOG_GMEOVER_D = 1;
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case DLOG_PUSED_D:
// do the work to define the pause Dialog
break;
case DLOG_GMEOVER_D:
// do the work to define the game over Dialog
break;
default:
dialog = null;
}
return dialog;
}
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
57
Tao DiaIog
biu din Dialog, goi hm
showDialog(DLOG_PUSED_D);
kt thc Dialog, goi hm dismiss() hoc
dismissDialog(int)
Nu kt thc Dialog m khng s dung lai Dialog ny
na, c th s dung removeDialog(int)
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
58
AIertDiaIog
L lp con ca Dialog, duoc s dung cho nhng dc dim
Tiu d
Message
S dung Button
Danh sch nhng item duoc lua chon
Thm button
lertDialog.Builder builder = new lertDialog.Builder(this);
builder.setMessage("re you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new Dialognterface.OnClickListener() {
public void onClick(Dialognterface dialog, int id) {
Myctivity.this.finish();
}
})
.setNegativeButton("No", new Dialognterface.OnClickListener() {
public void onClick(Dialognterface dialog, int id) {
dialog.cancel();
}
});
lertDialog alert = builder.create();
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
59
AIertDiaIog
Thm List
final CharSequence[] items = {"Red", "Green", "Blue"};
lertDialog.Builder builder = new lertDialog.Builder(this);
builder.setTitle("Pick a color");
buiIder.sette2s(items, new Dialognterface.OnClickListener() {
public void onClick(Dialognterface dialog, int item) {
Toast.makeText(getpplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
}
});
lertDialog alert = builder.create();
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
60
AIertDiaIog
Thm checkbox, radio button
final CharSequence[] items = {"Red", "Green", "Blue"};
lertDialog.Builder builder = new lertDialog.Builder(this);
builder.setTitle("Pick a color");
buiIder.set$ingIeChoicete2s(items, -1, new
Dialognterface.OnClickListener() {
public void onClick(Dialognterface dialog, int item) {
Toast.makeText(getpplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
}
});
lertDialog alert = builder.create();
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
61
ProgressDiaIog
ProgressDialog dialog =
ProgressDialog.show(Myctivity.this, "",
"Loading. Please wait...", true);
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM
6
Progress bar
Cc buc tao progress bar
Khi tao ProgressDialog
Thit lp kiu progress l STYLE_HORZONTL
Goi showDialog(int)
Tng s luong tin trin ca progress bng vic goi
setProgress(int)
11/13/2011
Trung Tm Tin Hoc - H KHTN - H
QG TP.HCM 63
Widget's References
http://developer.android.com/reference/andro
id/widget/package-summary.html
13/11/2011
Trung Tm Tin Hoc - H KHTN H
QG TP.HCM
64
13/11/2011
Trung Tm Tin Hoc - H KHTN H
QG TP.HCM
65
Hi/p

You might also like