[AndroidBasic] Bài 10- RadioButton-RadioGroup - Nguyễn Khoa Ninh
Có thể bạn quan tâm
Chào các bạn, ở bài 10 mình giới thiệu về RadioButton-RadioGroup.

Các nút chọn như hình trên được gọi là RadioButton, các bạn để ý rằng trong mỗi một nhóm các RadioButton chỉ được phép tích chọn một trong số các RadioButton. Vậy để quản lý được chúng một cách dễ dàng thì Android cung cấp thêm một thẻ có tên RadioGroup. Bình thường nếu ta viết 5 thẻ RadioButton liên tiếp nhau thì khi ta chọn ta có thể tích hết cả 5 thẻ RadioButton đó, nếu bây giờ ta nhóm 5 thẻ RadioButton đó vào trong một thẻ RadioGroup thì khi ta tích chọn chỉ được phép tích 1 mà thôi. Đó là tư tưởng của RadioButton trong Android.
Ứng dụng này ta sẽ viết một chương trình đăng ký tuyển dụng bao gồm các thông tin họ tên, giới tính,… thế thôi cho nó ít code 🙂

Giao diện ứng dụng sẽ như hình trên.
Tại file main_activity.xml ta có đoạn code sau:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/textBackground" android:text="Đăng ký tuyển dụng" android:textColor="@color/textTitle" android:textSize="20sp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Họ và tên: " android:textSize="@dimen/textSize" /> <EditText android:id="@+id/etHoTen" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:textSize="@dimen/textSize" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Giới tính: " android:textSize="@dimen/textSize" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/rbNam" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:text="Nam" android:textSize="@dimen/textSize" /> <RadioButton android:id="@+id/rbNu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Nữ" android:textSize="@dimen/textSize" /> </RadioGroup> <Button android:id="@+id/btXong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hoàn Tất" android:background="@color/textBackground" android:textColor="@color/textTitle" android:padding="3dp" android:layout_gravity="center_horizontal"/> </LinearLayout>Các bạn để ý thấy hai thẻ RadioButton mình đặt nằm trong cặp thẻ RadioGroup nhằm báo với hệ thống Android rằng “chỉ được phép chọn một trong hai chúng” Nếu bạn muốn tích trước một RadioButton nào đó thì thêm thuộc tính android:checked=”true” vào thẻ RadioButton đó.
Ngoài ra để tích trước bạn có thể sử dụng mã java với lệnh: rb.setChecked(true); trong đó rb chính là biến thuộc kiểu RadioButton được khai báo như sau: RadioButton rb = (RadioButton) findViewById(R.id.radioButton);
File java chính sẽ có nội dung sau:
package opin.myradiobutton; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Toast; public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener { private Button btHoanTat; private RadioButton rbNam, rbNu; private EditText etHoTen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btHoanTat = (Button) findViewById(R.id.btXong); rbNam = (RadioButton) findViewById(R.id.rbNam); rbNu = (RadioButton) findViewById(R.id.rbNu); etHoTen = (EditText) findViewById(R.id.etHoTen); rbNam.setOnCheckedChangeListener(this); rbNu.setOnCheckedChangeListener(this); btHoanTat.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (etHoTen.getText().toString().equals("")) { // Nếu chưa nhập họ tên Toast.makeText(MainActivity.this, "Đề nghị ghi rõ họ tên", Toast.LENGTH_LONG).show(); return; // Thoát khỏi hàm onClick } if (!rbNam.isChecked() && !rbNu.isChecked()) { // Nếu chưa chọn giới tính thì hiện lên thông báo Toast.makeText(MainActivity.this, "Đề nghị chọn giới tính", Toast.LENGTH_LONG).show(); return; // Thoát khỏi hàm onClick } // nếu chọn nam thì trả về xâu "Nam", ngược lại thì xâu "Nữ" String gioiTinh = (rbNam.isChecked()) ? "Nam" : "Nữ"; Toast.makeText( MainActivity.this, "Họ tên: " + etHoTen.getText() + "\nGiới tính:" + gioiTinh, Toast.LENGTH_LONG).show(); } }); } // Hệ thống gọi hàm này khi có sự thay đổi lựa chọn giới tính @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // buttonView là biến thể hiện "View" vừa được thao tác // isChecked thể hiện giá trị của "View" vừa được thao tá // trong ví dụ này View ở đây chính là 2 RadioButton rbNam và rbNu if (isChecked) { if ((RadioButton)buttonView == rbNam) { Toast.makeText(this, "Bạn vừa chọn Nam", Toast.LENGTH_SHORT).show(); } if (buttonView == rbNu) { Toast.makeText(this, "Bạn vừa chọn Nữ", Toast.LENGTH_SHORT).show(); } } } }Đoạn mã trên sử dụng hàm onCheckedChanged() để bắt sự kiện khi người dùng có hành động với các RadioButton. Hàm này được viết đè trong interface CompoundButton.OnCheckedChangeListener, do đó khi viết đè hàm ta phải implements CompoundButton.OnCheckedChangeListener vào class.
Thôi bây giờ thì chạy đi 🙂 các bạn có thể download source code ở đây: https://www.dropbox.com/sh/rq6e76emygwbina/AADVaRFtE_vxyGq_wkUdJ9PFa
Chia sẻ:
- X
Có liên quan
Từ khóa » Sử Dụng Radiobutton Trong Android
-
RadioButton Và RadioGroup Trong Lập Trình Android
-
Hướng Dẫn Và Ví Dụ Android RadioGroup Và RadioButton
-
Lập Trình Android - RadioButton - Hiệp Sĩ IT
-
Nút Chọn | Android Developers
-
Bài Tập 11: Sử Dụng Checkbox Và RadioButton Trong Android
-
Android: RadioButton Trong Android | V1Study
-
AJ 09: RadioButton Và RadioGroup Trong Lập Trình Android - YouTube
-
Lập Trình Android A-Z – Bài 29: Radio Button - YouTube
-
Bài 7. CheckBox Và RadioButton - Tự Học Tin
-
RadioButton Và RadioGroup - Trần Ngọc Minh Notes
-
Lập Trình Android - Bài 7: Toast, CheckBox, RadioButton, Dialog
-
Thêm Lề Giữa RadioButton Và Nhãn Của Nó Trong Android? - HelpEx
-
Android Nhận Giá Trị Từ Nút Radio đã Chọn - HelpEx
-
Radio Button Trong Lập Trình Android | Kênh Sinh Viên