2015-09-27 78 views
0

如果我在onClick()方法中調用了方法,但是如果我在onClick()方法中調用它兩次,它將不起作用。Android更改按鈕後的可見性onClick

private void changeVisible() { 

     if(progressBar.getVisibility() == View.VISIBLE && loginButton.getVisibility() == View.GONE) { 
      progressBar.setVisibility(View.GONE); 
      loginButton.setVisibility(View.VISIBLE); 
      //Toast.makeText(this, "Button visible", Toast.LENGTH_LONG).show(); 
     } 
     else { 
      progressBar.setVisibility(View.VISIBLE); 
      loginButton.setVisibility(View.GONE); 
      //Toast.makeText(this, "Button invisible", Toast.LENGTH_LONG).show(); 
     } 
    } 

使用案例:

@Override 
public void onClick(View v) { 
    if (v.getId() == R.id.loginButton) { 
     changeVisible(); 
     ... 
     try { 
      ... 
      if(...) { 
       ... 
      } 
      else { 
       ... 
       changeVisible(); 
      } 
     } 
     catch(Exception e) { 
      ... 
      changeVisible(); 
     } 
    } 
} 

請幫助。第一

+0

解決。 http://stackoverflow.com/questions/13090630/android-view-set-to-visible-and-gone-in-the-same-onclick-method-view-never-show – SBrx

回答

0

聲明查看Globbaly

Progressbar pb; 
    Button button; 

在您的onCreate()

pb=(ProgressBar) findViewById(R.id.progressBar)) 
    button = (Button) findViewById(R.id.loginButton); 

方法

private void changeVisible() { 
     if(pb.getVisibility()==View.VISIBLE && button.getVisiblity()==View.GONE) 
     { 
     pb.setVisibility(View.GONE); 
     button.setVisibility(View.VISIBLE); 
     } 
     else 
     { 
     pb.setVisibility(View.VISIBLE); 
     button.setVisibility(View.GONE) 
     } 
    } 
+0

嗨。我嘗試過,但不起作用 – SBrx