简体   繁体   中英

Need help in understanding how this checkbox code works in Android

public class CheckBoxDemo extends Activity implements
    CompoundButton.OnCheckedChangeListener {
  CheckBox cb;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    cb=(CheckBox)findViewById(R.id.check);
    cb.setOnCheckedChangeListener(this);
  }

  public void onCheckedChanged(CompoundButton buttonView,
                               boolean isChecked) {
    if (isChecked) {
      cb.setText(R.string.checked);
    }
    else {
      cb.setText(R.string.unchecked);
    }
  }
}

What I'm having trouble in understanding is how exactly this line works with this parameter being passed in

cb.setOnCheckedChangeListener(this);

Also, the method onCheckedChanged is not being explicitly called anywhere, how does Android make the connection to connect the checkbox state to the method name.

You are implementing the interface and onCheckedChanged() is in your code. Your Activity now is also the Listener.

When you implement an interface, you also have to override the methods specified by the interface. This means that the class that implements the interface can now act as an instance of that interface.

public class CheckBoxDemo extends Activity implements
CompoundButton.OnCheckedChangeListener {

Your CheckBoxDemo class implements OnCheckedChangeListener so it can now act as an OnCheckedChangeListener if it is needed.

Then later in your code you have

public void onCheckedChanged

Which is the method from the interface that CheckBoxDemo needs to implement for everything to work.

Therefore, you can now use this (refering to your current CheckBoxDemo instance) to pass off to setOnCheckedChangeListener () because all the prior conditions are met - Your Class can now successfully listen for check events.

For more information, read up on Interfaces from the java tutorials.

When you declared you class, it implements a Interface called OnCheckedChangeListener from the CompoundButton class

implements
    CompoundButton.OnCheckedChangeListener 


when you use this its refering to the this object your are working with which is a instance of the OnCheckedChangeListener .
When you implement a Interface , and the Interface has methods, then your Activity (or any class that does) must implement that method. In your case, its the onCheckedChanged() , so your JVM will know to make this relationship.

You can look up the source for CheckBox , but very roughly it works like this (this is not an excerpt of the code but a demonstration of what it might look like):

public class CheckBox implements ... {
  private OnCheckedChangeListener occl;
  public void setOnCheckedChangeListener(OnCheckedChangeListener newlistener) {
    occl = newlistener;
  }
  /* this will be called when you click the CheckBox */
  public void check(...) {
      /* draw the check mark and similar things, then: */
      occl.onCheckedChanged(...);
  }
}

So your onCheckedChanged() is actually being called by the internal implementation of your Component , on this case your CheckBox .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM