Android 使用 Java 更改 Seekbar Thumb 会导致 Thumb 位置重置

发布于 2024-12-08 14:06:13 字数 1633 浏览 3 评论 0原文

我正在尝试创建一个带有拇指的搜索栏,当用户按下它时,它会改变颜色,当用户松开时,它会变回来。我设法使用设置拇指更改拇指可绘制。但是,我还必须在 java 中设置可绘制边界,以使其在更改后显示。现在,当用户放开滑块并恢复到其原始可绘制对象时,它将可绘制对象重置为零位置并将进度保留在应有的位置。我曾尝试在可绘制对象更改后使用全局变量来重置搜索栏进度,但它似乎不起作用。如果我使用静态整数来重置进度,它就会按照我想要的方式工作。

对 Android 有点陌生,我不知道我做错了什么。

这是在触摸时更改可绘制对象的代码。

private void Seek_Height_Click() {
    Drawable myThumb = getResources().getDrawable(R.drawable.slider_button_click);
    myThumb.setBounds(new Rect(0, 0, myThumb.getIntrinsicWidth(),myThumb.getIntrinsicHeight()));
    Height_of_Target_skbr.setThumb(myThumb);
    Height_of_Target_skbr.setThumbOffset(-1);
}

这是将拇指设置回原始可绘制对象的代码。

private void Seek_Height_UnClick() {
    Drawable myThumb = getResources().getDrawable(R.drawable.slider_button);
    myThumb.setBounds(new Rect(0, 0,   myThumb.getIntrinsicWidth(),myThumb.getIntrinsicHeight()));
    Height_of_Target_skbr.setThumb(myThumb);
    Height_of_Target_skbr.setThumbOffset(-1);
    Height_of_Target_skbr.setProgress(Height_Prog_int);
}

这是调用上述方法的代码。

this.Height_of_Target_skbr.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

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

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            returnHeight_skbr();
            Seek_Height_Click();
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            returnHeight_skbr();
        }
    });

I am trying to create a seekbar with a thumb that changes color when it is pressed by the user and change back when the user lets go. I managed to change the thumb drawable using set thumb. However, I also had to set the drawable boundries in java to get it to appear after the change. Now when the user lets go of the slider and it reverts to its original drawable it resets the drawable to the zero position and leaves the progress where it should be. I have tried using a global variable to reset the seekbar progress after the drawable is changed, but it doesn't seems to work. If I use a static integer to reset the progress it works the way I want it to.

Kinda new to android and I don't know what I am doing wrong.

This is the code that changes the drawable when it is touched.

private void Seek_Height_Click() {
    Drawable myThumb = getResources().getDrawable(R.drawable.slider_button_click);
    myThumb.setBounds(new Rect(0, 0, myThumb.getIntrinsicWidth(),myThumb.getIntrinsicHeight()));
    Height_of_Target_skbr.setThumb(myThumb);
    Height_of_Target_skbr.setThumbOffset(-1);
}

This is the code to set the thumb back to the original drawable.

private void Seek_Height_UnClick() {
    Drawable myThumb = getResources().getDrawable(R.drawable.slider_button);
    myThumb.setBounds(new Rect(0, 0,   myThumb.getIntrinsicWidth(),myThumb.getIntrinsicHeight()));
    Height_of_Target_skbr.setThumb(myThumb);
    Height_of_Target_skbr.setThumbOffset(-1);
    Height_of_Target_skbr.setProgress(Height_Prog_int);
}

This is the code that calls the above method.

this.Height_of_Target_skbr.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

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

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            returnHeight_skbr();
            Seek_Height_Click();
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            returnHeight_skbr();
        }
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福不弃 2024-12-15 14:06:13

我可以通过在可绘制文件夹中创建样式,然后将拇指可绘制设置为该样式来完成这项工作。

我参考了本教程来完成这项工作。
http://www.youtube.com/watch?v=zXXCFmfJMNw

拇指样式代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item 
    android:state_pressed="true"
    android:drawable="@drawable/slider_button_click">
</item>
<item 
    android:drawable="@drawable/slider_button">
</item>
</selector>

I was able to make this work using by creating a style in the drawable folder and then setting the thumb drawable to that style.

I refereed to this tutorial to make this work.
http://www.youtube.com/watch?v=zXXCFmfJMNw

Code for Thumb Style

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item 
    android:state_pressed="true"
    android:drawable="@drawable/slider_button_click">
</item>
<item 
    android:drawable="@drawable/slider_button">
</item>
</selector>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文