[ Android ] Xây dựng ứng dụng: Nhập vào tên bạn, sau đó in ra lời chào bạn [Bắt đầu làm việc với Android]

Trước tiên hãy đảm bảo rằng Eclipse của bạn đã được Plug-In thư viện Android thành công [xem cách plug-in android tại đây].

Một ứng dụng Android cơ bản gồm các thành phần sau:



Trong đó, chúng ta phải thiết lập và code các file sau (bạn có thể copy và patse vào đúng vị trí):

1- Res \ layout (main.xml): 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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>


2- Res \ values:

 + string.xml (thiết lập các chuỗi ký tự):


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Example!</string>
    <string name="app_name">Loi chao: </string>
    <string name="edit_hint">Nhap vao ten cua ban!</string>
 </resources>


 + color.xml (thiết lập màu chữ):


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="text_color">#ff3300</color>
</resources>


3- src \ [Package của bạn] \ [class chính của bạn] (chứa code xử lý) :


package txt.B.Nhap;
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 NhapSOActivity extends Activity {
/** Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Thiet lap giao dien lay tu file main.xml
setContentView(R.layout.main);
//Lay ve cac thanh phan trong main.xml thong qua id
final EditText edit = (EditText)
findViewById(R.id.edit_text);
final TextView text = (TextView)
findViewById(R.id.text_view);
//Xu ly cho su kien an nut giua tren dien thoai
edit.setOnKeyListener(
new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
String chao="Chao ban "+edit.getText().toString();
text.setText(chao);
edit.setText("");
return true;
}
else {
return false;
}
}
});
}
}

*** Chú ý: Bạn cần để ý tên Project mà bạn tạo để thay đổi code cho chuẩn.

>> Kết quả khi thực thi ứng dụng:





[Các bài viết liên quan]

*******

Một số tài liệu và khoá học bổ ích dành cho bạn: 

# Giáo trình: Lập Trình Android [Click để xem]

# Khoá học online:  Lập trình Android toàn tập [Click để xem]

-----------------------------------------


Tìm kiếm nội dung khác: