Java 为每个循环按钮提供Id,并根据按钮的Id设置单击侦听器
我有一个程序,将检索一些数据从数据库,并显示在屏幕上的一些按钮。如何为创建的每个按钮提供Id,并使button.OnClickListener为每个具有正确Id的按钮提供Id 这是我的密码:Java 为每个循环按钮提供Id,并根据按钮的Id设置单击侦听器,java,android,sqlite,button,Java,Android,Sqlite,Button,我有一个程序,将检索一些数据从数据库,并显示在屏幕上的一些按钮。如何为创建的每个按钮提供Id,并使button.OnClickListener为每个具有正确Id的按钮提供Id 这是我的密码: private void selectAllGroup() { // TODO Auto-generated method stub MyGroup = (TextView) findViewById(R.id.tvListGroup); Database allGroup = new Database(M
private void selectAllGroup() {
// TODO Auto-generated method stub
MyGroup = (TextView) findViewById(R.id.tvListGroup);
Database allGroup = new Database(MyGroupActivity.this);
allGroup.open();
listGroup = allGroup.countHowManyGroups(username);
layout = (LinearLayout) findViewById(R.id.LLMyGroup);
String groupName[] = allGroup.fetchGroupName(username);
for (int i = 0; i < listGroup; i++) {
newBt = new Button(this);
newBt.setText(groupName[i]);
layout.addView(newBt);
}
allGroup.close();
}
您可以将for循环内的每个按钮的标签设置为
newBt.setTag(i);
newBt.setOnClickListener(this);
在OnClick方法中,将标记作为如下位置
@Override
public void onClick(View view){
int buttonClicked = (int) view.getTag();
// Do your code here according to the position of the button clicked
}
关于您的问题,我如何为创建的每个按钮提供Id,并为每个具有正确Id的按钮创建button.OnClickListener 只需在for循环中使用:
newBt.setId(id to set) // Should be positive, doesn't have to be unique
newBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//The View carries the Id
v.getId();
//Then you can simply set up a fast Switch-case for example
switch (v.getid()) {
case 1:
break;
default:
break;
}
});
更新
没问题,只需在onClick方法中将按钮强制转换到视图并获取字符串:
newBt.setId(id to set) // Should be positive, doesn't have to be unique
newBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button myClickedButton = (Button) v;
String buttonText = myClickedButton.getText();
//Do something with your logic here.
//You can also Switch-case on a String ! But AFAIK
//it's only possible on Java Compiler 1.7 or above.
//But Eclipse will guide you if you want to Switch on String
});
您不需要id来收听按钮点击事件。@piyush关于piyush Ansare:如果他不想为每个按钮提供相同的逻辑,他怎么知道哪个按钮被点击了?。您好,谢谢您的回答,,如果我想从点击的按钮中获取按钮文本呢?它能通过吗?
newBt.setId(id to set) // Should be positive, doesn't have to be unique
newBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button myClickedButton = (Button) v;
String buttonText = myClickedButton.getText();
//Do something with your logic here.
//You can also Switch-case on a String ! But AFAIK
//it's only possible on Java Compiler 1.7 or above.
//But Eclipse will guide you if you want to Switch on String
});
for (int i = 0; i < listGroup; i++) {
newBt = new Button(this);
newBt.setText(groupName[i]);
newBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//write your logic to do the work you need when button is clicked.You do not need id for this.
}
});
layout.addView(newBt);
}