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

相关文章

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

pdfmake生成pdf的使用

实际项目中有时会有根据填写的表单数据或者其他格式的数据,将数据自动填充到pdf文件中根据固定模板生成pdf文件的需求 文章目录 利用pdfmake生成pdf文件1.下载安装pdfmake第三方包2.封装生成pdf文件的共用配置3.生成pdf文件的文件模板内容4.调用方法生成pdf 利用pdfmake生成pdf文件 1.下载安装pdfmake第三方包 npm i pdfma

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

浅谈主机加固,六种有效的主机加固方法

在数字化时代,数据的价值不言而喻,但随之而来的安全威胁也日益严峻。从勒索病毒到内部泄露,企业的数据安全面临着前所未有的挑战。为了应对这些挑战,一种全新的主机加固解决方案应运而生。 MCK主机加固解决方案,采用先进的安全容器中间件技术,构建起一套内核级的纵深立体防护体系。这一体系突破了传统安全防护的局限,即使在管理员权限被恶意利用的情况下,也能确保服务器的安全稳定运行。 普适主机加固措施:

webm怎么转换成mp4?这几种方法超多人在用!

webm怎么转换成mp4?WebM作为一种新兴的视频编码格式,近年来逐渐进入大众视野,其背后承载着诸多优势,但同时也伴随着不容忽视的局限性,首要挑战在于其兼容性边界,尽管WebM已广泛适应于众多网站与软件平台,但在特定应用环境或老旧设备上,其兼容难题依旧凸显,为用户体验带来不便,再者,WebM格式的非普适性也体现在编辑流程上,由于它并非行业内的通用标准,编辑过程中可能会遭遇格式不兼容的障碍,导致操