android Seekbar沿着seekbar显示进度值

编程入门 行业动态 更新时间:2024-10-24 08:27:19
本文介绍了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显示进度值

本文发布于:2023-10-08 02:44:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471278.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度   android   Seekbar   seekbar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!