ANDROID笔记:CheckBox的简单使用

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >



    <RadioGroup

        android:id="@+id/radiogroup"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >



        <RadioButton

            android:id="@+id/radioman"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:checked="true"

            android:text="男" />



        <RadioButton

            android:id="@+id/radiowoman"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="女" />



        <RadioButton

            android:id="@+id/radionoman"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="非人类" />

    </RadioGroup>



    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >



        <CheckBox

            android:id="@+id/checkandroid"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="android" />



        <CheckBox

            android:id="@+id/checknet"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text=".net" />



        <CheckBox

            android:id="@+id/checkjava"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="java" />

    </LinearLayout>



    <ToggleButton

        android:id="@+id/tbtn"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textOff="开"

        android:textOn="关" />



</LinearLayout>
 1 package com.example.classtest;

 2 

 3 import android.app.Activity;

 4 import android.os.Bundle;

 5 import android.widget.CheckBox;

 6 import android.widget.CompoundButton;

 7 import android.widget.RadioButton;

 8 import android.widget.RadioGroup;

 9 import android.widget.RadioGroup.OnCheckedChangeListener;

10 import android.widget.Toast;

11 import android.widget.ToggleButton;

12 

13 

14 public class MainActivity extends Activity implements

15         CompoundButton.OnCheckedChangeListener {

16 

17     @Override

18     protected void onCreate(Bundle savedInstanceState) {

19         super.onCreate(savedInstanceState);

20         setContentView(R.layout.checktest);

21         RadioGroup group = (RadioGroup) findViewById(R.id.radiogroup);

22         group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

23 

24             @Override

25             public void onCheckedChanged(RadioGroup arg0, int arg1) {

26 

27                 RadioButton button = (RadioButton) findViewById(arg1);

28                 Toast.makeText(MainActivity.this, button.getText(),

29                         Toast.LENGTH_SHORT).show();

30             }

31         });

32 

33         CheckBox chkjava = (CheckBox) findViewById(R.id.checkjava);

34         CheckBox chknet = (CheckBox) findViewById(R.id.checknet);

35         CheckBox chkandroidBox = (CheckBox) findViewById(R.id.checkandroid);

36         chkandroidBox.setOnCheckedChangeListener(this);

37         chkjava.setOnCheckedChangeListener(this);

38         chknet.setOnCheckedChangeListener(this);

39 

40         ToggleButton toggleButton = (ToggleButton) findViewById(R.id.tbtn);

41         toggleButton.setOnCheckedChangeListener(this);

42     }

43 

44     @Override

45     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

46         // TODO Auto-generated method stub

47         Toast.makeText(MainActivity.this,

48                 buttonView.getText().toString() + isChecked, Toast.LENGTH_SHORT)

49                 .show();

50     }

51 }

ANDROID笔记:CheckBox的简单使用

你可能感兴趣的:(checkbox)