Android SeekBar的使用

定义

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.

SeekBar是ProgressBar的扩展,它添加了可拖动的滑块。用户可以触摸拇指并向左或向右拖动以设置当前进度级别,或使用箭头键。不建议将可聚焦的小部件放置在SeekBar的左侧或右侧。

SeekBar的客户端可以附加一个SeekBar.OnSeekBarChangeListener,以通知用户操作。

主要属性

android:progress Defines the default progress value, between 0 and max.(定义默认进度值,在0到最大值之间)
android:max Defines the maximum value. (定义滑块最大值)
android:thumb Draws the thumb on a seekbar. (用于绘制滑块按下去时候的drawable)

SeekBar.OnSeekBarChangeListener

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 methods
abstract void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)Notification that the progress level has changed.通知进度值改变
abstract void onStartTrackingTouch(SeekBar seekBar)Notification that the user has started a touch gesture.用户开始触摸
abstract void onStopTrackingTouch(SeekBar seekBar)Notification that the user has finished a touch gesture.用户停止触摸

onProgressChanged

Notification that the progress level has changed. Clients can use the fromUser parameter to distinguish user-initiated changes from those that occurred programmatically.

通知进度值已更改。 客户端可以使用fromUser参数将用户启动的更改与以编程方式进行的更改区分开。

Parameters
seekBar SeekBar: The SeekBar whose progress has changed(SeekBar的progress(进度值)发生改变)
progress int: The current progress level. This will be in the range min…max where min and max were set by ProgressBar#setMin(int) and ProgressBar#setMax(int), respectively. (The default values for min is 0 and max is 100.)(当前进度值)
fromUser boolean: True if the progress change was initiated by the user.如果是用户更待的进度值则为true

onStartTrackingTouch

Added in API level 1

public abstract void onStartTrackingTouch (SeekBar seekBar)

Notification that the user has started a touch gesture. Clients may want to use this to disable advancing the seekbar.

通知用户开始触碰。可以使用它禁止seekbar前进。

Parameters
seekBar SeekBar: The SeekBar in which the touch gesture began.正在开始触摸的SeekBar

onStopTrackingTouch

Added in API level 1

public abstract void onStopTrackingTouch (SeekBar seekBar)

Notification that the user has finished a touch gesture. Clients may want to use this to re-enable advancing the seekbar.

通知用户解除触摸。可以使用它重启SeekBar

Parameters
seekBar SeekBar: The SeekBar in which the touch gesture began.刚刚触摸的seekBar

SeekBar的样式如下:

demo

UI

<LinearLayout    android:id="@+id/ratioSeekBarWrap"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center_vertical"    android:orientation="horizontal"    app:layout_constraintBottom_toBottomOf="parent"    app:layout_constraintTop_toTopOf="parent">    <View        android:layout_width="@dimen/colorHelper_square_length"        android:layout_height="@dimen/colorHelper_square_length"        android:background="@color/colorHelper_square_from_ratio_background" />    <SeekBar        android:id="@+id/ratioSeekBar"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:max="100"        android:paddingLeft="20dp"        android:paddingRight="20dp" />    <View        android:layout_width="@dimen/colorHelper_square_length"        android:layout_height="@dimen/colorHelper_square_length"        android:background="@color/colorHelper_square_to_ratio_background" />LinearLayout>

SeekBar监听

private void init(){      // 设置SeekBar监听事件      mRatioSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){         @Override         public void onProgressChanged(SeekBar seekBar, int i, boolean b) {            // 进度条改变时            Log.e("now:",String.valueOf(i));         }         @Override         public void onStartTrackingTouch(SeekBar seekBar) {            // 刚刚触碰进度条            Toast.makeText(getContext(),"当前的进度:" + seekBar.getProgress(),Toast.LENGTH_LONG).show();//          Toast.makeText(getActivity(),"当前的进度:" + seekBar.getProgress(),Toast.LENGTH_LONG).show();         }         @Override         public void onStopTrackingTouch(SeekBar seekBar) {            // 松开进度条            int progress = seekBar.getProgress();            Toast.makeText(getContext(),"当前的进度:" + seekBar.getProgress(),Toast.LENGTH_LONG).show();         }      });      mRatioSeekBar.setProgress(50);   }

demo地址:

https://github.com/luoziling/RatioSeekBarTest/tree/master

更多相关文章

  1. android第一天
  2. Android关于获取用户是否允许某项权限
  3. android 网络异步加载数据进度条
  4. Android用户界面设计
  5. Android淘宝好评星级进度条RatingBar原来可以这么玩
  6. Activity配置属性
  7. 进度条背景的改变,界面更美观
  8. Android唯一标识
  9. android的进度条使用

随机推荐

  1. Android手动检查并申请权限
  2. Android 自定义标题
  3. 使用JEB调试Android App
  4. android解决无法设定listview的item高度
  5. 关于图片保存(Android)
  6. Android二级列表
  7. Android VideoView状态跟踪和错误处理
  8. Android处理XML的三种方式
  9. 即时通信之Bmob开发03
  10. 【读书笔记《Android游戏编程之从零开始