Java 如何检查是否在其 onClick 方法(在 XML 中声明)中选中了 android 复选框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18336151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if android checkbox is checked within its onClick method (declared in XML)?
提问by aBhijit
I have a checkbox in android which has the following XML:
我在 android 中有一个复选框,它具有以下 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.
这是我的 Activity 类中的 onClick() 方法。
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?
我知道我们可以创建复选框的对象并为其分配 id。但是在onClick
通过 XML声明方法时是否有更好的方法来实现功能?
采纳答案by Anand
try this one :
试试这个:
public void itemClicked(View v) {
//code to check if this checkbox is checked!
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()){
}
}
回答by Sami Eltamawy
This will do the trick:
这将解决问题:
public void itemClicked(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(MyAndroidAppActivity.this,
"Checked", Toast.LENGTH_LONG).show();
}
}
回答by Kishita Variya
You can try this code:
你可以试试这个代码:
public void itemClicked(View v) {
//code to check if this checkbox is checked!
if(((Checkbox)v).isChecked()){
// code inside if
}
}
回答by vbijoor
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fees Paid Rs100:"
android:textColor="#276ca4"
android:checked="false"
android:onClick="checkbox_clicked" />
Main Activity from here
从这里开始的主要活动
public class RegistA extends Activity {
CheckBox fee_checkbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_regist);
fee_checkbox = (CheckBox)findViewById(R.id.checkBox1);// Fee Payment Check box
}
checkbox clicked
单击复选框
public void checkbox_clicked(View v)
{
if(fee_checkbox.isChecked())
{
// true,do the task
}
else
{
}
}
回答by Nicoolasens
@BindView(R.id.checkbox_id) // if you are using Butterknife
CheckBox yourCheckBox;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
yourCheckBox = (CheckBox)findViewById(R.id.checkbox_id);// If your are not using Butterknife (the traditional way)
yourCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yourObject.setYourProperty(yourCheckBox.isChecked()); //yourCheckBox.isChecked() is the method to know if the checkBox is checked
Log.d(TAG, "onClick: yourCheckBox = " + yourObject.getYourProperty() );
}
});
}
Obviously you have to make your XML with the id of your checkbox :
显然,您必须使用复选框的 id 制作 XML:
<CheckBox
android:id="@+id/checkbox_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your label"
/>
So, the method to know if the check box is checked is : (CheckBox) yourCheckBox.isChecked()
it returns true
if the check box is checked.
因此,知道复选框是否被选中的方法是:如果复选框被选中(CheckBox) yourCheckBox.isChecked()
则返回true
。