在我的应用程序中,我动态添加了复选框。 在应用程序中,当我点击其中一个复选框n然后单击确定但我正在执行一些操作。下面是代码。没有工作单击复选框。 请告诉我该怎么办?
public class ImportFile extends Activity{
TableLayout tl;
Intent i;
ImageView im;
int idcount;
TextView nameoffile,sizeoffile,line;
CheckBox[] ch=new CheckBox[100];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
tl=(TableLayout) findViewById(R.id.table);
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator
+ "gallery" //folder name
);
if (!file.exists()) {
file.mkdirs();
}
/* i=new Intent();
i.putExtra("file","string");
setResult(RESULT_OK, i);
finish(); */
TableRow tr=new TableRow(this);
int id=1;
File f = new File("/sdcard/download");//or Environment.getExternalStorageDirectory().getPath()
File[] files = f.listFiles();
for(int i = 0; i < files.length; i++) {
File file1 = files[i];
//take the file name only
long size = file1.length()/1024;
String myfile = file1.getPath().substring(file1.getPath().lastIndexOf("/")+1,file1.getPath().length()).toLowerCase();
if(myfile.endsWith(".jpeg")||myfile.endsWith(".png")|| myfile.endsWith(".gif") || myfile.endsWith(".jpg"))
{
ch[id]=new CheckBox(this);
ch[id].setId(id);
ch[id].setText(" ");
System.out.println("id is........"+id);
Bitmap b=ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile((file1.getPath())), 40,40);
im=new ImageView(this);
im.setImageBitmap(b);
nameoffile=new TextView(this);
nameoffile.setText(" "+myfile);
nameoffile.setWidth(200);
sizeoffile=new TextView(this);
sizeoffile.setText(size+"KB");
sizeoffile.setWidth(100);
tr=new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
if((id%2)==0)
{
tr.setBackgroundResource(R.color.Thistle);
}
else{
tr.setBackgroundResource(R.color.Bisque);
}
tr.addView(ch[id++]);
idcount=id;
tr.addView(im);
tr.addView(nameoffile);
tr.addView(sizeoffile);
tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
}
}
}
int j;
public void ok(View v)
{
for (j=1;j<idcount;j++)
{
ch[j].setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
System.out.println("Checked ID :: " + ch[j].getId());
}
});
}
}
}
答案 0 :(得分:0)
试试这个
OnClickListener checkBoxListener;
checkBoxListener =new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(checkbox1.isChecked())
{
Toast.makeText(getBaseContext(), "something", Toast.LENGTH_SHORT).show();
}
}
};
checkbox1.setOnClickListener(checkBoxListener);