OnClickListener不从父类射击 [英] OnClickListener not firing from Parent class

查看:180
本文介绍了OnClickListener不从父类射击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个后续行动这个问题:<一href="http://stackoverflow.com/questions/3488880/group-of-views-controls-on-multiple-screens">http://stackoverflow.com/questions/3488880/group-of-views-controls-on-multiple-screens

我创建了一个父类,继承自它的子类。当我在子类中设置OnClickListener,当点击事件触发的按钮。当我将设置OnClickListener父类,则不会触发事件。我一定是在缺少明显的东西,但我只是没有看到它。

谢谢, 卡梅伦。

父类:

 公共类NavigationMenu扩展活动
{
    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.nav_bar);

        按钮loginButton =(按钮)findViewById(R.id.navbtnHome);
        loginButton.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图查看)
            {
                Toast.makeText(getApplicationContext(),卡梅伦,我在这里,Toast.LENGTH_SHORT).show();
                意图I =新的意图(NavigationMenu.this,Login.class);
                startActivity(ⅰ);
            }
        });
    }
}
 

子类:

 公共类设置来延长NavigationMenu
{
    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.settings);
    }
}
 

解决方案

现在的问题是,你的设置类的setContentView将重置由你的父类的onCreate()创建的布局。因此,回调也是如此。

要解决的是,我会建议你在nav_bar布局中添加的LinearLayout ID为容器。

您然后离开NavigationMenu类不变,并在你的子类,你只需要插入您查看该容器中。这应该工作:

 公共类设置来延长NavigationMenu
    {
        @覆盖
        保护无效的onCreate(包savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            的LinearLayout容器=(的LinearLayout)findViewById(R.id.container);
            查看settingsView = getLayoutInflater()膨胀(R.layout.settings,空)。
            container.addView(settingsView);
        }
    }
 

现在,一个更清洁的方式来做到这一点会迫使你NavigationMenu的子类,提供的东西放在容器中。

您可以实现通过添加一个抽象方法NavigationMenu,要求孩子们创建视图。

 公共抽象类NavigationMenu扩展活动
{
    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.nav_bar);

        按钮loginButton =(按钮)findViewById(R.id.navbtnHome);
        loginButton.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图查看)
            {
                Toast.makeText(getApplicationContext(),卡梅伦,我在这里,Toast.LENGTH_SHORT).show();
                意图I =新的意图(NavigationMenu.this,Login.class);
                startActivity(ⅰ);
            }
        });

        的LinearLayout容器=(的LinearLayout)findViewById(R.id.container);
        container.addView(CreateView的());
    }

    受保护的抽象的视图CreateView的();
}
 

而在你的子类,你只需要执行CreateView的():

 公共类设置来延长NavigationMenu
    {
        @覆盖
        受保护的视图CreateView的()
        {
            的LinearLayout容器=(的LinearLayout)findViewById(R.id.container);
            返回getLayoutInflater()膨胀(R.layout.settings,空)。
        }
    }
 

这样做的好处是,如果你改变你的布局(重命名你的容器为例),你必须只修改父类。

A Follow up to this question: http://stackoverflow.com/questions/3488880/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.

这篇关于OnClickListener不从父类射击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆