You are on page 1of 5

Tm hiu v thit k cc bng giao din! Bi ny mnh s ni v thit k cc layout vi nhng thuc tnh c bn ca n.

Trong th mc res layout bn s thy file main.xml nhng g bn mun thit k s thao tc trn file ny v s hin th ra mn hnh Eclipse h tr ko th bn thit k nhng khng tt cho ngi cha hiu! Mt s thuc tnh c bn Layout_width, layout_height: chiu rng ca view (fill_parent l to bng kch thc ca layout cha view ny, wrap_content l va ni dung cn hin th ca view) Orientation: vi LinearLayout, vic sp xp cc view l nm k nhau theo hng ngang hoc hng dc, ta khai bo orientation chn sp theo kiu no (horizontal (LinearLayout nm ngang)/vertical(LinearLayout thng ng)) Gravity: thuc tnh ny qui nh cc view nm bn trong layout s t theo v tr no so vi layout(trung tm, tri , phi, trn di) Weight: cc view phan chia t l din tch hin th trn mn hnh (t l tnh theo weight ca tng view trn tng s weight, cc view ko khai bo weight th s xem qua width v height) Mi 1 linearLayout nu c ch Edit Text hoc Text View cc bn c th thy t kha id t kha ny cho php khai bo id ca phn ly v trong code Cn trong file strings.xml trong folder values bn nh ngha nhng gi tr cn thit s dng main.xml. Sau y l 1 v d v LinearLayout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="vertical" >

<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="50px" android:typeface="monospace" /> </LinearLayout> on code trn bao gm 1 Linear Layout ln bao ton mn hnh v t kha Vertical(android:orientation="vertical") on code trn cho php bn nhp 1 on text phn EditText v hin th chng khi n ok phn TextView

Nhn thy on code phn TextView c android:textColor="@color/text_color bn phi to thm 1 file colors.xml trong folder Values v khai bo <color name = "text_color">#ff3300</color> vi #ff3300 l m mu hin th Quay tr li vi hm main.java package at.vidu1; 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 Vidu1Activity 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 file 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() { public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_CENTER){ text.setText(edit.getText().toString()); edit.setText(""); return true; }else{ return false; } } public void onClick(View v){

} }); } } Final EditText edit = (EditText) findViewById(R.id.edit.text) vi nhim v ly v cc thnh phn trong file main.xml thng qua id v cng lm tng t vi TextView