Bỏ bẵng đi 1 thời gian do bận việc này việc khác, cũng bỏ quên không vào chăm sóc viết bài cho cái blog này. Tình hình nay quay lại vẫn thấy nhiều bạn ghé vào thường xuyên để ngâm cứu, cũng thấy vui vui.
Dự tính thời gian tới lại cày kéo tiếp không thì kiến thức nó mai một đi hết thì phí. Mình thấy số lượng các bạn quan tâm tới Cocos2d-x cũng ngày càng tăng lên đáng kể, mới làm quen lập trình game cũng có, chuyển đổi từ engine - ngôn ngữ khác sang cũng có, nhưng túm lại là có phát triển. Tương lai của Engine này không biết tới đâu vì còn tùy thuộc vào Team phát triển của nó ( đang phát triển 3.3 - 3.4 rồi), nhưng chắc chắn cũng đang có nhiều người quan tâm. Càng đông càng vui nhỉ. Và sẽ học hỏi được nhiều điều hơn.
Không biết từ Blog này có bạn nào đã tạo được dự án nào chưa? Mềnh cũng ủ làm vài cái nho nhỏ cho vui, mà giờ trượt tiến độ quá, hix. Chắc phải cày lại thôi
Đôi lời cùng những ai đang ghé qua đây, hi vọng sẽ trở lại sớm sớm!
Với lập trình di động không có chỗ cho lập trình viên trung bình. ... Android cung cấp rất đầy đủ API, code mẫu, pattern để lập trình viên học và ...
Thứ Ba, 18 tháng 11, 2014
Thứ Ba, 11 tháng 11, 2014
Bài 10: Thêm một ví dụ về TextView, EditText, CheckBox, Button và ImageButton trong Android
Để ôn tập kỹ hơn về TextView, EditText, CheckBox, Button và ImageButton, chúng ta sẽ làm thêm một ví dụ tổng hợp và nâng cao hơn. Để hiểu tốt bài này, các bạn cần có kiến thức về hướng đối tượng trong java, và việc triển khai, viết các class của nó. Chúng ta sẽ cùng nghiên cứu về bài tập này, và mình sẽ cố gắng giải thích một cách dễ hiểu nhất.
Đề bài: Viết một chương trình cho phép nhập tên của học sinh, giới tính, học lực. Sau đó thống kê tổng số học sinh, và thống kê số học sinh theo giới tính, học lực.
-Mô tả yêu cầu:
Theo yêu cầu đề bài, các bạn sẽ sử dụng một ArrayList để lưu trữ khi nhập dữ liệu của học sinh. Mỗi đối tượng học sinh có các thuộc tính: Tên học sinh, Giới tính và Xếp Loại. Vì vậy các bạn cần phải tạo ra một class để lưu thông tin của học sinh và một class để lưu thông tin của danh sách học sinh.
Bạn tạo thêm 2 class Java trong package chứa MainActivity.Java và đặt tên như sau:
Và sau đây là toàn bộ code của chương trình:
+ activity_main.xml:
private boolean gioiTinh;
private int xepLoai;
Sau đó chuột phải vào nền soạn thảo của eclipse, chọn Source, chọn Generate Getters and Setters... Sau đó đánh dấu vào các ô vuông và OK. Trình soạn thảo sẽ tự sinh ra code của các hàm này.
Qua bài tập này, mình hy vọng các bạn đã nắm vững được việc sử dụng các control cơ bản: TextView, EditText, Checkbox... Mong rằng các bạn sẽ mở rộng và tự mầy mò thêm các bài tập để nắm vững kiến thức, tránh để học trước quên sau nhé :). Nếu các bạn có gì thắc mắc, hãy bình luận ngay tại đây. Hoặc các bạn có thể sử dụng anh Gu Gồ để thỏa mãn https://www.google.com.vn/
Đề bài: Viết một chương trình cho phép nhập tên của học sinh, giới tính, học lực. Sau đó thống kê tổng số học sinh, và thống kê số học sinh theo giới tính, học lực.
![]() |
| Giao diện chương trình |
-Mô tả yêu cầu:
- Khi bấm nút "Thêm", chương trình sẽ lưu lại kết quả vào trong một Arraylist.
- Khi bấm nút "Thống kê" chương trình sẽ hiển thị ra kết quả...
- Khi bấm nút "Thoát" có biểu tượng như trên, chương trình sẽ hiện một hộp hội thoại AlertDialog hỏi có thoát hay không, và thực thi.
Theo yêu cầu đề bài, các bạn sẽ sử dụng một ArrayList để lưu trữ khi nhập dữ liệu của học sinh. Mỗi đối tượng học sinh có các thuộc tính: Tên học sinh, Giới tính và Xếp Loại. Vì vậy các bạn cần phải tạo ra một class để lưu thông tin của học sinh và một class để lưu thông tin của danh sách học sinh.
Bạn tạo thêm 2 class Java trong package chứa MainActivity.Java và đặt tên như sau:
Và sau đây là toàn bộ code của chương trình:
+ activity_main.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"+MainActivity.java
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="Thông Tin Học Sinh"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#008000"
android:background="#FFFF00"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:layout_weight="1">
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp">
<TextView
android:text="Tên Học Sinh"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edtTenHS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
/>
</TableRow>
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp">
<TextView
android:text="Giới tính"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup android:id="@+id/rgGioiTinh"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rgNam"
android:text="Nam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"/>
<RadioButton
android:id="@+id/rgNu"
android:text="Nữ"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</TableRow>
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp">
<TextView
android:text="Xếp Loại:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup android:id="@+id/rgXepLoai"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rgTB"
android:text="TB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"/>
<RadioButton
android:id="@+id/rgKha"
android:text="Khá"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rgGioi"
android:text="Giỏi"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</TableRow>
</TableLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_weight="1">
<Button android:id="@+id/btnThem"
android:text="Thêm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button android:id="@+id/btnThongKe"
android:text="Thống kê"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:text="Thống kê"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#008000"
android:background="#FFFF00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"/>
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:layout_weight="1">
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:text="Tổng học sinh: "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edtTongHS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</TableRow>
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:text="Học sinh Nam: "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edtHSNam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</TableRow>
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:text="Học sinh Nữ: "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edtHSNu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</TableRow>
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:text="Số học sinh giỏi: "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edtHSGioi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</TableRow>
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:text="Số học sinh khá: "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edtHSKha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</TableRow>
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:text="Số học sinh TB: "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edtHSTB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</TableRow>
</TableLayout>
<ImageButton
android:id="@+id/imgbtnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/exit"/>
</LinearLayout>
</ScrollView>
package com.example.dshocsinh;+ HocSinh.java
import com.example.tthoadon.R;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
private ImageButton imgbtnExit;
private Button btnThem, btnThongKe;
private EditText edtTenHS, edtTongHS,edtHSNam, edtHSNu, edtHSGioi, edtHSKha, edtHSTB;
private RadioGroup rgGioitinh, rgXepLoai;
private DanhSachHocSinh dshs = new DanhSachHocSinh();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getControl(); //Hàm lấy control của các View trong activty_main.xml
doYourWork(); //Thực thi công việc theo yêu cầu
}
private void getControl() {
// TODO Auto-generated method stub
imgbtnExit = (ImageButton)findViewById(R.id.imgbtnExit);
btnThem = (Button)findViewById(R.id.btnThem);
btnThongKe = (Button)findViewById(R.id.btnThongKe);
edtTongHS = (EditText)findViewById(R.id.edtTongHS);
edtHSNam = (EditText)findViewById(R.id.edtHSNam);
edtHSNu = (EditText)findViewById(R.id.edtHSNu);
edtHSGioi = (EditText)findViewById(R.id.edtHSGioi);
edtHSKha = (EditText)findViewById(R.id.edtHSKha);
edtHSTB = (EditText)findViewById(R.id.edtHSTB);
edtTenHS = (EditText)findViewById(R.id.edtTenHS);
rgGioitinh = (RadioGroup)findViewById(R.id.rgGioiTinh);
rgXepLoai = (RadioGroup)findViewById(R.id.rgXepLoai);
}
private void doYourWork() {
// TODO Auto-generated method stub
btnThem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Tạo mới 1 đối tượng hs và gán thuộc tính
HocSinh hs = new HocSinh();
if(rgGioitinh.getCheckedRadioButtonId()==R.id.rgNam){
hs.setGioiTinh(true);
}else{
hs.setGioiTinh(false);
}
if(rgXepLoai.getCheckedRadioButtonId()==R.id.rgTB){
hs.setXepLoai(1);
}else if(rgXepLoai.getCheckedRadioButtonId()==R.id.rgKha){
hs.setXepLoai(2);
}else{
hs.setXepLoai(3);
}
//Thêm đối tượng hs vào trong dshs
dshs.addHocSinh(hs);
edtTenHS.setText("");
}
});
btnThongKe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
edtTongHS.setText(""+dshs.tongHocSinh());
edtHSGioi.setText(""+dshs.hsGioi());
edtHSKha.setText(""+dshs.hsKha());
edtHSTB.setText(""+dshs.hsTB());
edtHSNam.setText(""+dshs.tongHSNam());
edtHSNu.setText(""+dshs.tongHSNu());
}
});
imgbtnExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Thoát chứ nhỉ");
builder.setMessage("Có là thoát này ...");
builder.setNegativeButton("Không", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setPositiveButton("Có", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.create().show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
}
package com.example.dshocsinh;+ DanhSachHocSinh.java
public class HocSinh {
private String tenHocSinh;
private boolean gioiTinh;
private int xepLoai;
public String getTenHocSinh() {
return tenHocSinh;
}
public void setTenHocSinh(String tenHocSinh) {
this.tenHocSinh = tenHocSinh;
}
public boolean isGioiTinh() {
return gioiTinh;
}
public void setGioiTinh(boolean gioiTinh) {
this.gioiTinh = gioiTinh;
}
public int getXepLoai() {
return xepLoai;
}
public void setXepLoai(int xepLoai) {
this.xepLoai = xepLoai;
}
public int checkXepLoai(){
return this.getXepLoai();
}
}
package com.example.dshocsinh;*** Chú ý:
import java.util.ArrayList;
public class DanhSachHocSinh {
private ArrayListlistHS = new ArrayList ();
public void addHocSinh(HocSinh hs){
listHS.add(hs);
}
public int tongHocSinh(){
return listHS.size();
}
public int tongHSNam(){
int tong = 0;
for (HocSinh hs : listHS) {
if(hs.isGioiTinh()==true){
tong++;
}
}
return tong;
}
public int tongHSNu(){
int tong = 0;
for (HocSinh hs : listHS) {
if(hs.isGioiTinh()==false){
tong++;
}
}
return tong;
}
public int hsTB(){
int tong = 0;
for(HocSinh hs: listHS){
if(hs.getXepLoai()==1){
tong++;
}
}
return tong;
}
public int hsKha(){
int tong = 0;
for(HocSinh hs: listHS){
if(hs.getXepLoai()==2){
tong++;
}
}
return tong;
}
public int hsGioi(){
int tong = 0;
for(HocSinh hs: listHS){
if(hs.getXepLoai()==3){
tong++;
}
}
return tong;
}
}
- Các bạn lưu ý cách xây dựng 2 class HocSinh.java và DanhSachHocSinh.java. Điều này rất quan trọng cho sau này khi các bạn muốn xây dựng ứng dụng Android phức tạp hơn.
- Một mẹo nhỏ để code nhanh khi xây dựng một class tương tự HocSinh.java. Các bạn chỉ cần khai báo các thuộc tính sau:
private boolean gioiTinh;
private int xepLoai;
Sau đó chuột phải vào nền soạn thảo của eclipse, chọn Source, chọn Generate Getters and Setters... Sau đó đánh dấu vào các ô vuông và OK. Trình soạn thảo sẽ tự sinh ra code của các hàm này.
Qua bài tập này, mình hy vọng các bạn đã nắm vững được việc sử dụng các control cơ bản: TextView, EditText, Checkbox... Mong rằng các bạn sẽ mở rộng và tự mầy mò thêm các bài tập để nắm vững kiến thức, tránh để học trước quên sau nhé :). Nếu các bạn có gì thắc mắc, hãy bình luận ngay tại đây. Hoặc các bạn có thể sử dụng anh Gu Gồ để thỏa mãn https://www.google.com.vn/
Đăng ký:
Nhận xét (Atom)
Bài đăng phổ biến
-
Hi mọi người! Vậy là chúng ta đã cùng nhau làm xong 2 Game đơn giản ở những bài trước. Trong bài này mình sẽ hướng dẫn các bạn làm 1 game kh...
-
Một trong những cách kiếm tiền qua mạng phổ biến hiện này mà ai cũng có thể tham gia vào chỉ với chút ít kiến thức về máy tính là bán hàng q...
-
Trong tất cả các kiểu dữ liệu trên máy tính thì video là kiểu dữ liệu phức tạp nhất để mã hóa và giải mã, đồng thời cũng có dung lượng lớn n...
-
Ông Trần Quang Trung - Cục trưởng Cục An toàn Thực phẩm (ATTP - Bộ Y tế) khẳng định: "Cục ATTP sẽ xem xét bởi mì tôm của nước ta còn ph...
-
Có rất nhiều thiết bị mạng khác nhau, mỗi thiết bị có một đặc điểm và vai trò riêng, bài viết này sẽ giới thiệu về một số thiết bị mạng thôn...
-
Chào mọi người! Vậy là chúng ta đã đi được 1 chặng đường kha khá của Cocos2d-x V3 rồi. Cũng chuẩn bị xong Project thứ 2 đấy chứ. Trong phần ...
-
Để tìm hiểu về cách biểu diễn âm thành âm thanh trong máy tính (âm thanh số), trước hết ta phải hiểu mô hình chuyển âm thanh từ dạng tương t...
-
Trong một buổi họp với các doanh nghiệp, Cơ quan Thuế và Hải quan Anh - (HMRC) cho biết họ sẽ không đánh thuế 20% lên các giao dịch sử dụng ...
-
Máy tính nhúng là một thuật ngữ để chỉ một máy tính có khả năng tự trị được nhúng vào trong một môi trường hay một hệ thống mẹ. Đó là các h...
-
Yêu cầu Sử dụng thành thạo Java (Core Java) Có kiến thức tốt về cấu trúc dữ liệu và thuật toán Có khả năng tự học, tự nghiên cứu Quyền lợi M...


