我正在动态添加按钮,当我按下其他按钮时,动态添加按钮的数量会发生变化。
如何为每个动态添加的按钮设置单击侦听器。 (按钮数量可以改变)
这是我的代码:
final LinearLayout llNearbyPlaceslist = (LinearLayout)findViewById(R.id.nearby_places_list);
try{
llNearbyPlaceslist.removeAllViews();
for (Place p : nearPlaces.results) {
//Log.e("Places",p.name);
// Getting and placing list of places in Scroll View
Button btn = new Button(UserClass.this);
btn.setText(p.name);
NBPbuttonsList.add(btn);
llNearbyPlaceslist.addView(btn);
}
}
答案 0 :(得分:2)
只需设置每个侦听器:
后:
Button btn = new Button(UserClass.this);
添加:
btn.setOnClickListener....