【发布时间】: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