LinearLayout Listener无法正常工作

时间:2015-09-17 08:45:40

标签: android listener android-linearlayout

我有一个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);
    }
});

1 个答案:

答案 0 :(得分:0)

这是最后的答案:

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

 AddBtn.setOnClickListener(new View.OnClickListener() 
                            {

                                @Override
                                public void onClick(View v) 
                                {

                                        ingAddLayout.addView(buttonview);
                                    }


                                }

                            });