简体   繁体   English

设置Int的最小值和最大值? Android的

[英]Set Min and Max Value for Int? Android

Is there a way to set minimum and maximum value of an integer? 有没有办法设置一个整数的最小值和最大值? I did some research but I couldn't find anything useful. 我做了一些研究,但找不到任何有用的东西。 I need to set min value 0 and max value 30, so when user is touching button(TouchListener) numbers will go from 30 to 0. I know it can be done with if else statements but I am looking for a better way. 我需要将最小值设置为0,将最大值设置为30,因此当用户触摸button(TouchListener)时,数字将从30变为0。我知道可以使用if语句来完成此操作,但我正在寻找一种更好的方法。

     int total= Integer.MAX_VALUE + 30;
     int left= Integer.MIN_VALUE + 0;

        total= 30;
        left = 30;
            .....
    onTouchListener...
    switch (action) {
                case MotionEvent.ACTION_DOWN:

    left--;
    setText();
break;
    ...
protected void setText() {
    count.setText(left + "/" + total);
}

But the problem is, int value goes under 0. -1 -2 etc etc How can I stop this? 但是问题是,int值小于0。-1 -2等等等如何停止呢? How to set Min int value? 如何设置最小整数值?

Replace left--; left--; with the following: 具有以下内容:

left -= (left > 0 ? 1 : 0);

Try to check left value before decrees by 1 : 尝试在减1之前检查左值:

if(left < 0){
   left--;
   setText();
}

You can use the range seek bar which allow you to seek between two specific integer values. 您可以使用范围查找栏,它允许您在两个特定的整数值之间进行查找。

Here is the link - https://code.google.com/p/range-seek-bar/ 这是链接-https://code.google.com/p/range-seek-bar/

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

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