CheckBox trong Android
CheckBox là thành phần thể hiện trạng thái chọn (checked ) hoặc không chọn (unchecked ) CheckBox thường dùng khi người dùng có nhiều lựa chọn và được phép chọn một hoặc nhiều lựa chọn cùng lúc.
Lớp CheckBox là lớp con của lớp CompoundButton.
Các phương của CheckBox
Lớp CheckBox kế thừa nhiều phương thức của View, TextView hoặc Button
Một vài phương thức thường sử dụng:
Phương thức | Ý nghĩa |
---|---|
public boolean isChecked() | True nếu CheckBox là checked, ngược lại false |
public void setChecked(boolean status) | Thay đổi trạng thái của CheckBox |
CheckBox code trong XML
<CheckBox android:id="@+id/chksimpleCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Simple CheckBox"/>
CheckBox code trong Java
//initiate a check box CheckBox chksimpleCheckBox = (CheckBox) findViewById(R.id.chksimpleCheckBox); //check current state of a check box (true or false) Boolean checkBoxState = chksimpleCheckBox.isChecked();
Thuộc tính thường dùng của CheckBox
Bây giờ chúng xem một số thuộc tính hay sử dụng trong CheckBox trong tập tin XML
1. android:id: Là thuộc tính duy nhất của CheckBox. Xem ví dụ sau:
<CheckBox android:id="@+id/chksimpleCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Simple CheckBox"/>
Dựa vào Id ta sẽ lấy được control theo đúng Id này, xem code bên dưới để biết cách lấy control theo Id:
CheckBox chkSimpleCheckBox= (CheckBox) findViewById(R.id.chkSimpleCheckBox);
2. android:checked: checked là thuộc tính của CheckBox dùng để set trạng thái của CheckBox. Giá trị là true hoặc false, nếu giá trị là true thì trạng thái CheckBox là checked, ngược lại là false thì trạng thái của CheckBox là unchecked. Chúng ta cũng có thể set trạng thái của CheckBox bên Java Code bằng cách dùng phương thức setChecked(boolean status).
<CheckBox android:id="@+id/chksimpleCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Simple CheckBox" android:checked="true"/> <!--set the current state of the check box-->
Set trạng thái của CheckBox trong Java Class
/*Add in Oncreate() funtion after setContentView()*/ // initiate a check box CheckBox chksimpleCheckBox = (CheckBox) findViewById(R.id.chksimpleCheckBox ); // set the current state of a check box chksimpleCheckBox .setChecked(true);
3. android:gravity: Thuộc tính này thường sử dụng để canh nội dung trong CheckBox: left, right, center, top, bottom, center_vertical, center_horizontal.
<CheckBox android:id="@+id/chksimpleCheckBox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Simple CheckBox" android:checked="true" android:gravity="right|center_vertical"/> <!-- gravity of the check box-->
4. android:text: thuộc tính text dùng hiển thị nội dung trong một CheckBox. Chúng ta có thể set thuộc tính này trong tập tin xml hoặc java code
<CheckBox android:id="@+id/chksimpleCheckBox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:checked="true" android:text="Hiep si IT"/> <!--displayed text of the check box-->
Thiết lập thuộc tính text của CheckBox trong Java class:
/*Add in Oncreate() funtion after setContentView()*/ // initiate check box CheckBox chksimpleCheckBox = (CheckBox) findViewById(R.id.chksimpleCheckBox ); // displayed text of the check box chksimpleCheckBox.setText("Hiệp Sĩ IT");
4. android:textColor: Thuộc tính này dùng xác định màu chữ, dạng màu chữ: “#argb”, "#rgb”, “#rrggbb”, hoặc “#aarrggbb”. Vi dụ sau set nội dung văn bản trong CheckBox màu đỏ.
<CheckBox android:id="@+id/chksimpleCheckBox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:checked="true" android:textColor="#f00" android:text="Hiep si IT"/> <!--displayed text of the check box-->
Thiết lập thuộc tính textColor trong Java class:
/*Add in Oncreate() funtion after setContentView()*/ //initiate the checkbox CheckBox chksimpleCheckBox = (CheckBox) findViewById(R.id.chksimpleCheckBox); //red color for displayed text chksimpleCheckBox.setTextColor(Color.RED);
5. android:textSize: Thuộc tính textSize xác định kích thước nội dung văn bản của CheckBox. Chúng ta có thể đặt kích thước văn bản theo: sp(scale independent pixel) hoặc dp(density pixel). Trong ví dụ này chúng ta xác định kich thước cho văn bản là 40sp.
<CheckBox android:id="@+id/chksimpleCheckBox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:checked="true" android:textColor="#f00" android:text="Hiep si IT" android:textSize="40sp"/><!--set Text Size of text in CheckBox-->
Thiết lập thuộc tính TextSize của CheckBox trong Java class:
/*Add in Oncreate() funtion after setContentView()*/ CheckBox chksimpleCheckBox = (CheckBox) findViewById(R.id.chksimpleCheckBox ); //set 40sp displayed text size chksimpleCheckBox .setTextSize(40);
6. android:textStyle: Thuộc tính xác định loại văn bản của CheckBox, thông thường có các loại văn bản:bold, italic và normal. Nếu chúng ta muốn sử nhiều hơn một loại văn bản thì phải thêm phép toán hoặc "|" vào giữa các loại văn bản. Trong ví dụ sau chúng ta set bold và italic cho chuỗi văn bản của CheckBox:
<CheckBox android:id="@+id/chksimpleCheckBox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:checked="false" android:textColor="#f00" android:text="Hiep si IT" android:textSize="20sp" android:textStyle="bold|italic"/><!--set Text style of text in CheckBox-->
7. android:background: Thuộc tính này xác định màu nền cho CheckBox.
8. android:padding: Thuộc tính này xác định khoảng cách từ đường viền của CheckBox với nội dung nó chứa: left, right, top or bottom. Cũng ví dụ trên bây giờ chúng ta xác định padding=30sp từ mọi phía của CheckBox.
<CheckBox android:id="@+id/chksimpleCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Padding Attribute Of Check Box" android:textColor="#44f" android:textSize="20sp" android:textStyle="bold|italic" android:checked="false" android:padding="30dp"/> <!--30dp padding from all side's-->
Ví dụ: Trong ví dụ này chúng ta sẽ làm app chọn 1 ngôn ngữ lập trình trên các đối tượng CheckBox. Khi người sử dụng click lên CheckBox sẽ hiển thị ngôn ngữ vừa chọn, thông qua việc sử dụng đối tượng TOAST (đối tượng này sẽ học trong những bài sau) . Tiến hành tạo project, vào thư mục res /layout -> activity_main.xml thiết kế giao diện sau:
Bước 1: Tạo một project tên là TextView: File->New->Android Application Project điền các thông tin ->Next ->Finish
Bước 4: Mở res -> layout -> xml (hoặc) activity_main.xml và thêm code, chúng ta sẽ tạo các đối tượng CheckBox Relative Layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" 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="hiepsiit.com.checkbox.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select Your Programming language: " android:textColor="#f00" android:textSize="20sp" android:textStyle="bold" /> <LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:background="#e0e0e0" android:orientation="vertical"> <CheckBox android:id="@+id/chkAndroid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:checked="false" android:padding="27dp" android:text="@string/android" android:textColor="#44f" android:textSize="20sp" android:textStyle="bold|italic" /> <CheckBox android:id="@+id/chkJava" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:checked="false" android:padding="27dp" android:text="@string/java" android:textColor="#f44" android:textSize="20sp" android:textStyle="bold|italic" /> <CheckBox android:id="@+id/chkPHP" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:checked="false" android:padding="27dp" android:text="@string/php" android:textColor="#444" android:textSize="20sp" android:textStyle="bold|italic" /> <CheckBox android:id="@+id/chkPython" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:checked="false" android:padding="27dp" android:text="@string/python" android:textColor="#888" android:textSize="20sp" android:textStyle="bold|italic" /> <CheckBox android:id="@+id/chkUnity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:checked="false" android:padding="27dp" android:text="@string/unity" android:textColor="#101010" android:textSize="20sp" android:textStyle="bold|italic" /> </LinearLayout> </RelativeLayout>
Chuyển quan XML đổi tên các điều khiển:
Điều khiển | android:id | android:text |
---|---|---|
CheckBox1 | chkAndroid | @string/android |
CheckBox2 | chkJava | @string/java |
CheckBox3 | chkPHP | @string/php |
CheckBox4 | chkPython | @string/python |
CheckBox5 | chkunity | @string/unity |
Vào thư mục res/values bổ sung string.xml (Chúng ta không khai báo trực tiếp nhãn vào các điều khiển, mà thông qua string.xml)
<resources> <string name="app_name">CheckBoxExample</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="android">Android</string> <string name="java">Java</string> <string name="php">PHP</string> <string name="python" >Python</string> <string name="unity">Unity 3D</string> </resources>
Bước 3: Mở app -> src ->MainActivity.java và thêm code. Khi click vào CheckBox sẽ lấy các giá trị của CheckBox, sau dùng đối tượng TOAST để hiển thị.
package hiepsiit.com.checkbox; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.CheckBox; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ CheckBox android, java, python, php, unity3D; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initiate views android = (CheckBox) findViewById(R.id.chkAndroid); android.setOnClickListener(this); java = (CheckBox) findViewById(R.id.chkJava); java.setOnClickListener(this); python = (CheckBox) findViewById(R.id.chkPython); python.setOnClickListener(this); php = (CheckBox) findViewById(R.id.chkPHP); php.setOnClickListener(this); unity3D = (CheckBox) findViewById(R.id.chkUnity); unity3D.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.chkAndroid: if (android.isChecked()) Toast.makeText(getApplicationContext(), "Android", Toast.LENGTH_LONG).show(); break; case R.id.chkJava: if (java.isChecked()) Toast.makeText(getApplicationContext(), "Java", Toast.LENGTH_LONG).show(); break; case R.id.chkPHP: if (php.isChecked()) Toast.makeText(getApplicationContext(), "PHP", Toast.LENGTH_LONG).show(); break; case R.id.chkPython: if (python.isChecked()) Toast.makeText(getApplicationContext(), "Python", Toast.LENGTH_LONG).show(); break; case R.id.chkUnity: if (unity3D.isChecked()) Toast.makeText(getApplicationContext(), "Unity 3D", Toast.LENGTH_LONG).show(); break; } } }
Ứng dụng này được phát triển bởi adt bundle, android 4.2 sử dụng minimum sdk 11 and target sdk 21.
Kết quả khi chạy ứng dụng:
Sau đó Click vào một CheckBox bất kỳ hiển thị thông tin:
Cập nhật công nghệ từ Youtube tại link: https://www.youtube.com/channel/UCOxeYcvZPGf-mGLYSl_1LuA/videos
Để tham gia khóa học công nghệ truy cập link: http://thuvien.hocviendaotao.com
Mọi hỗ trợ về công nghệ email: dinhanhtuan68@gmail.com