How to disable button click?

后端 未结 5 1176
滥情空心
滥情空心 2020-12-28 08:35

In my android application, there are number of images in drawable folder. In my layout there are two buttons: Back and Next buttons. On clicking next and back buttons 2 diff

相关标签:
5条回答
  • 2020-12-28 08:56
    case R.id.next:
            Log.i("Tag","tag");
            if(imageCounter < imageList.length)
            {
                imageCounter++;
                imagePath = imageList[imageCounter];
                if (imageCounter==(imageList.length)-1)
                {
                    ImageButton next=(ImageButton)findViewBYId(R.id.next);
                    next.setEnabled(false);
                }
            }
            break;
        case R.id.back:
            if(imageCounter > 0)
            {
                imageCounter--;
                imagePath = imageList[imageCounter];
                if (imageCounter==0)
                {
                    ImageButton back=(ImageButton)findViewBYId(r.id.back);
                    back.setEnabled(false);
                }
            }
            break;
    
    0 讨论(0)
  • 2020-12-28 09:01

    With kotlin you can disable a button with

    button.isEnabled = false
    

    or to disable clicking on it

    button.isClickable = false
    
    0 讨论(0)
  • 2020-12-28 09:04
    next.setClickable(false);
    
    0 讨论(0)
  • 2020-12-28 09:06

    more preferred solution is,

    onclick(){
      btn.setEnabled(false);
      btn.setClickable(false);
      //yourwork
      myWork();
    }
    
    myWork(){
     //your tasks.
     btn.setEnabled(true);
     btn.setClickable(true);
    }
    

    As a link can be ignored easily, I had to repeat this again and again

    0 讨论(0)
  • just insert two extra images in your drawable folder one for disabled right arrow and one for disabled left arrow.

    now try this

             case R.id.next:
    
             Log.i("Tag","tag");
    
                if(imageCounter < imageList.length)
    
    
                {
                    imageCounter++;
    
                    imagePath = imageList[imageCounter];
    
                    if (imageCounter==(imageList.length)-1)
    
                    {
    
    
                          //disabling right button by changing image from following code                                                                                                                
    
                          next.setImageDrawable(getResources().getDrawable(R.drawable.right_disabled));
    
                    }
                }
                break;
            case R.id.back:
    
                if(imageCounter > 0)
    
                {
                    imageCounter--;
    
                    imagePath = imageList[imageCounter];
    
                    if (imageCounter==0)
    
                    {
                          back.setImageDrawable(getResources().getDrawable(R.drawable.left_disabled));
                    }
                }
                break;
    
    0 讨论(0)
提交回复
热议问题