Android UI系列-----CheckBox和RadioButton(1)

xiaoluo501395377發表於2013-10-27

主要記錄一下CheckBox多選框和RadioGroup、RadioButton單選框的設定以及註冊監聽器

1.CheckBox

佈局檔案:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <CheckBox 
        android:id="@+id/eatId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吃飯"/>
    
    <CheckBox 
        android:id="@+id/sleepId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="睡覺"/>
    
    <CheckBox 
        android:id="@+id/playId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="玩遊戲"/>
    
    <CheckBox
        android:id="@+id/allId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="全選"/>

</LinearLayout>

MainActivity:

public class MainActivity extends Activity
{
    private CheckBox eatBox;
    private CheckBox sleepBox;
    private CheckBox playBox;
    private CheckBox allBox;

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

        eatBox = (CheckBox) findViewById(R.id.eatId);
        sleepBox = (CheckBox) findViewById(R.id.sleepId);
        playBox = (CheckBox) findViewById(R.id.playId);
        allBox = (CheckBox) findViewById(R.id.allId);

        // OnBoxClickListener listener = new OnBoxClickListener();
//        OnBoxCheckedListener listener2 = new OnBoxCheckedListener();
        CheckedBoxListener listener3 = new CheckedBoxListener();
        // eatBox.setOnClickListener(listener);
        // sleepBox.setOnClickListener(listener);
        // playBox.setOnClickListener(listener);

        eatBox.setOnCheckedChangeListener(listener3);
        sleepBox.setOnCheckedChangeListener(listener3);
        playBox.setOnCheckedChangeListener(listener3);

        allBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked)
            {
                eatBox.setChecked(isChecked);
                sleepBox.setChecked(isChecked);
                playBox.setChecked(isChecked);
            }
        });
    }

    class CheckedBoxListener implements OnCheckedChangeListener
    {
        private int count = 0;

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked)
        {
            if (isChecked)
            {
                count++;
                if (count == 3)
                {
                    allBox.setChecked(isChecked);
                }
            }
            else
            {
                count--;
                allBox.setChecked(isChecked);
            }
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // CheckBox點選監聽器
    class OnBoxClickListener implements OnClickListener
    {
        @Override
        public void onClick(View view)
        {
            CheckBox box = (CheckBox) view;
            if (box.getId() == R.id.eatId)
            {
                System.out.println("eatBox");
            } else if (box.getId() == R.id.sleepId)
            {
                System.out.println("sleepBox");
            } else if (box.getId() == R.id.playId)
            {
                System.out.println("playBox");
            }
            if (box.isChecked())
            {
                System.out.println("Box is checked");
            } else
            {
                System.out.println("Box is unChecked");
            }
            System.out.println("CheckBox is clicked!");
        }
    }

    // CheckBox狀態改變監聽器
    class OnBoxCheckedListener implements OnCheckedChangeListener
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked)
        {
            CheckBox box = (CheckBox) buttonView;
            if (box.getId() == R.id.eatId)
            {
                System.out.println("eatBox");
            } else if (box.getId() == R.id.sleepId)
            {
                System.out.println("sleepBox");
            } else if (box.getId() == R.id.playId)
            {
                System.out.println("playBox");
            }
            if (isChecked)
            {
                System.out.println(box.getText() + " is checked!");
            } else
            {
                System.out.println(box.getText() + " is unchecked!");
            }
        }
    }

}

2.RadioGroup和RadioButton

RadioGroup中可以放置多個RadioButton單選框,位於同一RadioGroup中的RadioButton每次只能選擇一個

<LinearLayout 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:orientation="vertical"
    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=".MainActivity" >

    <RadioGroup 
        android:id="@+id/radioGroupId1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <RadioButton 
            android:id="@+id/femailButtonId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
        
        <RadioButton
            android:id="@+id/maleButtonId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"/>
    </RadioGroup>
    
    <RadioGroup 
        android:id="@+id/raidoGroupId2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton 
            android:id="@+id/womenButtonId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="women"/>
        <RadioButton 
            android:id="@+id/manButtonId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="man"/>
    </RadioGroup>
    
</LinearLayout>

MainActivity:

public class MainActivity extends Activity
{
    private RadioGroup radioGroup;
    private RadioButton femaleRadio;
    private RadioButton maleRadio;
    private RadioGroup radioGroup2;
    private RadioButton womenRadio;
    private RadioButton manRadio;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        radioGroup = (RadioGroup)findViewById(R.id.radioGroupId1);
        femaleRadio = (RadioButton)findViewById(R.id.femailButtonId);
        maleRadio = (RadioButton)findViewById(R.id.maleButtonId);
        radioGroup2 = (RadioGroup)findViewById(R.id.raidoGroupId2);
        womenRadio = (RadioButton)findViewById(R.id.womenButtonId);
        manRadio = (RadioButton)findViewById(R.id.manButtonId);
        
        RadioGroupListener listener = new RadioGroupListener();
        radioGroup.setOnCheckedChangeListener(listener);
        radioGroup2.setOnCheckedChangeListener(listener);
    }

    class RadioGroupListener implements OnCheckedChangeListener
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
            if(checkedId == femaleRadio.getId() || checkedId == womenRadio.getId())
            {
                womenRadio.setChecked(true);
                femaleRadio.setChecked(true);
                System.out.println("femaleRadio is cheched!");
            }
            else if(checkedId == maleRadio.getId() || checkedId == manRadio.getId())
            {
                manRadio.setChecked(true);
                maleRadio.setChecked(true);
                System.out.println("maleRadio is checked!");
            }
        }
    }
    
    class RadioButtonListener implements android.widget.CompoundButton.OnCheckedChangeListener
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked)
        {
            if(isChecked)
            {
                System.out.println("RadioButton is checked!");
            }
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

注意:我們可以給RadioGroup註冊一個OnCheckedChangeListener,引用的是android.widget.RadioGroup.OnCheckedChangeListener這個包下的監聽器,其裡面的方法是:

     
    //  group表示當前選中的這一組的RadioGroup物件,checkId表示的是當前這組中選中的那個單選框的ID
     public
void onCheckedChanged(RadioGroup group, int checkedId) { if(checkedId == femaleRadio.getId() || checkedId == womenRadio.getId()) { womenRadio.setChecked(true); femaleRadio.setChecked(true); System.out.println("femaleRadio is cheched!"); } else if(checkedId == maleRadio.getId() || checkedId == manRadio.getId()) { manRadio.setChecked(true); maleRadio.setChecked(true); System.out.println("maleRadio is checked!"); } }

而我們還可以給每個RadioButton註冊一個OnCheckedChangeListener,但是這裡就要使用 android.widget.CompoundButton.OnCheckedChangeListener 這個監聽器類,其裡面的方法:

    
    //  buttonView表示的就是當前呼叫這個方法的那個RadioButton物件,isChecked表示當前是否為選擇
     public
void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { System.out.println("RadioButton is checked!"); } }

 

相關文章