安卓学习 之 SeekBar(音视频播放进度条)

SeekBar

1.使用场景

2.简单使用与事件监听

1.setProgress

2.setOnSeekBarChangeListener

3.注意事项

首先是拖拽一个SeekBar到视图区域中:

复制代码
android:max="100"       设置进度条的最大值为100
android:progress="40"   设置进度值为40(也就是圆点的位置到40%处)

看看这次学习的效果吧,用鼠标拖动进度条,日志中就会记录拖动开始,拖动中,拖动结束时的进度值或是做些其他的事情。

下面来看今天一上午学习的所有的代码吧:

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Check_Box_Activity"
    android:orientation="vertical">


    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="小花猫" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="小花狗" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="单选1" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="单选2" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="单选3" />
    </RadioGroup>

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton"
        android:textOn="按钮打开"
        android:textOff="按钮关闭"/>

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:max="100"
        android:progress="40"/>
</LinearLayout>

再来看Activity的代码吧:

java 复制代码
package com.example.android1;

import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class Check_Box_Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_check_box);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        CheckBox checkbox1 = findViewById(R.id.checkBox);   //获取复选框1的对象
        CheckBox checkbox2 = findViewById(R.id.checkBox2);  //获取复选框2的对象

        checkbox1.setChecked(false);  //复选框1设置为没有选中的状态
        checkbox2.setChecked(true);   //复选框2设置为选中的状态

        checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(@NonNull CompoundButton compoundButton, boolean b) {
                boolean ischecked1 = checkbox1.isChecked();
                Log.e("tag","复选框1的状态:"+ischecked1);
            }
        });

        checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(@NonNull CompoundButton compoundButton, boolean b) {
                boolean ischecked2 = checkbox2.isChecked();
                Log.e("tag","复选框2的状态:"+ischecked2);
            }
        });


        //*******************************   以下是SeekBar的内容   ************************************************
        SeekBar seekbar = findViewById(R.id.seekBar);   //找到SeekBar对象

        seekbar.setMax(200);           //设置进度条的最大值为200
        seekbar.setProgress(50);       //设置进度条的进度值为50

        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {     //监听进度条的值发生改变时的事件
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {           //当鼠标拖动进度条进度值发生改变时
                Log.e("tag","现在正在拖动进度条的进度值:"+i);          //日志打印进度条当前的进度值
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {       //进度条开始改变时的要做的事情
                Log.e("tag","现在开始拖动进度条的进度值:"+seekBar.getProgress());          //日志打印鼠标刚刚开始拖动进度条事的进度值
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {     //进度条改变停止时要做的事情
                Log.e("tag","现在结束拖动进度条的进度值:"+seekBar.getProgress());          //日志打印鼠标拖动进度条结束时的进度值
            }
        });

    }
}
相关推荐
en.en..5 小时前
Linux mmap 内存映射深度解析:基于帧缓冲 /dev/fb0
java·服务器·前端
洋不写bug5 小时前
链表补充练习,双链表的模拟实现
java·数据结构·链表·双链表·底层实现
后台模板学习5 小时前
用 IM 即时聊天项目一次讲清消息去重算法的踩坑与解决方案
java·数据库·spring
秋名RG5 小时前
2026/6/15 系统故障复盘与整改方案
java·架构
今天要早睡_5 小时前
C++ 核心语法速过:命名空间、引用、函数重载与 nullptr 深度解析
android·java·c++
南讯股份Nascent5 小时前
2026年CRM技术选型观察:当“数据打通“成为第一需求,电商CRM的架构价值在哪里
java·大数据·用户运营
心易行者6 小时前
Python自动化测试7步落地法:用python在线运行省掉90%环境配置时间
java·开发语言·人工智能·python·log4j·ai编程
晨欣6 小时前
NVIDIA GPU 架构演进学习笔记(GPT-5.6 Terra 生成)
笔记·学习·gpu·显卡·nvidia·英伟达
啦啦啦~~~2226 小时前
PC端+安卓端阅读器推荐!开源本地小说阅读器软件,
android·论文阅读·windows·开源软件·福昕阅读器
小雪崩6 小时前
嵌入式学习 day40:数据库
数据库·学习