为 Android 中动态创建的按钮实现 OnClickListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10673628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing OnClickListener for dynamically created buttons in Android
提问by user1276092
I dynamically created buttons through code rather than from XML.
The code is as below :
我通过代码而不是从 XML 动态创建按钮。
代码如下:
dynamicview = (LinearLayout)findViewById(R.id.llayout);
LinearLayout.LayoutParams lprams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
for(int i=0;i<nob;i++){
Button btn = new Button(this);
btn.setId(i+1);
btn.setText("Button"+(i+1));
btn.setLayoutParams(lprams);
dynamicview.addView(btn);
}
I am not finding a way in which I can implement OnClickListener to each of these buttons so that I can perform action based on the reference I get.
Can anyone help me in sorting out this issue. ?
Thanks in Advance,
我没有找到可以对这些按钮中的每一个实现 OnClickListener 的方法,以便我可以根据获得的参考执行操作。
谁能帮我解决这个问题。?
提前致谢,
回答by Boris Strandjev
See the code below:
请参阅下面的代码:
for(int i=0;i<nob;i++) {
Button btn = new Button(this);
btn.setId(i+1);
btn.setText("Button"+(i+1));
btn.setLayoutParams(lprams);
final int index = i;
btn.setOnClickListener(new OnClickListener() {
void onClick(View v) {
Log.i("TAG", "The index is" + index);
}
});
dynamicview.addView(btn);
}
My example is pretty simple, but demonstrates how you can get the button index into the OnClickListeber
. You can access any final
field in the declared anonymous classes (e..g the OnClickListener
).
我的示例非常简单,但演示了如何将按钮索引放入OnClickListeber
. 您可以访问final
声明的匿名类中的任何字段(例如OnClickListener
)。
回答by Samir Mangroliya
for(int i=0;i<nob;i++){
Button btn = new Button(this);
btn.setId(i+1);
btn.setText("Button"+(i+1));
btn.setOnClickListener(btnclick); <<<<<<<set click
btn.setLayoutParams(lprams);
dynamicview.addView(btn);
}
And add this listner outside the any method and inside class
并在任何方法和类内部添加此侦听器
OnClickListener btnclick = new OnClickListener() {
@Override
public void onClick(View view) {
switch(view.getId()){
case 1:
//first button click
break;
//Second button click
case 2:
break;
case 3:
//third button click
break;
case 4:
//fourth button click
break;
.
.
.
default:
break;
}
}
};
回答by Arif Nadeem
Use a List
and add Button
s you create to that List
使用 aList
并添加Button
您创建的 sList
List<Button> list = new ArrayList<Button>();
Now add your button to that List
现在将您的按钮添加到该列表
for(int i=0;i<nob;i++){
Button btn = new Button(this);
btn.setId(i+1);
btn.setText("Button"+(i+1));
btn.setLayoutParams(lprams);
dynamicview.addView(btn);
list.add(btn);
}
Then use advanced for loop to iterate through the list and add click listener for each Button..
然后使用高级 for 循环遍历列表并为每个按钮添加单击侦听器。
for(Button btn : list){
btn.setOnClickListener(this);
}
回答by Leo240
Maybe this will be helpful for somebody I wanted to use the one counter variable for group of dynamically created buttons:
也许这对我想对动态创建的按钮组使用一个计数器变量的人会有所帮助:
I've created the array for counters:
我已经为计数器创建了数组:
int[] squatReps;
From preferences I've taken the quantity of elements:
从偏好中,我采用了元素的数量:
squatReps = new int[squatSets]; // where squatSets is taken from preferences
And finally we create the buttons
最后我们创建按钮
for (int i = 0; i < squatSets; i++){
squatReps[i] = Integer.parseInt(sharedPreferences.getString("squats_reps", "0"));
final Button squat_b = new Button(this.getActivity());
squat_b.setId(i);
squat_b.setText("");
squat_b.setLayoutParams(layoutParams);
final int index = i;
squat_b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
squat_b.setText(Integer.toString(squatReps[index]--));
startTimer();
}
});
panel_1.addView(squat_b);
回答by ngesh
its the same thing...
这是同一件事...
for(int i=0;i<nob;i++){
Button btn = new Button(this);
btn.setId(i+1);
btn.setText("Button"+(i+1));
btn.setLayoutParams(lprams);
btn.setOnCLickListsener(new listener());
dynamicview.addView(btn);
}
listener implemnets OnClickListenere{
public void onClick(View v){
}
}
}
回答by ρяσ?ρ?я K
for(int i=0;i<nob;i++){
Button btn = new Button(this);
btn.setId(i+1);
btn.setText("Button"+(i+1));
btn.setLayoutParams(lprams);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/////Do some job on button click
}
});
dynamicview.addView(btn);
}