android复选框显示和隐藏

时间:2016-10-01 04:32:31

标签: android checkbox

当我点击我想要显示linearlayout7,linearlayout8,linearlayout9的复选框时,我在linearlayout6中有一个复选框。否则我想隐藏这3个linearlayout(7,8,9)。

复选框

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox1"
/>

XML     

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:text="@string/string41" />

    <EditText
        android:id="@+id/txtTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:ems="10"
        android:hint="@string/string41"/>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/screen4layout2">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:text="@string/string42" />

    <EditText
        android:id="@+id/txtAuthor"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:ems="10"
        android:hint="@string/string42"/>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/screen4layout3">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:text="@string/string43" />

    <EditText
        android:id="@+id/txtPublisher"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:ems="10"
        android:hint="@string/string43"/>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/screen4layout4">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.25"
        android:text="@string/string44" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/txtIsbn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="5"
            android:hint="@string/string44"/>
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/screen4layout5">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.25"
        android:text="@string/string45" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editText6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="5"
            android:hint="@string/string45" />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/screen4layout6">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox1"
/>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.25"
            android:text="@string/string46" />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/screen4layout7">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.25"
        android:text="@string/string47" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editText7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="5"
            android:hint="@string/string47" />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/screen4layout8">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.25"
        android:text="@string/string48" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editText8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="5"
            android:hint="@string/string48" />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/screen4layout9">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.25"
        android:text="@string/string49" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editText9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="5"
            android:hint="@string/string49" />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="81dp"
    android:orientation="horizontal"
    android:id="@+id/screen4layout10">

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnSubmit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="5"
            android:text="@string/save" />

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="5"
            android:text="Delete" />
        <Button
            android:id="@+id/btn_close_popup4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Close" />
    </LinearLayout>
</LinearLayout>

5 个答案:

答案 0 :(得分:1)

checkboxes布局中添加三个xml,就像您为checkbox1所做的那样,然后为它们指定ID。

  CheckBox checkbox=(CheckBox)findViewById(R.id.checkBoxInLayout6);

     public void addListenerOnChk() // for checkbox
        {

            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                    if(isChecked)
                    {
                        checkBoxInLayout7.setVisibility(View.VISIBLE);
                        checkBoxInLayout8.setVisibility(View.VISIBLE);
                        checkBoxInLayout9.setVisibility(View.VISIBLE);

                    }
                    else
                    {
                        checkBoxInLayout7.setVisibility(View.INVISIBLE);
                        checkBoxInLayout8.setVisibility(View.INVISIBLE);
                        checkBoxInLayout9.setVisibility(View.INVISIBLE);

                    }
                }
            });
        }

答案 1 :(得分:1)

试试这些:

 checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                {
                    ll7.setVisibility(View.VISIBLE);
                    ll8.setVisibility(View.VISIBLE);
                    ll9.setVisibility(View.VISIBLE);

                }
                else
                {
                    ll7.setVisibility(View.GONE);
                    ll8.setVisibility(View.GONE);
                    ll9.setVisibility(View.GONE);

                }
            }
        });

答案 2 :(得分:1)

CheckBox checkbox=(CheckBox)findViewById(R.id.checkbox1);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                if(isChecked)
                {
                    checkBoxInLayout7.setVisibility(View.VISIBLE);
                    checkBoxInLayout8.setVisibility(View.VISIBLE);
                    checkBoxInLayout9.setVisibility(View.VISIBLE);

                }
                else
                {
                    checkBoxInLayout7.setVisibility(View.INVISIBLE);
                    checkBoxInLayout8.setVisibility(View.INVISIBLE);
                    checkBoxInLayout9.setVisibility(View.INVISIBLE);

                }
            }
        });

轻松一试......

答案 3 :(得分:0)

这是解决方案

checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                linearlayout7.setVisibility(View.VISIBLE);
                linearlayout8.setVisibility(View.VISIBLE);
                linearlayout9.setVisibility(View.VISIBLE);
            }else {
                linearlayout7.setVisibility(View.GONE);
                linearlayout8.setVisibility(View.GONE);
                linearlayout9.setVisibility(View.GONE);
            }
        }
    });

简单的代码。 很乐意提供帮助

答案 4 :(得分:0)

1.增加

android:onClick="onCheckboxClicked" 

将XML格式设置为CheckBox,然后在.java文件中添加以下代码

public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();

// Check which checkbox was clicked
switch(view.getId()) {
    case R.id.checkbox_meat:
        if (checked)
            showLayout789();
          //do things when checked
        else
            removeLayout789();
          // do things when unchecked
        break;

}
}

现在使用他们的ID找到布局并设置其可见性

private void showLayout789(){
View v9 = findViewById(R.id.screen4layout9);
    v9.setVisibility(View.VISIBLE);
View v8 = findViewById(R.id.screen4layout8);
    v8.setVisibility(View.VISIBLE);
View v7 = findViewById(R.id.screen4layout7);
    v7.setVisibility(View.VISIBLE);
}

设置隐身

 private void showLayout789(){
View v9 = findViewById(R.id.screen4layout9);
    v9.setVisibility(View.GONE);
View v8 = findViewById(R.id.screen4layout8);
    v8.setVisibility(View.GONE);
View v7 = findViewById(R.id.screen4layout7);
    v7.setVisibility(View.GONE);
}