文章目录

  • 1 CheckBox
    • 1.1 CheckBox介绍
  • 2 RadioButton
    • 2.1 RadioButton介绍
  • 3 ToggleButton
    • 3.1 ToggleButton介绍
  • 4 SeekBar
    • 4.1 SeekBar介绍
  • 5 综合演示案例

1 CheckBox

1.1 CheckBox介绍

CheckBox系统封装的复选控件,主要有两种状态:选中及未选中。我们可以监听状态变化: setOnCheckedChangeListener。

简单看下XML文件:

<CheckBoxandroid:id="@+id/checkBox"android:layout_width="match_parent"android:layout_height="wrap_content"android:checked="true"android:text="CheckBox"/>

下面看下java代码:

package com.imooc.demo;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);CheckBox checkBox = findViewById(R.id.checkBox);// 设置是否选中(设置它的状态)checkBox.setChecked(false);// 获取它的状态(是否选中)boolean isChecked = checkBox.isChecked();Log.d(TAG, "onCreate, isChecked:" + isChecked);checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {Log.d(TAG, "onCheckedChanged: "+ isChecked);// 当状态被改变的时候,可以处理很多的数据和UI}});}
}

2 RadioButton

2.1 RadioButton介绍

RadioButton是单选控件可以和RadioGroup一起使用,只能选择一个。

RadioButton和CheckBox区别:

  1. 通过点击无法变为未选中
  2. 一组RadioButton,只能同时选中一个
  3. 在大部分UI框架中默认都以圆形表示

xml文件如下:

<RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"><RadioButtonandroid:id="@+id/radioButton2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="RadioButton"/><RadioButtonandroid:id="@+id/radioButton3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="RadioButton"/><RadioButtonandroid:id="@+id/radioButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="RadioButton"/></RadioGroup>

3 ToggleButton

3.1 ToggleButton介绍

ToggleButton用来切换程序中的状态,主要有两种状态:

  • android:textOn
  • android:textOff

设置其setChecked(boolean),点击事件setOnCheckedChangeListener。

下面简单看下其xml文件:

<ToggleButtonandroid:id="@+id/toggleButton2"android:layout_width="match_parent"android:layout_height="wrap_content"android:textOn="hello"android:textOff="bye bye"android:checked="true"android:text="ToggleButton"/>

4 SeekBar

4.1 SeekBar介绍

SeekBar主要用来设置进度,效果如下:

xml文件如下:

<SeekBarandroid:id="@+id/seekBar"android:layout_width="match_parent"android:layout_height="wrap_content"android:max="100"android:progress="30"/>

java代码对应如下:

package com.imooc.demo;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);SeekBar seekBar = findViewById(R.id.seekBar);seekBar.setProgress(40);seekBar.setMax(100);seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {Log.d(TAG, "onProgressChanged: "+ progress);}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {Log.d(TAG, "onStartTrackingTouch: " + seekBar.getProgress());}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {Log.d(TAG, "onStopTrackingTouch: " +  seekBar.getProgress());}});}
}

5 综合演示案例

界面如下:

首先看下xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns: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=".MainActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="10dp"android:background="#B9B9FF"android:text="@string/start_select_food"android:textAlignment="center"android:textColor="#8A2BE2"android:textSize="30sp"android:textStyle="bold|italic"android:typeface="monospace"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="45dp"android:layout_marginLeft="15dp"android:gravity="center_vertical"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/name"android:textSize="22sp"/><EditTextandroid:id="@+id/nameEditText"android:layout_width="200dp"android:layout_height="wrap_content"android:hint="@string/edit_text_input_hint_name"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="45dp"android:layout_marginLeft="15dp"android:gravity="center_vertical"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/sex"android:textSize="22sp"/><RadioGroupandroid:id="@+id/sexRadioGroup"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/maleRadioButton"android:textSize="22sp"android:layout_width="75dp"android:layout_height="wrap_content"android:text="@string/male"/><RadioButtonandroid:id="@+id/femaleRadioButton"android:textSize="22sp"android:layout_width="75dp"android:layout_height="wrap_content"android:text="@string/female"/></RadioGroup></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="45dp"android:layout_marginLeft="15dp"android:gravity="center_vertical"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="喜好"android:textSize="22sp"/><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"><CheckBoxandroid:id="@+id/hotCheckBox"android:text="辣"android:textSize="22sp"android:layout_width="65dp"android:layout_height="wrap_content"/><CheckBoxandroid:id="@+id/fishCheckBox"android:text="海鲜"android:textSize="22sp"android:layout_width="100dp"android:layout_height="wrap_content"/><CheckBoxandroid:id="@+id/sourCheckBox"android:text="酸"android:textSize="22sp"android:layout_width="65dp"android:layout_height="wrap_content"/></LinearLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="45dp"android:layout_marginLeft="15dp"android:gravity="center_vertical"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="预算"android:textSize="22sp"/><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0元"android:textSize="22sp"/><SeekBarandroid:id="@+id/seekBar"android:textSize="22sp"android:layout_width="220dp"android:max="100"android:layout_height="wrap_content"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="100元"android:textSize="22sp"/></LinearLayout></LinearLayout><Buttonandroid:id="@+id/searchButton"android:layout_width="300dp"android:layout_height="50dp"android:text="寻找菜品"android:layout_gravity="center_horizontal"android:gravity="center_horizontal"android:textSize="22sp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:orientation="vertical"android:layout_height="0dp"android:layout_weight="1"><ImageViewandroid:id="@+id/foodImageView"android:src="@drawable/ic_launcher_foreground"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="3"/><ToggleButtonandroid:id="@+id/showToggleButton"android:textOff="下一个"android:textOn="显示信息"android:layout_gravity="center_horizontal"android:gravity="center_horizontal"android:textSize="22sp"android:layout_width="300dp"android:layout_height="50dp"/></LinearLayout></LinearLayout>

java文件如下:

package com.imooc.demo;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.ToggleButton;import com.imooc.demo.model.Food;
import com.imooc.demo.model.Person;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private EditText mNameEditText;private RadioGroup mSexRadioGroup;private CheckBox mHotCheckBox, mFishCheckBox, mSourCheckBox;private SeekBar mSeekBar;private Button mSearchButton;private ImageView mFoodImageView;private ToggleButton mToggleButton;private List<Food> mFoods;private Person mPerson;private List<Food> mFoodResult;private boolean mIsFish;private boolean mIsSour;private boolean mIsHot;private int mPrice;private int mCurrentIndex;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 初始化控件findViews();// 初始化数据initData();// 为控件添加监听器,实现基本功能setListeners();// 自测}private void initData() {// new 出来一个空的食物 listmFoods = new ArrayList<>();// 初始化添加所有的数据mFoods.add(new Food("麻辣香锅", 55, R.drawable.malaxiangguo, true, false, false));mFoods.add(new Food("水煮鱼", 48, R.drawable.shuizhuyu, true, true, false));mFoods.add(new Food("麻辣火锅", 80, R.drawable.malahuoguo, true, true, false));mFoods.add(new Food("清蒸鲈鱼", 68, R.drawable.qingzhengluyu, false, true, false));mFoods.add(new Food("桂林米粉", 15, R.drawable.guilin, false, false, false));mFoods.add(new Food("上汤娃娃菜", 28, R.drawable.wawacai, false, false, false));mFoods.add(new Food("红烧肉", 60, R.drawable.hongshaorou, false, false, false));mFoods.add(new Food("木须肉", 40, R.drawable.muxurou, false, false, false));mFoods.add(new Food("酸菜牛肉面", 35, R.drawable.suncainiuroumian, false, false, true));mFoods.add(new Food("西芹炒百合", 38, R.drawable.xiqin, false, false, false));mFoods.add(new Food("酸辣汤", 40, R.drawable.suanlatang, true, false, true));mPerson = new Person();mFoodResult = new ArrayList<>();}private void findViews() {mNameEditText = findViewById(R.id.nameEditText);mSexRadioGroup = findViewById(R.id.sexRadioGroup);mHotCheckBox = findViewById(R.id.hotCheckBox);mFishCheckBox = findViewById(R.id.fishCheckBox);mSourCheckBox = findViewById(R.id.sourCheckBox);mSeekBar = findViewById(R.id.seekBar);mSeekBar.setProgress(30);mSearchButton = findViewById(R.id.searchButton);mToggleButton = findViewById(R.id.showToggleButton);mToggleButton.setChecked(true);mFoodImageView = findViewById(R.id.foodImageView);}private void setListeners() {// 设置单选框listenermSexRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId){case R.id.maleRadioButton:mPerson.setSex("男");break;case R.id.femaleRadioButton:mPerson.setSex("女");break;}}});// 设置复选框listenermFishCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {mIsFish = isChecked;}});mSourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {mIsSour = isChecked;}});mHotCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {mIsHot = isChecked;}});mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {mPrice = seekBar.getProgress();Toast.makeText(MainActivity.this, "价格: " + mPrice, Toast.LENGTH_SHORT).show();}});mSearchButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {search();}});mToggleButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(mToggleButton.isChecked()){//  下一个菜mCurrentIndex ++;if(mCurrentIndex < mFoodResult.size()){mFoodImageView.setImageResource(mFoodResult.get(mCurrentIndex).getPic());} else {Toast.makeText(MainActivity.this, "没有啦", Toast.LENGTH_SHORT).show();}} else {// 显示信息:菜的名称if(mCurrentIndex < mFoodResult.size()){Toast.makeText(MainActivity.this, "菜名: " + mFoodResult.get(mCurrentIndex).getName(), Toast.LENGTH_SHORT).show();} else {Toast.makeText(MainActivity.this, "没有啦", Toast.LENGTH_SHORT).show();}}}});}// 查找菜品private void search() {// 结果列表每次都清空// 遍历所有菜// 如果符合条件,则加入到我们的结果列表中// 如果为空,先初始化if(mFoodResult == null){mFoodResult = new ArrayList<>();}// 先清除之前的结果mFoodResult.clear();// 当前显示的是结果中的第几个菜mCurrentIndex = 0;for (int index = 0; index < mFoods.size(); index++) {Food food = mFoods.get(index);if(food != null){// 价格要小于设定的价格// 是顾客选择的口味if(food.getPrice() < mPrice &&(food.isHot() ==  mIsHot|| food.isFish() == mIsFish|| food.isSour() == mIsFish)){mFoodResult.add(food);}}}// 先显示第一张图片if(mCurrentIndex < mFoodResult.size()){mFoodImageView.setImageResource(mFoodResult.get(mCurrentIndex).getPic());} else {mFoodImageView.setImageResource(R.drawable.ic_launcher_foreground);}}}

Android中的基础控件CheckBox、RadioButton、ToggleButton、SeekBar相关推荐

  1. Android中的基础控件TextView、Button、ImageView、EditText、ProgressBar

    文章目录 1 Android中的基础控件 1.1 控件的通用属性 2 TextView 2.1 TextView的继承关系 2.2 TextView的常用属性 3 EditText 3.1 常用属性 ...

  2. 从零开始学android:Android中的基本控件(上)

    从零开始学android:Android中的基本控件(上) 本章内容较多,下面只贴代码,大家只需要贴到自己eclipse里就知道作用^^! View组件简介 Android中的View组件包含了几乎所 ...

  3. Android游戏开发系统控件-CheckBox

    Android游戏开发系统控件-CheckBox 2012/5/11 星期五 CheckBox是Android系统最普通的UI控件,继承了Button按钮 下面通过一个实例来学习 作者:wwj 功能: ...

  4. Android中进度条控件使用

    android中进度条控件使用 ProgressBar pb = findViewById(R.id.pb);pb.setMax(100);pb.setProgress(33); 转载于:https: ...

  5. android中互斥的控件,Android控件之Radiobutton与RadioGroup

    RadioButton 是一个单选控件,在一个RadioGroup中,各个RadioButton是互斥的 XML文件: xmlns:tools="http://schemas.android ...

  6. android 中的组合控件的设计

    在开发应用程序的时候,很多时候会使用到几个重复的控件,例如Android手机的设置界面里面的位置服务里面的每一栏都是组合控件,也就是说多个控件组成一个整体,如下图所示: 红色方框里面的是由两个Text ...

  7. android中翻页控件,Android GridView控件分页自定义

    上一篇:Android GridView控件自定义中,我们自定义了Android GridView控件. 包名解释: com.yaomei.activity.adapter   DEMO使用到的自定义 ...

  8. android 中使用TabHost控件实现微信界面的底部菜单效果

    首先,在布局文件中的代码如下:(菜单位于底部,需要在代码中设置) <TabHostandroid:id="@android:id/tabhost"android:layout ...

  9. Android中如何使控件保持固定宽高比

    目录 1.自定义view 2.adjustViewBounds 3.百分比布局 4.ConstraintLayout 我们在android开发过程中可能会遇到一种情况,一个组件需要保持固定的宽高比,但 ...

最新文章

  1. 20.Valid Parentheses (python)
  2. IdentityServer Topics(2)- 定义资源
  3. 【转】Struts2 和 Spring MVC对比
  4. 【Android 异步操作】AsyncTask 异步任务 ( AsyncTask 异步任务执行方法 execute 方法相关源码解析 )
  5. 【iOS】iOS 调试快速定位程序在哪崩溃
  6. iview的走马灯嵌套在模态框中,宽度为0的解决方案
  7. setpriority_Java Thread类的最终void setPriority(int priority)方法(带示例)
  8. 线程的特点 java 1615387415
  9. vs2017中配置openGL环境
  10. cookie与session以及他们的常规用途——web开发必须熟知的知识
  11. linux 卸载系统服务,Linux卸载系统自带的httpd的方法
  12. 圆通快递单号yt开头_乡镇快递取件二次收费,四川省消委会点名这些快递公司...
  13. 异常处理:java.lang.ClassNotFoundException: javax.xml.bind.JAXBContext
  14. esp8266 继电器接线图_基于ESP8266的温控继电器
  15. RecyclerView系列:GridLayoutManager的构造函数中的orientation理解
  16. 【K最近邻法(KNN)】的Python和R语言简单实现鸢尾花分类
  17. MBR15200FAC-ASEMI插件肖特基二极管MBR15200FAC
  18. Python之组合数据类型(列表、元组、集合、字典)
  19. Eclipse报错DataIntegrityViolationException异常解决办法
  20. 测试开发工程师成长日记011 - linux常用命令day03

热门文章

  1. java 抽象类 模板_Java抽象类的构造模板模式用法示例
  2. 【Matlab 控制】Simulink仿真+S函数例子
  3. Python 列表前加 *号
  4. 5.4 SVM的使用建议-机器学习笔记-斯坦福吴恩达教授
  5. 玩转Mixly – 2、Arduino AVR编程 之 输入输出
  6. STM32 电机教程 5 - 步进电机基础知识介绍
  7. 安装中文版man手册,同时保留原英文版手册
  8. MT6573驱动开发日志之touchpanel .
  9. 聊聊flink的logback配置
  10. Oracle 12c 安装(内附软件包)