2015-06-11 85 views
-5

我有4個EditText,我在XML中設置爲invisible,當單擊button時,我希望它們可以成對顯示。例如,當單擊button的時候,我想et1et2可見,那麼當按鈕被再次點擊eet1eet2可見。而當所有的人都看到,我想TextView可見。設置EditText的可見度

public class app extends ActionBarActivity { 
EditText et1; 
EditText et2; 
EditText eet1; 
EditText eet2; 
TextView sum; 
Button button; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_app); 
    et1 = (EditText)findViewById(R.id.et1); 
    et2 = (EditText)findViewById(R.id.et2); 
    eet1 = (EditText)findViewById(R.id.eet1); 
    eet2 = (EditText)findViewById(R.id.eet2); 
    sum = (TextView)findViewById(R.id.sum); 
    button = (Button)findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      onClick(); 
     } 
    }); 
} 

public void onClick() { 

     et1.setVisibility(View.VISIBLE); 
     eT1.setVisibility(View.VISIBLE); 

     eet1.setVisibility(View.VISIBLE); 
     eet2.setVisibility(View.VISIBLE); 

    } 
} 

回答

0

設置一個變量,你儲存多少次,你點擊的按鈕:

public class app extends ActionBarActivity { 
    int counter; 
    ... 

然後在的onClick您計數器加一,並區分情況:

public void onClick() { 
    switch(counter): 
     case 0: 
      et1.setVisibility(View.VISIBLE); 
      ... 
      break; 
     case 1: 
      ... 
      break; 
     case ... 
    } 
    counter++; 
} 
+0

謝謝非常。這是工作。 @mrtn – jj7

0
public void onClick() { 
    if (if (et1.getVisibility() == View.INVISIBLE && 
      et2.getVisibility() == View.INVISIBLE)) { 
      et1.setVisibility(View.VISIBLE); 
      eT1.setVisibility(View.VISIBLE); 
    } else if() ///... you Get the idea 

}