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

  由于使用比较简单,这篇博客涵盖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

时间: 2024-08-06 03:45:00

android基础组件---->Checkboxes的使用的相关文章

Android 基础组件

基础组件 所有的控件都可以在java代码中创建出来,并且大部分的属性都对应set和get方法,比如 View view = new View(Context context)  context是上下文,是Activity父类,一般传入当前Activity 1.TextView text 文本 setText() getText() textColor文本颜色 #FFFFFF setTextColor(Color.Blue) getTextColor() textSize文本大小   sp set

android基础组件----&gt;Buttons的使用

按钮由文本或图标(或文本和一个图标)组成,当用户触摸到它时,会发生一些动作.今天我们开始Button的学习. Button的简要说明 根据你是否想要一个带有文本的按钮,一个图标,或者两者,你可以在三种方式中创建按钮来进行布局: With text, using the Button class: <Button android:layout_width="wrap_content" android:layout_height="wrap_content" an

android基础组件----&gt;Pickers的使用

Android为提供了一个随时可用的对话框,方便用户选取时间或者日期.今天我们就简单的学习一下Picker的使用. 项目结构如下: 创建一个Time Picker 创建一个TimePickerFragment class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { @Override public void onTimeSet(TimePicker view,

Android基础入门教程——2.3.12 Date &amp; Time组件(下)

Android基础入门教程--2.3.12 Date & Time组件(下) 标签(空格分隔): Android基础入门教程 本节引言: 本节我们来继续学习Android系统给我们提供的几个原生的Date & Time组件,他们分别是: DatePicker(日期选择器),TimePicker(时间选择器),CalendarView(日期视图),好吧, 其实一开始让我扣这几个玩意我是拒绝的,因为在我的印象里,他们是这样的: 简直把我丑哭了,有木有,终于知道为什么那么多人喜欢自定义这种类型的

android四大基础组件--Service生命周期详解

android四大基础组件--ServiceService 生命周期详解 1.Service的生命周期: I> 在非绑定Service情况下,只有oncreate(),onStartCommand(),onDestory()方法情况下:  操作方法对应生命周期一: a.[执行startService(Intent)] 执行生命周期方法:oncreate()--->onStartCommand(): b.[执行stopService(Intent)] 执行生命周期方法:onDestory();

Android基础之五:四大组件(Broadcast Receiver)

Broadcast Receiver作为Android四大组件之一,在整个系统中广泛运用,系统中存在各种各样的广播机制,例如下载,网络等都有具体的广播接收器 广播在很大程度上简化了开发,可以通过广播监听系统状态变化,监听另一App中事件传递,可以接收当前App中不同组件,不同UI.不同线程之间的消息传递 广播的创建与使用 创建一个继承自BroadcastReceiver类的子类,重写onReceiver方法 public class MReceiver extends BroadcastRece

Android基础之四大组件---Activity

Android基础之四大组件-Activity 1.什么是Activity 2.Activity生命周期 3.Activity生命周期演示 4.Activity之间的通信 5.Activity之加载模式 6.Activity的栈式管理 1.什么是Activity? Activity是用户接口程序,它是Android应用程序的基本功能单元,它的主要功能是提供界面.Activity是Android的核心类,该类的全名是android.app.Activity.Activity相当于C/S程序中的窗体

Android 基础总结:(二)Android APP基础及组件

1.Android APP基础 Android应用程序是用Java编程语言写的.编译后的Java代码--包括应用程序要求的任何数据和资源文件,通过aapt工具捆绑成一个Android包,归档文件以.apk为后缀.这个文件是分发应用程序和安装到移动设备的中介或工具,用户下载这个文件到他们的设备上.一个.apk文件中的所有代码被认为是一个应用程序. aapt: aapt是Android Asset Packaging Tool的首字母缩写,这个工具包含在SDK的tools/目录下.查看.创建.更新与

【Android基础】-Service组件使用详解

Service是Android四大组件之一,它与Activity的区别是:它一直在后台运行,没有前台界面.一旦Service被启动起来后,他就跟Activity一样,完全具有自己的生命周期. 一.创建Service,定义一个继承Service的子类 Service中定义了一系列生命周期方法,如下: IBinder onBind(Intent intent):该方法是Service必须实现的方法.该方法返回一个IBinder对象,应用程序可以通过该对象与Service组件通信. void onCr