findViewById inside a Static Method

走远了吗. 提交于 2019-12-23 08:05:10

问题


I have this static method:

public static void displayLevelUp(int level, Context context) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.custom_level_coast,
            (ViewGroup) findViewById(R.id.toast_layout_root));  // this row

    TextView text = (TextView) layout.findViewById(R.id.toastText);
    text.setText("This is a custom toast");

    Toast toast = new Toast(context);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();

    Toast.makeText(context, String.valueOf(level), Toast.LENGTH_SHORT)
            .show();

}

However, I can't figure out how to get the firstfindViewById to play nice with this as it says it is a non-static method. I understand why it says that, but there must be a workaround? I passed context into this method but couldn't work them out together.


回答1:


One thing you can do is make the view a class wide variable and use that. I dont actually recommend doing that but it will work if you need something quick and dirty.

Passing in the view as a parameter would be the preferred way




回答2:


it is a little weird. But you can pass the root view as parameter.

//some method...
ViewGroup root = (ViewGroup) findViewById(R.id.toast_layout_root);
displayLevelUp(level, context, root);
//some method end...


public void displayLevelUp(int level, Context context, ViewGroup root) {

LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View layout = inflater.inflate(R.layout.custom_level_coast,
        root);

TextView text = (TextView) layout.findViewById(R.id.toastText);
text.setText("This is a custom toast");

Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

Toast.makeText(context, String.valueOf(level), Toast.LENGTH_SHORT)
        .show();

}



回答3:


If you want to stick with a static method use Activity instead of Context as parameter and do a activity.findViewById like so:

public static void displayLevelUp(int level, Activity activity) {
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.toastText, (ViewGroup) activity.findViewById(R.id.abs__action_bar_container));  // this row

Another way to do it is to pass the parent ViewGroup as parameter instead of a Context or Activity:

public static void displayLevelUp(int level, ViewGroup rootLayout) {
    View layout = rootLayout.inflate(rootLayout.getContext(), R.layout.custom_level_coast, rootLayout.findViewById(R.id.toast_layout_root));  // this row


来源:https://stackoverflow.com/questions/16342004/findviewbyid-inside-a-static-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!