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

相关文章

QT移植到RK3568开发板的方法步骤

《QT移植到RK3568开发板的方法步骤》本文主要介绍了QT移植到RK3568开发板的方法步骤,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录前言一、获取SDK1. 安装依赖2. 获取SDK资源包3. SDK工程目录介绍4. 获取补丁包二

Java反转字符串的五种方法总结

《Java反转字符串的五种方法总结》:本文主要介绍五种在Java中反转字符串的方法,包括使用StringBuilder的reverse()方法、字符数组、自定义StringBuilder方法、直接... 目录前言方法一:使用StringBuilder的reverse()方法方法二:使用字符数组方法三:使用自

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

使用mvn deploy命令上传jar包的实现

《使用mvndeploy命令上传jar包的实现》本文介绍了使用mvndeploy:deploy-file命令将本地仓库中的JAR包重新发布到Maven私服,文中通过示例代码介绍的非常详细,对大家的学... 目录一、背景二、环境三、配置nexus上传账号四、执行deploy命令上传包1. 首先需要把本地仓中要

Spring Cloud之注册中心Nacos的使用详解

《SpringCloud之注册中心Nacos的使用详解》本文介绍SpringCloudAlibaba中的Nacos组件,对比了Nacos与Eureka的区别,展示了如何在项目中引入SpringClo... 目录Naacos服务注册/服务发现引⼊Spring Cloud Alibaba依赖引入Naco编程s依

java导出pdf文件的详细实现方法

《java导出pdf文件的详细实现方法》:本文主要介绍java导出pdf文件的详细实现方法,包括制作模板、获取中文字体文件、实现后端服务以及前端发起请求并生成下载链接,需要的朋友可以参考下... 目录使用注意点包含内容1、制作pdf模板2、获取pdf导出中文需要的文件3、实现4、前端发起请求并生成下载链接使

Java springBoot初步使用websocket的代码示例

《JavaspringBoot初步使用websocket的代码示例》:本文主要介绍JavaspringBoot初步使用websocket的相关资料,WebSocket是一种实现实时双向通信的协... 目录一、什么是websocket二、依赖坐标地址1.springBoot父级依赖2.springBoot依赖

Java使用Mail构建邮件功能的完整指南

《Java使用Mail构建邮件功能的完整指南》JavaMailAPI是一个功能强大的工具,它可以帮助开发者轻松实现邮件的发送与接收功能,本文将介绍如何使用JavaMail发送和接收邮件,希望对大家有所... 目录1、简述2、主要特点3、发送样例3.1 发送纯文本邮件3.2 发送 html 邮件3.3 发送带

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D