全屏浏览
缩小浏览
回到页首

android基础组件---->Checkboxe的使用

  由于使用比较简单,这篇博客涵盖Checkboxes和Radio Buttons和Toggle Buttons。好了我们开始今天的学习。我被世俗隐瞒,转身又被自己撞倒。从莫须有的罪名起步,行色简单,心术复杂。

 

目录导航

  1.   Checkboxes的使用
  2.   Radio Buttons的使用
  3.   Toggle Buttons的使用
  4.   测试的源代码

 项目结构如下:

 

Checkboxes的使用

 复选框允许用户从集合中选择一个或多个选项。通常情况下,你应该给出一个垂直列表中的每个复选框选项。通常情况下,我们通过垂直的列表去展现复选框的选项。

一、 在布局文件中增加CheckBox组件。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/checkbox_meat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="meat" />

    <CheckBox
        android:id="@+id/checkbox_cheese"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cheese" />
</LinearLayout>

  二、 在代码中增加对CheckBox的使用。

// checkBox的测试
private void checkBox() {
    meatBox = (CheckBox) findViewById(R.id.checkbox_meat);
    cheeseBox = (CheckBox) findViewById(R.id.checkbox_cheese);

    meatBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "meatBox click", Toast.LENGTH_SHORT).show();
            cheeseBox.toggle();
        }
    });
    cheeseBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "cheeseBox click", Toast.LENGTH_SHORT).show();
            meatBox.setSelected(false);
        }
    });
}

 三、 CheckBox运行的效果如下:

 

Radio Buttons的使用

单选按钮允许用户选择从集合中选择一个选项。如果你认为用户需要看到所有可用的选项,您应该使用互斥的单选按钮。如果没有必要显示所有选项,用Spinner来代替。

一、 在布局文件中增加RadioGroup组件,在group中包含两个RadioButton

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radio_boy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="boy" />

        <RadioButton
            android:id="@+id/radio_girl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="girl" />
    </RadioGroup>
</LinearLayout>

 二、 在代码中增加对RadioButton的使用。 

// radio button的测试
public void onRadioButtonClicked(View view) {
    switch (view.getId()) {
        case R.id.radio_boy:
            Toast.makeText(MainActivity.this, "radio boy", Toast.LENGTH_SHORT).show();
            break;
        case R.id.radio_girl:
            Toast.makeText(MainActivity.this, "radio gril", Toast.LENGTH_SHORT).show();
            break;
        default:
            Toast.makeText(MainActivity.this, "default", Toast.LENGTH_SHORT).show();
            break;
    }
}

 三、 RadioButton运行的效果如下:

 

Toggle Buttons的使用

 切换按钮允许用户在两个状态之间进行设置。

  You can add a basic toggle button to your layout with the ToggleButton object. Android 4.0 (API level 14) introduces another kind of toggle button called a switch that provides a slider control, which you can add with a Switch object.

  If you need to change a button's state yourself, you can use the CompoundButton.setChecked() or CompoundButton.toggle() methods.

一、 在布局文件中增加ToggleButton组件

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

 二、 在代码中增加对ToggleButton的使用。

// toggleButton的测试
private void toggleButton() {
    toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                Toast.makeText(MainActivity.this, "is checked", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "no checked", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

 三、 RadioButton运行的效果如下:

 

 测试的源代码

MainActivity.java

package com.huhx.linux.checkboxtest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
    private CheckBox meatBox;
    private CheckBox cheeseBox;
    private RadioGroup radioGroup;
    private ToggleButton toggleButton;

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

        checkBox();
        toggleButton();
    }

    // toggleButton的测试
    private void toggleButton() {
        toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(MainActivity.this, "is checked", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "no checked", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // radio button的测试
    public void onRadioButtonClicked(View view) {
        switch (view.getId()) {
            case R.id.radio_boy:
                Toast.makeText(MainActivity.this, "radio boy", Toast.LENGTH_SHORT).show();
                break;
            case R.id.radio_girl:
                Toast.makeText(MainActivity.this, "radio gril", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(MainActivity.this, "default", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    // checkBox的测试
    private void checkBox() {
        meatBox = (CheckBox) findViewById(R.id.checkbox_meat);
        cheeseBox = (CheckBox) findViewById(R.id.checkbox_cheese);

        meatBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "meatBox click", Toast.LENGTH_SHORT).show();
                cheeseBox.toggle();
            }
        });
        cheeseBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "cheeseBox click", Toast.LENGTH_SHORT).show();
                meatBox.setSelected(false);
            }
        });
    }
}
MainActivity.java

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.huhx.linux.checkboxtest.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/checkbox_meat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="meat" />

        <CheckBox
            android:id="@+id/checkbox_cheese"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="cheese" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radio_boy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onRadioButtonClicked"
                android:text="boy" />

            <RadioButton
                android:id="@+id/radio_girl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onRadioButtonClicked"
                android:text="girl" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>
activity_main.xml

 

posted @ 2016-05-06 07:25  huhx  阅读(328)  评论(0编辑  收藏  举报