简体   繁体   中英

OnClickListener not firing from Parent class

A Follow up to this question: Group of Views (controls) on multiple screens

I have created a parent class and a child class that inherits from it. When I set the OnClickListener in the child class, the event fires when the button is clicked. When I move the set OnClickListener to the parent class, the event doesn't fire. I've got to be missing something obvious but I just don't see it.

Thanks, Cameron.

Parent Class:

public class NavigationMenu extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nav_bar);

        Button loginButton = (Button) findViewById(R.id.navbtnHome);
        loginButton.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View view) 
            {
                Toast.makeText(getApplicationContext(), "Cameron, Im here", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(NavigationMenu.this, Login.class);
                startActivity(i);
            }
        });
    }
}

Child Class:

public class Settings extends NavigationMenu 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
    }
}

The problem is that the setContentView of your Settings class will reset the layout that was created by your parent class' onCreate(). And therefore, the callbacks as well.

To work around that, I would suggest that you add a LinearLayout with id "container" in your nav_bar layout.

You then leave the NavigationMenu class untouched and in your child class, you just insert your view in the container. This should work :

  public class Settings extends NavigationMenu 
    {
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            LinearLayout container = (LinearLayout)findViewById(R.id.container);
            View settingsView = getLayoutInflater().inflate(R.layout.settings, null);
            container.addView(settingsView);
        }
    }

Now, a cleaner way to do that would be to force your NavigationMenu's child class to provide something to be put in the container.

You could achieve that by adding an abstract method to NavigationMenu that would require the children to create the view.

public abstract class NavigationMenu extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nav_bar);

        Button loginButton = (Button) findViewById(R.id.navbtnHome);
        loginButton.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View view) 
            {
                Toast.makeText(getApplicationContext(), "Cameron, Im here", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(NavigationMenu.this, Login.class);
                startActivity(i);
            }
        });

        LinearLayout container = (LinearLayout)findViewById(R.id.container);
        container.addView(createView());
    }

    protected abstract View createView();
}

And in your child class, you just have to implement createView () :

  public class Settings extends NavigationMenu 
    {
        @Override
        protected View createView()
        {
            LinearLayout container = (LinearLayout)findViewById(R.id.container);
            return getLayoutInflater().inflate(R.layout.settings, null);
        }
    }

This has the advantage that if you change your layout (rename your container for example), you would have to modify only the parent class.

您应该在孩子的setContentView之后调用super.onCreate,但是您必须变通,因为您的父级使用另一个视图调用setcontentview

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