You are on page 1of 5

B3: Bn khung Package Explore bn tri i ti th mc res, bn s thy c 3 th

mc con:

- drawable: th mc cha cc hnh nh lm icon hoc ti nguyn cho giao
din...
- layout: cha cc file xml thit k giao din.
- values: cha cc gi tr s dng trong ng dng c bn nh ngha, nh cc
dng k t (string), cc mu (color), cc themes...

B4:Vo th mc layout, chn file main.xml v g on code sau vo thay cho ton
b ni dung c sn (Eclipse h tr ko th cho xml nhng theo mnh khng nn s
dng):
M:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/edit_hint"
/>
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/text_color"
android:textSize="28px"
android:typeface="monospace"
/>
</LinearLayout>
Trong on XML ny chng ta khai bo mt Linear Layout vi 2 thnh phn con
ca n l 1 Edit Text (dng g xu k t) vi 1 Text View (hin th xu k t).
Linear Layout c khai bo vi t kha orientation nhm ch ra chiu sp xp ca
2 thnh phn con l chiu dc. Cn vi layout_width, layout_height cc bn c th
cho gi tr bng "fill_parent" hoc "wrap_content" thng bo thnh phn ny s
c chiu rng (di) ph y thnh phn cha hoc ch va bao ni dung.
Trong Edit Text v Text View cc bn c th thy c t kha id, t kha ny cho
php khai bo id ca cc thnh phn ly v trong code (s cp sau).
Ngoi ra t kha hint trong Edit Text cho php hin ra phn ni dung m khi Edit
Text vn cha c k t no. "@string/edit_hint" thng bo ly trong file
strings.xml xu c tn l edit_hint.
Cn textColor ca Text View th thng bo on k t s c hin th vi mu
ly trong file colors.xml, textSize ch ra c ch bng 28 pixel v typeface ch ra
kiu ch l monospace

B5:Vn trong th mc res, vo values v chn file strings.xml. B sung thm dng
nh ngha cho edit_hint nh sau:
M:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Example!</string>
<string name="app_name">Example 1</string>
<string name="edit_hint">Enter the work
here</string>
</resources>
B6:Trong th mc values, to file colors.xml (chut phi vo th mc, chn New -
> Android XML File, v lu ch s, khng phi l color.xml). G ni dung cho
file nh sau:
M:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="text_color">#ff3300</color>
</resources>
OK, vy l bn to mt mu mi cho dng ch s c hin th trong Text
View (ff3300 l m hexa ca mu ). Thc cht bn hon ton c th g thng
M:
android:textColor="#ff3300"
trong file main.xml m khng cn to mi file colors.xml, nhng mc ch ca
XML trong Android chnh l h tr nng cp chnh sa d dng. Nu sau ny
bn mun sa mu ca dng text th ch cn vo colors.xml thay i thay v m
mm trong main.xml (c th rt di nu giao din phc tp).

Cc thnh phn trn mi ch l cc phn c bn ca XML. Ngoi ra cc bn c th
khai bo thm v Animation, Style v Theme (phc tp hn nhiu nn mnh khng
gii thiu trong phn c bn ny).

B7: Vy l chng ta hon thin phn giao din vi XML, gi n vit code
x l cc s kin cho cc thnh phn:
=> vo th mc src (source code ca project) => at.exam => Example.java, g ni
dung code sau vo:
M:
package at.exam;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;

public class Example extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Thit lp giao din ly t file main.xml
setContentView(R.layout.main);

//Ly v cc thnh phn trong main.xml thng
qua id
final EditText edit = (EditText)
findViewById(R.id.edit_text);
final TextView text = (TextView)
findViewById(R.id.text_view);

//Thit lp x l cho s kin nhn nt gia ca
in thoi
edit.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode,
KeyEvent event) {
if (event.getAction() ==
KeyEvent.ACTION_DOWN
&& keyCode ==
KeyEvent.KEYCODE_DPAD_CENTER) {

text.setText(edit.getText().toString());
edit.setText("");
return true;
}
else {
return false;
}
}

});
}
}

You might also like