Android中的基础控件CheckBox、RadioButton、ToggleButton、SeekBar
生活随笔
收集整理的這篇文章主要介紹了
Android中的基础控件CheckBox、RadioButton、ToggleButton、SeekBar
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 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文件:
下面看下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區別:
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android中的约束布局
- 下一篇: 相互宝审核不通过会通知吗