Java 为动态创建的复选框添加onClick操作侦听器
我正在Android类中动态创建复选框(非活动)。所以我需要在我的复选框中添加Java 为动态创建的复选框添加onClick操作侦听器,java,android,Java,Android,我正在Android类中动态创建复选框(非活动)。所以我需要在我的复选框中添加onClickAction Listener,以及如何实现它 我正在使用以下代码 public class DataBaseAdapter extends SQLiteOpenHelper { ...//onCreate and onUpdate ... ... public TableLayout getAllAlarmList(Context con) {
onClick
Action Listener,以及如何实现它
我正在使用以下代码
public class DataBaseAdapter extends SQLiteOpenHelper
{
...//onCreate and onUpdate
...
...
public TableLayout getAllAlarmList(Context con)
{
TableLayout tb = new TableLayout(con);
TableRow[] tr = new TableRow[maxCount]; //maxCount is the number of rows
CheckBox[] check = new CheckBox[maxCount]; //maxCount is the number of rows in the database.
for(int i=0;i<maxCount;i++)
{
tr[i]=new TableRow(con);
check[i]= new CheckBox(con); //con is Context class passed as argument.
check[i].setText(Integer.toString(i));
check[i].setId(100+i);
// I have to add onClick Action Listener here.
tr[i].addView(check[i]);
tb.addView(tr[i]);
}
return tb;
}
}
public类数据库适配器扩展了SQLiteOpenHelper
{
…//onCreate和onUpdate
...
...
公共表布局getAllAlarmList(上下文con)
{
TableLayout tb=新的TableLayout(con);
TableRow[]tr=new TableRow[maxCount];//maxCount是行数
CheckBox[]check=new CheckBox[maxCount];//maxCount是数据库中的行数。
对于(int i=0;i,您还可以将任何侦听器设置为非活动类的视图。尝试如下操作:
check[i].setId(100+i);
check[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
switch(buttonView.getId()) {
case R.id.checkbox_meat:
// do your code here...
break;
case R.id.checkbox_cheese:
// do your code here...
break;
// TODO: Veggie sandwich
}
}
});
你设置CheckBox的检查[i]setOnCheckedChangeListener
有什么问题?因为它不是活动类,所以无法编写检查[i]setOnCheckedChangeListener(新的onCheckedChangeListener)
作为类不扩展视图类,那么如何实现@ShardaSingh:ok首先删除android:onClick=“onCheckboxClicked"
来自所有复选框xml。复选框是动态存在的。因此,在xml中,复选框不会被单击。复选框是动态创建的,因此前一个复选框没有关于下一个复选框及其id的记录。@ShardaSingh:我在回答中集成了代码,如果仍然存在任何问题,请检查它,然后解释更多您想要的内容点击复选框后执行