SeekBar使用方法

2024-09-04 22:18
文章标签 使用 方法 seekbar

本文主要是介绍SeekBar使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概述

本文讨论SeekBar的使用方法。

在http://blog.csdn.net/a_flying_bird/article/details/50948916一文讨论了Handler的使用方法,这是本文的基础。本文使用同样的示例,即一个计算任务。

Class Overview

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.

Clients of the SeekBar can attach a SeekBar.OnSeekBarChangeListener to be notified of the user’s actions.

示例

采用和Handler使用方法一样的例子,只是把TextView换成了SeekBar。

效果

seekbar

my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/start"android:layout_width="192dp"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="@string/start" /><SeekBarandroid:id="@+id/seekBar"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>

字符串

<string name="start">Start</string>

代码

省略掉自动生成的代码

public class MainActivity extends Activity {protected static final String TAG = "MainActivity";private Button button = null;private SeekBar seekBar = null;private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {int count = msg.what;seekBar.setProgress(count);}};private Runnable adding = new Runnable() {@Overridepublic void run() {double d;for (int count = 1; count <= 1000; count++) {for (int i = 0; i < 1000; i++) {d = Math.sqrt(Math.sqrt(i));Log.d(TAG, "count = " + count + ", sqrt(sqrt(" + i + "))=" + d);}if (count % 10 == 0) {handler.sendEmptyMessage(count / 100);}}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_layout);button = (Button)findViewById(R.id.start);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {calculate();}});seekBar = (SeekBar)findViewById(R.id.seekBar);seekBar.setMax(10);}protected void calculate() {Thread thread = new Thread(adding);thread.start();}

要点

如下:
- 初始化最大范围:void setMax(int max)
- 更新进度:void setProgress(int progress)

更多事件处理

SeekBar另一个常用的方法是:

/*** Sets a listener to receive notifications of changes to the SeekBar's progress level. Also* provides notifications of when the user starts and stops a touch gesture within the SeekBar.* * @param l The seek bar notification listener* * @see SeekBar.OnSeekBarChangeListener*/
public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {mOnSeekBarChangeListener = l;
}

其中用到的接口如下:

/*** A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch* the thumb and drag left or right to set the current progress level or use the arrow keys.* Placing focusable widgets to the left or right of a SeekBar is discouraged. * <p>* Clients of the SeekBar can attach a {@link SeekBar.OnSeekBarChangeListener} to* be notified of the user's actions.** @attr ref android.R.styleable#SeekBar_thumb*/
public class SeekBar extends AbsSeekBar {/*** A callback that notifies clients when the progress level has been* changed. This includes changes that were initiated by the user through a* touch gesture or arrow key/trackball as well as changes that were initiated* programmatically.*/public interface OnSeekBarChangeListener {/*** Notification that the progress level has changed. Clients can use the fromUser parameter* to distinguish user-initiated changes from those that occurred programmatically.* * @param seekBar The SeekBar whose progress has changed* @param progress The current progress level. This will be in the range 0..max where max*        was set by {@link ProgressBar#setMax(int)}. (The default value for max is 100.)* @param fromUser True if the progress change was initiated by the user.*/void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser);/*** Notification that the user has started a touch gesture. Clients may want to use this* to disable advancing the seekbar. * @param seekBar The SeekBar in which the touch gesture began*/void onStartTrackingTouch(SeekBar seekBar);/*** Notification that the user has finished a touch gesture. Clients may want to use this* to re-enable advancing the seekbar. * @param seekBar The SeekBar in which the touch gesture began*/void onStopTrackingTouch(SeekBar seekBar);}

示例:

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {Toast.makeText(MainActivity.this, "onProgressChanged()", Toast.LENGTH_SHORT).show();}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {Toast.makeText(MainActivity.this, "onStartTrackingTouch()", Toast.LENGTH_SHORT).show();}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {Toast.makeText(MainActivity.this, "onStopTrackingTouch()", Toast.LENGTH_SHORT).show(); }});

这篇关于SeekBar使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1137183

相关文章

MySQL分区表的具体使用

《MySQL分区表的具体使用》MySQL分区表通过规则将数据分至不同物理存储,提升管理与查询效率,本文主要介绍了MySQL分区表的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、分区的类型1. Range partition(范围分区)2. List partition(列表分区)3. H

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

Go语言中Recover机制的使用

《Go语言中Recover机制的使用》Go语言的recover机制通过defer函数捕获panic,实现异常恢复与程序稳定性,具有一定的参考价值,感兴趣的可以了解一下... 目录引言Recover 的基本概念基本代码示例简单的 Recover 示例嵌套函数中的 Recover项目场景中的应用Web 服务器中

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI

SpringBoot3中使用虚拟线程的完整步骤

《SpringBoot3中使用虚拟线程的完整步骤》在SpringBoot3中使用Java21+的虚拟线程(VirtualThreads)可以显著提升I/O密集型应用的并发能力,这篇文章为大家介绍了详细... 目录1. 环境准备2. 配置虚拟线程方式一:全局启用虚拟线程(Tomcat/Jetty)方式二:异步