我有一个Button
add_Connection
拥有CheckBox
动态创建的TableRow
。
如果选中add_Connection
,我想启用CheckBox
,
当我使用此代码Button
启用禁用工作正常,但如果我检查cb1和cb2按钮启用
如果我取消选中cb2并且仍然选择了cb1,则会禁用Button
点击完成后。
这里
cb: check box
add_connection: Button
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
add_Connections.setEnabled(isChecked);
}
});
答案 0 :(得分:0)
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
Button button;
ArrayList<CheckBox> checkboxesList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
button = (Button) findViewById(R.id.button);
button.setEnabled(false);
cb1.setTag("cb1");
cb2.setTag("cb2");
cb3.setTag("cb3");
checkboxesList.add(cb1);
checkboxesList.add(cb2);
checkboxesList.add(cb3);
CheckBoxListener listener = new CheckBoxListener(checkboxesList, button);
for (CheckBox checkbox : checkboxesList) {
checkbox.setOnCheckedChangeListener(listener);
}
}
public static class CheckBoxListener implements CompoundButton.OnCheckedChangeListener {
ArrayList<CheckBox> list;
Button button;
public CheckBoxListener(ArrayList<CheckBox> checkboxesList, Button btn) {
list = checkboxesList;
button = btn;
}
private boolean checkState() {
for (CheckBox checkbox : list) {
if (checkbox.isChecked()) {
return true;
}
}
return false;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
button.setEnabled(checkState());
Log.d("TAG", "Button is: " + checkState());
}
}
}
and layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.thomsonreuters.alexandrugoja.myapplication.MainActivity">
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>