Android SeekBar的使用

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:progressDefines the default progress value, between 0 and max.(定义默认进度值,在0到最大值之间)
android:maxDefines the maximum value. (定义滑块最大值)
android:thumbDraws 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 voidonProgressChanged(SeekBar seekBar, int progress, boolean fromUser)Notification that the progress level has changed.通知进度值改变
abstract voidonStartTrackingTouch(SeekBar seekBar)Notification that the user has started a touch gesture.用户开始触摸
abstract voidonStopTrackingTouch(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
seekBarSeekBar: The SeekBar whose progress has changed(SeekBar的progress(进度值)发生改变)
progressint: 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.)(当前进度值)
fromUserboolean: 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
seekBarSeekBar: 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
seekBarSeekBar: 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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值