Bài 10: Thêm một ví dụ về TextView, EditText, CheckBox, Button và ImageButton trong Android
Labels:
Alert Dialog,
AlertDialog trong android,
android,
CheckBox,
Checkbox trong android,
EditText,
Getters And Setters,
lap trinh android,
TextView
·
Posted by
Miller
at
5:35 PM
Đề 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" 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>+MainActivity.java
package com.example.dshocsinh;
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.
*/
}
+ HocSinh.java
package com.example.dshocsinh;
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();
}
}
+ DanhSachHocSinh.javapackage com.example.dshocsinh;
import java.util.ArrayList;
public class DanhSachHocSinh {
private ArrayList listHS = 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;
}
}
*** Chú ý:- 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/
Subscribe to:
Post Comments (Atom)



mình làm giống bạn nhưng bị lỗi: Error: incompatible types: Object cannot be converted to Student
ReplyDelete