如何验证多个复选框
How to validate multiple checkboxes
我在一行中有三个复选框。对于每个复选框,我在下面都有一个不可见的布局。
1) 如果选中第一个复选框,相应的布局将可见。
2) 如果选中第二个复选框,相应的布局将可见。
3) 如果选中第三个复选框,相应的布局将可见。
4) 如果所有复选框都被选中,所有布局将可见。
检查您的复选框是否被选中,并根据它显示隐藏,如下所示:
CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
yourLayout.setVisibility(View.Visible);
}
else
{
yourLayout.setVisibility(View.Gone );
}
}
});
在您的 xml 布局文件中,为每个复选框指定一个 ID(checkbox_1、checkbox_2、checkbox_3):
android:id="@+id/checkbox_1"
与布局元素相同(layout_1、layout_2、layout_3):
android:id="@+id/layout_1"
在您的代码中,指向元素:
CheckBox checkbox1 = (CheckBox) findViewById(R.id.checkbox_1);
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout_1);
为每个复选框添加一个检查侦听器:
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
layout1.setVisibility(View.VISIBLE);
}
else
{
layout1.setVisibility(View.INVISIBLE);
}
}
});
其他两个也一样。
试试这个代码
public class MySampleActivity extends Activity {
CheckBox cb1, cb2, cb3, cb4;
LinearLayout l1, l2, l3, l4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
l3 = (LinearLayout) findViewById(R.id.l3);
l4 = (LinearLayout) findViewById(R.id.l4);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(1));
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(2));
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(3));
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(4));
}
public class MyCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
int position;
public MyCheckedChangeListener(int position) {
this.position = position;
}
private void changeVisibility(LinearLayout l1, boolean isChecked) {
if (isChecked) {
l1.setVisibility(View.VISIBLE);
} else {
l1.setVisibility(View.GONE);
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (position) {
case 1:
changeVisibility(l1, isChecked);
break;
case 2:
changeVisibility(l2, isChecked);
break;
case 3:
changeVisibility(l3, isChecked);
break;
case 4:
changeVisibility(l4, isChecked);
break;
}
}
}
}
我在一行中有三个复选框。对于每个复选框,我在下面都有一个不可见的布局。
1) 如果选中第一个复选框,相应的布局将可见。
2) 如果选中第二个复选框,相应的布局将可见。
3) 如果选中第三个复选框,相应的布局将可见。
4) 如果所有复选框都被选中,所有布局将可见。
检查您的复选框是否被选中,并根据它显示隐藏,如下所示:
CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
yourLayout.setVisibility(View.Visible);
}
else
{
yourLayout.setVisibility(View.Gone );
}
}
});
在您的 xml 布局文件中,为每个复选框指定一个 ID(checkbox_1、checkbox_2、checkbox_3):
android:id="@+id/checkbox_1"
与布局元素相同(layout_1、layout_2、layout_3):
android:id="@+id/layout_1"
在您的代码中,指向元素:
CheckBox checkbox1 = (CheckBox) findViewById(R.id.checkbox_1);
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout_1);
为每个复选框添加一个检查侦听器:
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
layout1.setVisibility(View.VISIBLE);
}
else
{
layout1.setVisibility(View.INVISIBLE);
}
}
});
其他两个也一样。
试试这个代码
public class MySampleActivity extends Activity {
CheckBox cb1, cb2, cb3, cb4;
LinearLayout l1, l2, l3, l4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
l3 = (LinearLayout) findViewById(R.id.l3);
l4 = (LinearLayout) findViewById(R.id.l4);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(1));
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(2));
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(3));
cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(4));
}
public class MyCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
int position;
public MyCheckedChangeListener(int position) {
this.position = position;
}
private void changeVisibility(LinearLayout l1, boolean isChecked) {
if (isChecked) {
l1.setVisibility(View.VISIBLE);
} else {
l1.setVisibility(View.GONE);
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (position) {
case 1:
changeVisibility(l1, isChecked);
break;
case 2:
changeVisibility(l2, isChecked);
break;
case 3:
changeVisibility(l3, isChecked);
break;
case 4:
changeVisibility(l4, isChecked);
break;
}
}
}
}