Chủ Nhật, 19 tháng 1, 2014

Bài 13 check box in android -2


Blog chia sẻ kinh nghiệm lập trình Android
Chào các bạn hôm nay mình sẽ hướng dẫn các bạn cách bắt sự kiện ngay sau khi tick vào checkbox hay bỏ tích khỏi check box, như đã hướng dẫn ở bài trước chúng ta không sử dụng event cho check box mà sử dụng chekbox thông qua event của button.
 Các bạn tạo 1 project với tên các bạn đặt tùy ý, trong file mainactivity.xml các bạn thêm cho mình một checkbox có id là check


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="chuyển 2G/3G" />

    <CheckBox
        android:id="@+id/chb2g3g"
        android:layout_marginTop="20dp"
        android:layout_width="266dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginLeft="30dp"
        android:text="2G is available "
        android:layout_marginBottom="20dp"
        android:textColor="#FFFFFF" />
    
    </LinearLayout>

    <Button
        android:id="@+id/btexit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="exit" />

</LinearLayout>


Vậy là xong phần giao diện, các bạn chuyển sang phần main.class để tạo event cho checkbox


//tạo event cho checkbox
checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});




Sau khi hoàn tất các công đoạn , chúng ta chạy thử và ứng dụng sẽ như thế này .



full code main.class

package com.example.demo_chekbox;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends Activity {
Button btexit;
CheckBox checkBox;
// khởi tạo context để sử dụng Dialog
Context context=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// khai báo Id
btexit=(Button)findViewById(R.id.btexit);
checkBox=(CheckBox)findViewById(R.id.chb2g3g);

// gọi phương thức sử lí checkboxx
chekbox2g3g();

// gọi phương thức thoát khỏi ứng dụng
method_exit();
}//oncreate

// tạo phương thức sử lí checkbox
public void chekbox2g3g()
{
//tạo event cho checkbox
checkBox.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// mặc định ban đầu check box chưa đk tích và ở chế độ 2 G
// khi click vào check box sẽ chuyển lên 3G
if(checkBox.isChecked())
{
// xử lí khi check box được chọn
checkBox.setText("3G is available");// thay đổi text của check box
Toast.makeText(getApplication(), "Mạng 3G", 1).show();

}else// chỉ ở 2G nếu checkboxx không được check
{
// xử lí khi check box KHÔNG được chọn
checkBox.setText("2G is available");// thay đổi text của check box
Toast.makeText(getApplication(), "Mạng 2G", 1).show();

}
}
});

}
// phương thức thoát ưng dụng
public void method_exit()
{ btexit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//khởi tạo dialog hỏi xem có thoát hay không
AlertDialog.Builder b = new AlertDialog.Builder(context);
b.setTitle("Thoát ứng dụng");// tiêu đề của dialog
b.setMessage(" Thông báo !");//để viết thông báo của dialog
b.setNegativeButton("No", null);// tạo nút No trong dialog và khi click vào
// thì không làm gì cả

b.setNeutralButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// tạo nut "Yes" trên dialog khi nhấn vào sẽ thoát khỏi ứng dụng
System.exit(0);
}
});
// sau khi có đầy đủ các thuốc tính của dialog thì gọi dialog khi nhấn Button exit
b.show();
}
});

}
}





Vào giao diện đầu tiên khi chưa chọn checkbox



Chọn checkbox lập tức toast sẽ hiện thông báo chuyển sang mạng 3g đồng thời text của checkbox cũng chuển sang mạng 3G



 chọn Exit thì dialog hỏi thoát sẽ xuất hiện

Chúc các bạn thành công.

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: John Ly Phạm


◄ Back Next ►
 

Copyright 2014 Blog Tự Học Android - All Right Reserved Design by Garatemplate Published By Goo Quynh