简体   繁体   English

Android 搜索栏值更改按钮

[英]Android seekbar value change button

project's problem Image:项目的问题图片:

项目问题 图片

Hi.你好。 I want to know about this problem with these two buttons.我想知道这两个按钮的这个问题。 First of all, I emphasized two buttons.首先,我强调了两个按钮。 Left button is Decrease button and right button is Increase button.左边的按钮是减少按钮,右边的按钮是增加按钮。 And the problem is... If all values are the maximum value.问题是......如果所有值都是最大值。 Clicking the Increase button does not change the value.单击“增加”按钮不会更改该值。 However, if I click the increase button two or three times and then click the decrease button to decrease the value again, the value does not change.但是,如果我单击增加按钮两三次,然后单击减少按钮再次减小该值,则该值不会改变。 To decrease the value, I must press the decrease button as much as I press the increase button.要减少该值,我必须按减少按钮的次数与按增加按钮的次数相同。 :( :(

When I first encountered this problem, I thought it was caused by the overlapping call of OnClickListener .刚开始遇到这个问题的时候,还以为是OnClickListener的重叠调用造成的。 So I tried to avoid calling in duplicate if the value was the maximum, but I couldn't solve the problem.所以我尽量避免在值为最大值的情况下重复调用,但我无法解决问题。 I ask questions because I've been looking for people who have had the same problem with me for about a week but haven't found a solution.我提出问题是因为我一直在寻找与我有同样问题的人大约一个星期但没有找到解决方案。 Please.请。 I want to go home.我想回家。

Please use the below code for updating the seek bar on button click.请使用以下代码更新按钮单击时的搜索栏。

SeekBar simpleSeekBar= new SeekBar(this);

        // Increase button Click {100 is the max value of SeekBar }
        if (simpleSeekBar.getMax() == 100){
            simpleSeekBar.setProgress(simpleSeekBar.getProgress() + 10);
        }

        //  Decrease button
        if (simpleSeekBar.getMax() <= 0){
            simpleSeekBar.setProgress(simpleSeekBar.getProgress() - 10);
        }

I hope it's helpful to you.我希望它对你有帮助。

Kotlin科特林

Your logic should be like this for increaseButton对于增加按钮,您的逻辑应该是这样的

        increaseButton.setOnClickListener {
        if(seekBar.progress > MAX_LIMIT) {
            seekBar.progress = MAX_LIMIT
            return@setOnClickListener
        }
        if (seekBar.progress == MAX_LIMIT) {
            // you can show notification that you are reach limit
            return@setOnClickListener
        }
        seekBar.progress = seekBar.progress + 1
    }

And same logic for decreaseButton和减少按钮的相同逻辑

        decreaseButton.setOnClickListener {
        if(seekBar.progress < MIN_LIMIT) {
            seekBar.progress = MIN_LIMIT
            return@setOnClickListener
        }
        if (seekBar.progress == MIN_LIMIT) {
            // you can show notification that you are reach limit
            return@setOnClickListener
        }
        seekBar.progress = seekBar.progress - 1
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM