简体   繁体   中英

android - accessing any button (or any widget) to set visibility dynamically

I've been searching everywhere on stackoverflow and I can't seem to find something to help this question: is it possible for me to access a button, textview...etc from mainActivity (or a custom class) from any layout I have? and the layout already has a class, but I want to set it invisible with out having to set each button's visibility to gone manually.

this does not do what I want...

View view;
LayoutInflater inflater = (LayoutInflater)   this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.myLayout, null);
Button btn;
btn = (Button) view.findViewWithTag(R.id.btnWithTag);
btn.setVisibility(View.GONE);

This is how I solved it, hope it helps you as well:

    public void CheckWidget(Button myBtn) {

        try {
            //Visibility
                    switch (4) {
                        case "0":
                            myBtn.setVisibility(View.VISIBLE);
                            break;
                        case "4":
                            myBtn.setVisibility(View.INVISIBLE);
                            break;
                        case "8":
                            myBtn.setVisibility(View.GONE);
                            break;
                        default:
                            myBtn.setVisibility(View.VISIBLE);
                            break;
                    }
                //Enabled
                    myBtn.setEnabled(true);
                }
            }
        } catch (Exception exce) {
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM