10
Ver 1.0 2016, FIT - HCMUP Lab 04: Các loi Control Ths. Lương Trần Hy Hiến, KHOA CNTT TRƯỜNG ĐH SƯ PHẠM TP. HCM 1 1 Radio Button Radio Group, CheckBox, Spinner Thiết kế màn hình thu thp thông tin người dùng và hin thli kết qugi thông tin dùng AlertDialog. Cu trúc layout màn hình ng dng:

1 Radio Button Radio Group, CheckBox, SpinnerVer 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP.HCM

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Radio Button Radio Group, CheckBox, SpinnerVer 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP.HCM

Ver 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 1

1 Radio Button – Radio Group, CheckBox, Spinner

Thiết kế màn hình thu thập thông tin người dùng và hiển thị lại kết quả gửi thông tin

dùng AlertDialog.

Cấu trúc layout màn hình ứng dụng:

Page 2: 1 Radio Button Radio Group, CheckBox, SpinnerVer 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP.HCM

Ver 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 2

Giao diện chương trình khi chạy:

Yêu cầu khi bấm button gửi thông tin phải kiểm tra họ tên ít nhất 3 kí tự, phải chọn một

màu yêu thích nhất.

txtHoTen = (EditText)findViewById(R.id.editTextHoTen);

chkMU = (CheckBox)findViewById(R.id.chkMU);

chkBM = (CheckBox)findViewById(R.id.chkBM);

chkBar = (CheckBox)findViewById(R.id.chkBar);

rgMau = (RadioGroup)findViewById(R.id.rgMauYT);

sQQ = (Spinner)findViewById(R.id.spinnerQQ);

//đổ dữ liệu cho spinner Quê quán

ArrayList<String> dsQQ = new ArrayList<String>();

dsQQ.add("Khánh Hòa");

dsQQ.add("Hồ Chí Minh");

dsQQ.add("Long An");

dsQQ.add("Quảng Ngãi");

dsQQ.add("Quảng Bình");

ArrayAdapter adap = new ArrayAdapter(

this, android.R.layout.simple_spinner_item, dsQQ

);

//adap.setDropDownViewResource(android.R.layout.simple_spinner_dropdow

Page 3: 1 Radio Button Radio Group, CheckBox, SpinnerVer 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP.HCM

Ver 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 3

n_item);

sQQ.setAdapter(adap);

btnXuat = (Button)findViewById(R.id.btnXuatTT);

btnXuat.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

//Kiểm tra tên hợp lệ (> 3 kí tự)

String ten = (txtHoTen.getText() + "").trim();

if(ten.length() < 3)

{

txtHoTen.requestFocus();

txtHoTen.selectAll();

Toast.makeText(MHThongTin.this, "Họ tên phải >= 3 ký tự",

Toast.LENGTH_LONG).show();

return;

}

//Xứ lý chọn màu

int id = rgMau.getCheckedRadioButtonId();

if(id == -1)

{

Toast.makeText(MHThongTin.this, "Phải chọn màu",

Toast.LENGTH_LONG).show();

return;

}

//Lấy màu chủ đạo

RadioButton rad =(RadioButton) findViewById(id);

String mau_chon = rad.getText() + "";

//Kiểm tra CLB yêu thích

String clb_thich = "";

if(chkMU.isChecked())

clb_thich += "\t" + chkMU.getText() + "\n";

if(chkBM.isChecked())

clb_thich += "\t" + chkBM.getText() + "\n";

if(chkBar.isChecked())

clb_thich += "\t" + chkBar.getText() + "\n";

//Hiển thị hộp thoại thông báo

AlertDialog.Builder builder=new

AlertDialog.Builder(MHThongTin.this);

builder.setTitle("Thông tin cá nhân");

//tạo nội dung thông báo

String msg = ten;

msg += "\n--------------\n";

msg += "Quê quán: " + sQQ.getSelectedItem();

msg += "\n--------------\n";

Page 4: 1 Radio Button Radio Group, CheckBox, SpinnerVer 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP.HCM

Ver 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 4

msg += "CLB yêu thích: \n";

msg += clb_thich;

msg += "--------------\n";

msg += "Màu sắc chủ đạo: ";

msg += mau_chon;

builder.setMessage(msg);

builder.setPositiveButton("Đóng", new

DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.cancel();

}

});

builder.create().show();

}

});

2 Custom Layout ListView

Tạo mới Class SinhVien và thực hiện Thêm sinh viên vào danh sách như thiết kế.

Page 5: 1 Radio Button Radio Group, CheckBox, SpinnerVer 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP.HCM

Ver 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 5

2.1 Xây dựng lớp SinhVien

public class SinhVien {

public boolean isGioiTinh() {

return GioiTinh;

}

@Override

public String toString() {

return getMaSV() + " : " + getHoTen();

}

public void setGioiTinh(boolean gioiTinh) {

GioiTinh = gioiTinh;

}

public String getHoTen() {

return HoTen;

}

public void setHoTen(String hoTen) {

HoTen = hoTen;

}

public String getMaSV() {

return MaSV;

}

public void setMaSV(String maSV) {

MaSV = maSV;

}

private String MaSV;

private String HoTen;

private boolean GioiTinh;

}

2.2 Thiết kế Layout riêng cho một item

Page 6: 1 Radio Button Radio Group, CheckBox, SpinnerVer 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP.HCM

Ver 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 6

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent">

<ImageView

android:id="@+id/imgitem"

android:layout_width="30dip"

android:layout_height="30dip"

android:paddingLeft="2dp"

android:paddingRight="2dp"

android:paddingTop="2dp"

android:layout_marginTop="4dp"

android:contentDescription="here"

android:src="@drawable/icon_male" />

<TextView

android:id="@+id/txtitem"

android:layout_height="wrap_content"

android:layout_width="0dip"

android:layout_weight="2"

android:layout_marginTop="4dp"

android:paddingLeft="2dp"

android:paddingRight="2dp"

android:paddingTop="2dp"

android:textSize="20sp" />

<CheckBox

android:id="@+id/chkitem"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</LinearLayout>

2.3 Tạo lớp CustomAdapter kế thừa từ ArrayAdapter

Xâu dựng lớp CustomAdapter kế thừa để quản lý dữ liệu.

public class MyArrayAdapter extends ArrayAdapter<SinhVien> {

Activity context = null;

ArrayList<SinhVien> myArray = null;

int layoutId;

/**

* Constructor này dùng để khởi tạo các giá trị

* từ MainActivity truyền vào

* @param context : là Activity từ Main

Page 7: 1 Radio Button Radio Group, CheckBox, SpinnerVer 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP.HCM

Ver 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 7

* @param layoutId: Là layout custom do ta tạo

(my_item_layout.xml)

* @param arr : Danh sách sinh viên truyền từ MainActivity

*/

public MyArrayAdapter(Activity context, int layoutId,

ArrayList<SinhVien>arr) {

super(context, layoutId, arr);

this.context = context;

this.layoutId = layoutId;

this.myArray = arr;

}

/**

* hàm dùng để custom layout, ta phải override lại hàm này

* từ MainActivity truyền vào

* @param position : là vị trí của phần tử trong danh sách

nhân viên

* @param convertView: convertView, dùng nó để xử lý Item

* @param parent : Danh sách sinh viên truyền từ Main

* @return View: trả về chính convertView

*/

public View getView(int position, View convertView,

ViewGroup parent) {

LayoutInflater inflater = context.getLayoutInflater();

convertView = inflater.inflate(layoutId, null);

//chỉ là test thôi, bạn có thể bỏ If đi

if(myArray.size() > 0 && position >= 0)

{

//dòng lệnh lấy TextView ra để hiển thị Mã và tên

lên

final TextView txtdisplay = (TextView)

convertView.findViewById(R.id.txtitem);

//lấy sinh viên thứ position

final SinhVien emp = myArray.get(position);

//đưa thông tin lên TextView

//emp.toString() sẽ trả về Id và Name

txtdisplay.setText(emp.toString());

//lấy ImageView ra để thiết lập hình ảnh cho đúng

final ImageView imgitem =

(ImageView)convertView.findViewById(R.id.imgitem);

//nếu là Nữ thì lấy hình con gái

if(emp.isGioiTinh())

imgitem.setImageResource(R.drawable.icon_female);

else//nếu là Nam thì lấy hình con trai

imgitem.setImageResource(R.drawable.icon_male );

Page 8: 1 Radio Button Radio Group, CheckBox, SpinnerVer 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP.HCM

Ver 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 8

}

//Vì View là Object là dạng tham chiếu đối tượng, nên

//mọi sự thay đổi của các object bên trong convertView

//thì nó cũng biết sự thay đổi đó

return convertView;//trả về View này, tức là trả luôn

//về các thông số mới mà ta vừa thay đổi

}

}

2.4 Code xử lý trong OnCreate()

lvSinhVien = (ListView) findViewById(R.id.lvnhanvien);

arrSinhVien = new ArrayList<SinhVien>();

//Khởi tạo đối tượng adapter và gán Data source

adapter=new MyArrayAdapter(

this,

R.layout.my_item_layout,// lấy custom layout

arrSinhVien/*thiết lập data source*/);

lvSinhVien.setAdapter(adapter);//gán Adapter vào Lisview

btnNhap.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String ma = editMa.getText() + "";

String ten = editTen.getText() + "";

boolean gioitinh = false;//Nam =false

if (genderGroup.getCheckedRadioButtonId() == R.id.radNu)

gioitinh = true;

//Tạo một employee

SinhVien emp = new SinhVien();

emp.setMaSV(ma);

emp.setHoTen(ten);

emp.setGioiTinh(gioitinh);

//Đưa vào danh sách

arrSinhVien.add(emp);

//gọi hàm cập nhật giao diện

adapter.notifyDataSetChanged();

//Sau khi update thì xóa trắng dữ liệu và cho editma

focus

editMa.setText("");

editTen.setText("");

editMa.requestFocus();

}

});

btnRemoveAll.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

//ta nên đi ngược danh sách, kiểm tra phần tử nào

Page 9: 1 Radio Button Radio Group, CheckBox, SpinnerVer 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP.HCM

Ver 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 9

checked

//thì xóa đúng vị trí đó ra khỏi arrEmployee

for(int i = lvSinhVien.getChildCount() - 1; i >=0; i--)

{

//lấy ra dòng thứ i trong ListView

//Dòng thứ i sẽ có 3 phần tử: ImageView, TextView,

Checkbox

v = lvSinhVien.getChildAt(i);

//Ta chỉ lấy CheckBox ra kiểm tra

CheckBox chk = (CheckBox)

v.findViewById(R.id.chkitem);

//Nếu nó Checked thì xóa ra khỏi arrEmployee

if(chk.isChecked())

{

//xóa phần tử thứ i ra khỏi danh sách

arrSinhVien.remove(i);

}

}

adapter.notifyDataSetChanged();

}

});

2.5 Kết quả chương trình

Page 10: 1 Radio Button Radio Group, CheckBox, SpinnerVer 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP.HCM

Ver 1.0 – 2016, FIT - HCMUP Lab 04: Các loại Control

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM 10

Check chọn các sinh viên và bấm nút (X) để xóa.

---Hết---