How can I hide/show an element when a button is pr

2019-03-18 20:51发布

问题:

I'm trying to learn how to develop Android using the Eclipse IDE. What I'm trying to do right now is make a hidden TableLayout visible when a button is pressed. However, I have no idea as to what I need to put in the button's OnClick property.

Also, are there any tutorials online that could help me learn how to develop Android apps in Eclipse?

Thanks!

回答1:

well just take the reference of the TableLayout by using findViewById(int) in the onClickListener(). once you have the object of TableLayout, call setVisibility(View.VISIBLE)



回答2:

TableLayout tl = (TableLayout)findeViewById(R.id.yourtablelayout);

tl.setVisibility(View.VISIBLE);

Something like that within your onClick() method should do the trick.



回答3:

Try:

TableLayout table;
Button button;
table = (TableLayout) findViewById (R.id.tablelayout1);
button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        // View list = (View)findViewById(R.id.myviewId); 
        tbleview.setVisibility(View.INVISIBLE); 

    }
});

Hope this works.



回答4:

Try this in MainActivity class :

  TextView textview;

/* onClick method of show button */

  public void show(View view){
    textview.setVisibility(View.VISIBLE);

}

/* onClick method of hide button */

public void hide(View view){
    textview.setVisibility(View.INVISIBLE);
}

and try this in onCreate method :

    textview = (TextView) findViewById(R.id.textview);