开发者

How to create single check box event for all check boxes in android?

开发者 https://www.devze.com 2023-03-17 04:03 出处:网络
I\'m new to android it is possible in .net, But i want to build this logic in android is it possible to do that please help me, i kno开发者_开发技巧w only creating check box event for each check box i

I'm new to android it is possible in .net, But i want to build this logic in android is it possible to do that please help me, i kno开发者_开发技巧w only creating check box event for each check box instead of that i want to create single event for all Check boxes please help me.

Vb.net

private sub Chkbox1_Checkchanged(byval sen as object,byval e as system.eventargs) handles Chkbox1.checked,chkbox2.checked,chkbox3.checked
    blnvalue=true
end sub

android

  chkbox1= (CheckBo1) findViewById(R.id.chkbox1);
  rbtn1.setOnClickListener(new View.OnClickListener()
  {         
      public void onClick(View v) 
      {             
              blnvalue=true;
      }
   });


implements onCheckChangedEvent for for all checkBox.

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (buttonView.getId() == activity.getCheckRefresh()) {
            activity.setRefreshRate("1");
            activity.setEnable(isChecked);
            return;
        }
}


Assuming you are in an Activity, you can make your Activity implement View.OnClickListener, and then use this for each View within your Activity, even ones that aren't CheckBox.

public class MyActivity extends Activity implements View.OnClickListener {

    //declare checkboxes

    public void onCreate(Bundle something) {
        //setup stuff and get views

        rbtn1.setOnClickListner(this);
        rbtn2.setOnClickListner(this);
        rbtn3.setOnClickListner(this);
        rbtn4.setOnClickListner(this);
    }

    public void onClick(View v) {
       if(v == rbtn1) {
           //do stuff for checkbox1
       } else if(v == rbtn2) {
           //do stuff for checkbox2
       } else if(v == rbtn3) {
           //do stuff for checkbox3
       } else if(v == rbtn4) {
           //do stuff for checkbox4
       }
    }
}


You can also use onChackChanged Event & OnClickEvent for checkbox.

chkbox1= (CheckBo1) findViewById(R.id.chkbox1);
chkbox2= (CheckBo1) findViewById(R.id.chkbox2);


chkbox1.setOnClickListener(this);
chkbox1.setOnCheckedChangeListener(this);

chkbox2.setOnClickListener(this);
chkbox2.setOnCheckedChangeListener(this);

public void onClick(View v) 
{
   if(v.getId() == R.id.chkbox2)
          // your code
}

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    if (buttonView.getId() == activity.getCheckRefresh()) {
        activity.setRefreshRate("1");
        activity.setEnable(isChecked);
        return;
    }
}


u can do this.. here click is an inner class.. and create an instance of it n pass it to every setOnClickListener(click)

class click implements OnClickListener
{
 public void onClick(View v) 
{
// your code
}

}
0

精彩评论

暂无评论...
验证码 换一张
取 消