+ Reply to Thread
Results 1 to 2 of 2

Thread: Boolean value is always set to true

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    40

    Boolean value is always set to true

    I have that code :
    Code:
     b1.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                boolean b = true;
                if(b == true){
                    tv1.setText("true");
                    b2.setEnabled(false);
                    b = false;
    
                } else
    
                    if(b == false){
                    b2.setEnabled(true);
                    b = true;
                    tv1.setText("false");
    But whenever i click the button the boolean is always true in the first place.

  2. #2
    You always get the Boolean value true because whenever you click the button you srt b to true and to avoid that , then you need to get the Boolean value away from the onclick method and you will need to apply that code :
    Code:
    b1.setOnClickListener(new View.OnClickListener() {
    
        boolean b = true;
    
        public void onClick(View arg0) {
            if (b) {
                tv1.setText("true");
                b2.setEnabled(false);
                b = false;
    
            } else {
                b2.setEnabled(true);
                b = true;
                tv1.setText("false");
            }
        }
    
    });

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts