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

相关文章

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下