CheckBox và RadioButton đều sử dụng chung 2 phương thức :
a. phương thức setChecked
, dùng để thiết lập checked. Nếu ta gọi setChecked(true) tức là cho phép control được checked, còn gọi setChecked(false) thì control sẽ bị unchecked.
b. phương thức isChecked
, kiểm tra xem control có được checked hay không. Nếu có checked thì trả về true ngược lại trả về false.
- CheckBox cho phép ta checked nhiều đối tượng, còn RadioButton thì tại một thời điểm nó chỉ cho ta checked 1 đối tượng trong cùng một group mà thôi.
1. CheckBox
Nếu chúng ta muốn người sử dụng có thể chọn nhiều lựa chọn thì chúng ta nên sử dụng CheckBox, ví dụ xem hình bên dưới:
Ta có thể thiết lập cho CheckBox bất kỳ được checked mặc định trong XML:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <CheckBox android:id="@+id/chkIos" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ios" android:textSize="30sp"/> <CheckBox android:id="@+id/chkandroid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android" android:checked="true" android:textSize="30sp"/> <CheckBox android:id="@+id/chkWindows" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="windows" android:textSize="30sp"/> <Button android:id="@+id/btnDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Display" /> </LinearLayout>
MainActivity.java:
public class MainActivity extends Activity { private CheckBox chkIos, chkAndroid, chkWindows; private Button btnDisplay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addListenerOnChkIos(); addListenerOnButton(); } //khi click sẽ Toast lên cái vừa chọn public void addListenerOnChkIos() { chkIos = (CheckBox) findViewById(R.id.chkIos); chkIos.setOnClickListener(new OnClickListener() {
@Override public void onClick(View arg0) { // TODO Auto-generated method stub //is chkIos checked? if (((CheckBox) arg0).isChecked()) { Toast.makeText(MainActivity.this,"Ban chon ios", Toast.LENGTH_LONG).show(); } } }); } // kiem tra no da duoc check hay chua đúng là true sai là false public void addListenerOnButton() { chkIos = (CheckBox) findViewById(R.id.chkIos); chkAndroid = (CheckBox) findViewById(R.id.chkandroid); chkWindows = (CheckBox) findViewById(R.id.chkWindows); btnDisplay = (Button) findViewById(R.id.btnDisplay); btnDisplay.setOnClickListener(new OnClickListener() { //kiem tra khi click button display @Override public void onClick(View v) { StringBuffer result = new StringBuffer(); result.append("IPhone check : ").append(chkIos.isChecked()); result.append("\nAndroid check : ").append(chkAndroid.isChecked()); result.append("\nWindows Mobile check :").append(chkWindows.isChecked()); Toast.makeText(MainActivity.this,result.toString(), Toast.LENGTH_LONG).show(); } }); } }
2. RadioButton
Nếu chúng ta muốn người sử dụng chỉ được chọn 1 lựa chọn trong nhiều chọn lựa chúng ta đưa ra thì nên sử dụng RadioButton.
- Tương tự như CheckBox, ta cũng có thể thiết lập checked cho RadioButton bất kỳ trong XML:
- Nhìn vào hình trên chúng ta thấy là ta phải sử dụng RadioGroup để gom nhóm các RadioButton lại cùng một nhóm nào đó, những RadioButton mà cùng một nhóm thì tại 1 thời điểm chỉ có 1 RadioButton được checked mà thôi. Trong một màn hình ta có thể tạo nhiều nhóm RadioGroup khác nhau.
Dựa vào RadioGroup để biết chính xác Id của RadioButton nào được checked. Dựa vào Id này ta sẽ xử lý đúng nghiệp vụ:
Activity_main.xml
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" Android:layout_width="fill_parent" Android:layout_height="fill_parent" Android:orientation="vertical" > <RadioGroup Android:id="@+id/radioSet" Android:layout_width="wrap_content" Android:layout_height="wrap_content" > <TextView Android:id="@+id/textView1" Android:layout_width="320dp" Android:layout_height="43dp" Android:text="Bạn có quý ai trong lớp không ?? " Android:textSize="20sp" Android:layout_gravity="center" Android:gravity="center"/> <RadioButton Android:id="@+id/radio1" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:text="Có" Android:checked="true" Android:textSize="30sp" /> <RadioButton Android:id="@+id/radio2" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:text="Không" Android:textSize="30sp"/> </RadioGroup> <Button Android:id="@+id/btnDisplay" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:text="display" /> </LinearLayout>
ActivityMain.java
package apps1pro.com.radiobutton; import Android.support.v7.app.ActionBarActivity; import Android.os.Bundle; import Android.view.Menu; import Android.view.MenuItem; import Android.view.View; import Android.view.View.OnClickListener; import Android.widget.Button; import Android.widget.RadioButton; import Android.widget.RadioGroup; import Android.widget.Toast; public class MainActivity extends ActionBarActivity { private RadioGroup radioGroup; private RadioButton radioButton; private Button btnDisplay; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addListenerOnButton(); } public void addListenerOnButton() { radioGroup = (RadioGroup) findViewById(R.id.radioSet); btnDisplay = (Button) findViewById(R.id.btnDisplay); btnDisplay.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // get selected radio button from radioGroup int selectedId = radioGroup.getCheckedRadioButtonId(); // find the radiobutton by returned id radioButton = (RadioButton) findViewById(selectedId); Toast.makeText(MainActivity.this,radioButton.getText(), Toast.LENGTH_SHORT).show(); } }); } }
Như hình trên, chúng ta thấy hàm
getCheckedRadioButtonId()
: hàm này trả về Id của RadioButton nằm trong RadioGroup được checked. Dựa vào Id này chúng ta so sánh để biết được trên giao diện người sử dụng đang checked lựa chọn nào.