【问题标题】:LinearLayout Listener not working线性布局监听器不工作
【发布时间】:2015-09-17 08:45:40
【问题描述】:

我有一个 LinearLayout,我以编程方式将按钮添加到 , 我试图在按下时删除一个按钮,但监听器不工作..

// ingAddLayout - the layout which the buttons are added to//
     String txt = "Dynamic Button"; 
        int idCounter = 0;  

            final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);

            params.setMargins(10, 20, 30, 20);
            AddBtn.setOnClickListener(new View.OnClickListener() 
                            {

                                @Override
                                public void onClick(View v) 
                                {
                                        Button btn = new Button(context);
                                        btn.setText(txt);
                                        btn.setBackgroundColor(getResources().getColor(R.color.white));
                                        btn.setId(idCounter);
                                        didCounter++;
                                        ingAddLayout.addView(btn,params);
                                    }


                                }

                            });

听者:

ingAddLayout.setOnClickListener(new View.OnClickListener() 
{

    @Override
    public void onClick(View v)  
    {               
        ingAddLayout.removeViewAt(idCounter-1);
    }
});

【问题讨论】:

  • 什么是ingAddLayout??
  • 我添加按钮的LinearLayout。
  • 您是否通过 findviewById() 添加了您的 LinearLayout id java??
  • yes.ingAddLayout = (LinearLayout)findViewById(R.id.dynamicIngsLayout);
  • 你需要在按下按钮时删除按钮,然后你在布局上应用 onclick ???

标签: android listener android-linearlayout


【解决方案1】:

这就是最后的答案:

Button buttonview = inflator.inflate(button layout xml);
buttonview.setonclicklistener(listener);

 AddBtn.setOnClickListener(new View.OnClickListener() 
                            {

                                @Override
                                public void onClick(View v) 
                                {

                                        ingAddLayout.addView(buttonview);
                                    }


                                }

                            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 2015-10-21
    • 2014-03-02
    • 1970-01-01
    相关资源
    最近更新 更多