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

Chào các bạn, tiếp tục bài 14, hôm nay mình sẽ hướng dẫn các bạn cách dùng Radiobutton với event của chúng luôn. Ở bài trước chúng ta bắt sự kiện thông qua button ok , bây giờ chúng ta bắt sự kiện ngay khi ấn click vào Radiobutton.

 Tạo project mới. trong main_activity.xml các bạn cho thêm 2 nút radiobutton, cho vào radiogroup.
project này chúng ta sẽ demo cách chuyển 2G <=> 3G và thông báo thành công lên Toast.

Code: mainactivity.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Demo Radiobutton"
        android:textColor="#ffffff"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="22dp"
        android:text="Chọn Loại Mạng"
        android:textColor="#ffffff"
        android:textSize="22dp" />

    <RadioGroup
        android:id="@+id/rdoGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FF6534" >

        <RadioButton
            android:id="@+id/rdo2g"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="2G" />

        <RadioButton
            android:id="@+id/rdo3g"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="3G" />
    </RadioGroup>
        <Button
            android:id="@+id/btexit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:textColor="#ffffff"
            android:text="Thoát" />
    </LinearLayout>

Giao diện sau khi tạo được file xml. 
  


Tiếp theo vào main class. 

full code main.class

package com.example.demo_radiobutton1;

import android.app.Activity;
import android.app.AlertDialog;
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.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

Button btok, btexit;
Context context = this;
RadioButton rdo2g, rdo3g;
RadioGroup group;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// khai báo id
rdo2g = (RadioButton) findViewById(R.id.rdo2g);
rdo3g = (RadioButton) findViewById(R.id.rdo3g);
group = (RadioGroup) findViewById(R.id.rdoGroup);

btexit = (Button) findViewById(R.id.btexit);

// để mặc định là mạng 2g thì để radiobutton 1 được chọn trước

rdo2g.setChecked(true);

// gọi các phương thức
method_exit();
method_infor();
}

// method okinfor
public void method_infor() {// sự kiện khi click vòa radiobutton 3g
rdo2g.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (rdo2g.isChecked()) {
Toast.makeText(getApplication(), "2G is connected", 1)
.show();
}
}
});

// sự kiện khi click vòa radiobutton 3g

rdo3g.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (rdo3g.isChecked()) {
Toast.makeText(getApplication(), "3g is connected", 1)
.show();
}
}
});
}

// mothod exit
public void method_exit() {
btexit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("thoát");
builder.setNegativeButton("không", null);
builder.setNeutralButton("có",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
System.exit(0);// thoát ứng dụng
}
});
builder.show();// gọi dialog khi clik button ok
}
});
}
}

Giải thích code : 







ok. các bạn chạy chương trình , kết quả được như sau :





   Sau bài này mình tin các bạn sẽ sử dụng thành thạo Radiobutton. Bài sau mình hướng dẫn các bạn sử dụng processbar.

 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



0 comments:

Post a Comment

http://knlaptrinhandroid.blogspot.com/

 
Top