Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

java - How to check if android checkbox is checked within its onClick method (declared in XML)?

I have a checkbox in android which has the following XML:

<CheckBox
   android:id="@+id/item_check"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="itemClicked" />

This is my onClick() method in my Activity class.

public void itemClicked(View v) {
  //code to check if this checkbox is checked!
}

I am aware that we can create an object of the checkbox and assign id to it. But is there a better way to achieve the functionality when declaring onClick method via XML?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try this one :

public void itemClicked(View v) {
  //code to check if this checkbox is checked!
  CheckBox checkBox = (CheckBox)v;
  if(checkBox.isChecked()){

  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...