android Seekbar沿着seekbar显示进度值 [英] android Seekbar show progress value along the seekbar

查看:94
本文介绍了android Seekbar沿着seekbar显示进度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 seekbar listview ,每个 seekbar 代表100%的产品价值.通过移动 seekbar ,可以更改%的值,我想在用户拖动 seekbar 时沿搜索栏显示值的变化.我不希望添加另一个用于更新值的文本框.

I have a listview with seekbar, each seekbar represents 100% of a value of product. By moving the seekbar the % value can be changed, I want to show the change in value along the seekbar while the user is dragging the seekbar. I do not wish to add another text box where the value is updated.

我更多地是在寻找栏指针上方显示一个小视图的选项,它会显示在 onStartTracking 上,并消失在 onStopTracking 事件上.

I am more looking at options to show a small view on top of the seekbar pointer, which appears onStartTracking and disappears onStopTracking event.

有没有办法做到这一点?

Is there a way to achieve this?

推荐答案

这是一种简单的方法,

  • 在XML中添加 textView
  • 通过 seekBar
  • 的移动来更改其位置
  • 这将使用 setX()
  • 对其进行定位
  • 如果需要,还可以为其位置提供 setY()
  • 如果要隐藏,请编写一些逻辑,并在需要时设置 textView 可见性.
  • Add a textView in your XML
  • Change it's position with the movement of the seekBar
  • That will position it using setX()
  • If you want you can give setY() for its Y position as well
  • If you want to hide write some logic and make textView Visibility GONE when you need.

示例:

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                    int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
                    textView.setText("" + progress);
                    textView.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
                    //textView.setY(100); just added a value set this properly using screen with height aspect ratio , if you do not set it by default it will be there below seek bar 

                }


,或者您可以将 Paint 与自定义的 SeekBar 类一起使用,而无需任何 TextView


or you can use Paint with your custom SeekBar class without any TextView

创建一个类

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.SeekBar;


public class MySeekBar extends SeekBar {
    public MySeekBar (Context context) {
        super(context);
    }

    public MySeekBar (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MySeekBar (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas c) {
        super.onDraw(c);
        int thumb_x = (int) (( (double)this.getProgress()/this.getMax() ) * (double)this.getWidth());
        float middle = (float) (this.getHeight());

        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(20);
        c.drawText(""+this.getProgress(), thumb_x, middle, paint);
    }
}

在XML中设置/创建您的 SeekBar

Set/create your SeekBar in your XML

  <yourPackageName.MySeekBar
            android:id="@+id/my_seek_bar"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="26dp"
            android:max="10"/>

现在从您的活动中

seekBar = (MySeekBar) findViewById(R.id.my_seek_bar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
});

这篇关于android Seekbar沿着seekbar显示进度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆