Blog chia sẻ kinh nghiệm lập trình Android

Chào các bạn, hôm nay mình sẽ chia sẻ cho các bạn việc sử dụng check box trong android. 
Check box là những ô mà bạn có thể đánh dấu tích hay bỏ dấu tích để chọn hay bỏ chọn một mục nào đó mà bạn muốn. về công dụng của checkbox chắc không cần bàn nhiều , giờ chúng ta tập trung sử dụng chúng nhé :
  Như những bài trước bạn tạo 1 project với tên bạn muốn đặt, tại main_activity.xml bạn thêm các check box như sau : (dùng với linearlayout các layout khác tương tự nhé).



Thêm đoạn code đó vào các bạn sẽ thấy hiện lên 1 check box.  
<CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:text="textcheckbox " />
Các bạn tạo một giao diện như sau :



ĐÂY là Code :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000FFF"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textSize="25dp"
        android:gravity="center"
        android:text="Demo Check box in android" />
<LinearLayout
    android:layout_marginTop="20dp"
    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FF45FF"
        android:orientation="vertical" >
    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20dp"
        android:background="#ffffff"
        android:text="Món ăn mà bạn thích" />

    <CheckBox
        android:id="@+id/cbthitga"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:text="thịt gà " />

    <CheckBox
        android:id="@+id/cblonquay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:text="thịt lợn quay" />

    <CheckBox
        android:id="@+id/cbthitbo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:text="thịt bò" />

    <CheckBox
        android:id="@+id/cbcalau"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:text="cá lấu" />
<LinearLayout
    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#008899"
        android:weightSum="20"
        >
    <CheckBox
        android:id="@+id/cball"
        android:layout_width="match_parent"
        android:textColor="#FFFFFF"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:gravity="center"
        android:text="chọn hết" />

    <CheckBox
        android:id="@+id/cbdisall"
        android:layout_width="match_parent"
        android:textColor="#FFFFFF"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:gravity="center"
        android:text="bỏ chọn hết" />
    </LinearLayout>
    </LinearLayout>
<LinearLayout
    android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <Button
        android:id="@+id/btok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:text="ok" />

    <Button
        android:id="@+id/btexit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:layout_weight="10"
        android:text="exit" />
  </LinearLayout>
</LinearLayout>

Vậy là đã xong phần tạo giao diện, bây giờ chúng ta sử dụng các checkbox vừa tạo.

Khai báo id trong min.class




Tiếp theo các bạn tạo phương thức cho sự kiện click button ok để sử lí check box.





Code 

package com.example.demo_chekbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends Activity {
    CheckBox cbga,cblon,cbbo,cbca,cball,cbdisall;
    Button btok, btexit;
    String text;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // khai báo Id
  cball=(CheckBox)findViewById(R.id.cball);
  cbbo=(CheckBox)findViewById(R.id.cbthitbo);
  cbca=(CheckBox)findViewById(R.id.cbcalau);
  cblon=(CheckBox)findViewById(R.id.cblonquay);
  cbdisall=(CheckBox)findViewById(R.id.cbdisall);
  cbga=(CheckBox)findViewById(R.id.cbthitga);
  
  btexit=(Button)findViewById(R.id.btexit);
  btok=(Button)findViewById(R.id.btok);
  
  // gọi phương thức
  setCball();
  setCbdisall();
  setBtok();
 }//oncreate
 
 public  void setCball()
 {
   if(cball.isChecked())
   {
    cbbo.setChecked(true);
    cbca.setChecked(true);
    cbga.setChecked(true);
    cbbo.setChecked(true);
    cblon.setChecked(true);
    cbdisall.setChecked(false);
   
   }
  
 }
 
 public void setCbdisall()
 {

   if(cbdisall.isChecked())
   {
    cbbo.setChecked(false);
    cbca.setChecked(false);
    cbga.setChecked(false);
    cbbo.setChecked(false);
    cblon.setChecked(false);
    cball.setChecked(false);
   
   }
 }
 
 public void setBtok() {
  btok.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View arg0) {
    setCball();
    setCbdisall();
   }
  });
 }

}
Chạy ứng dụng và các bạn xem kết quả. ở bài viết này chỉ nêu ra cách dùng chekbox dơn giản nhất.
Bài Viết sau mình sẽ hướng dẫn các bạn up date lại thông tin ngay sau khi nhấn ticks hay bỏ tích trên checkboxx
Mọi thắc mắc các bạn liên hệ mình qua địa chỉ Gmail : svk10acntt@gmail.com hoặc Fb: https://www.facebook.com/pham.tienphong.37

0 comments:

Post a Comment

http://knlaptrinhandroid.blogspot.com/

 
Top