【问题标题】:oncheckedchangelistener for checkbox in androidandroid中复选框的oncheckedchangelistener
【发布时间】:2013-02-15 05:31:13
【问题描述】:

在我的应用程序中,我动态添加了复选框。 在应用程序中,当我单击其中一个复选框 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());
                    }


                });
        } 
}
}

【问题讨论】:

  • 需要你的整个活动萌芽,这个 sn-p 并没有说太多。还有更好的方法来设置视图的 onClickListener(),方法是让 Activity imliment onClickListener(),并设置视图 view.onClickListener(this)
  • 我已经更新了上面的代码。我也试过onclickListener。它也不起作用

标签: android checkbox oncheckedchanged


【解决方案1】:

试试这个

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2019-03-10
    • 1970-01-01
    • 2013-04-03
    相关资源
    最近更新 更多