如何在Android Studio中将一个GONE可见性更改为VISIBLE可见性

3
在我的应用程序中,我将我的Frame Layout的可见性设置为GONE,并且我希望在单击复选框时显示Frame Layout。是否可以将其设置为VISIBLE?
xml:这是完整的xml代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/Container">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/red_logo"/>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Marchant Offer"
    android:textSize="30px"
    android:textColor="#ff0000"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Spinner
        android:id="@+id/spinnerCountry"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </Spinner>

    <TextView
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Country" />

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Spinner
        android:id="@+id/spinnerState"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </Spinner>

    <TextView
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="State/Province" />

</LinearLayout>

<EditText
    android:id="@+id/editTextCity"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="City"/>

<EditText
    android:id="@+id/editTextAirport"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Neareast Airport"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Spinner
        android:id="@+id/spinnerCategory"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </Spinner>

    <TextView
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Category" />


</LinearLayout>

<EditText
    android:id="@+id/editSTextOfferTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Offer Title"/>

<EditText
    android:id="@+id/editTextOfferSummary"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Offer Summary"/>


<EditText
    android:id="@+id/editTextOfferDescription"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Offer Description"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Offer Highlights"/>

    <Button
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"/>
</LinearLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Inclusion"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Others Inclusion"/>

    <Button
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"/>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do You Offer Inclusive Hotel/Port Pick-Up and Drop-off"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <CheckBox
        android:id="@+id/checkBoxYes"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

        <TextView
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yes"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/checkBoxNo"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <TextView
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No"/>
    </LinearLayout>
</LinearLayout>

 <FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:visibility="gone">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/ifyes"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Your picking up coverage area"/>

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Time to picking up the Guess"/>

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Additional Info for Guess"/>
    </LinearLayout>
  </FrameLayout>
 </LinearLayout>
</ScrollView>

Java代码:

checkBoxYes = (CheckBox)findViewById(R.id.checkBoxYes);
checkBoxYes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            frameLayout.setVisibility(View.VISIBLE);
        }
    }
});

FrameLayout 的高度是 wrap_content,请将其改为 match_parent - Bharatesh
3个回答

1
是的..

ch = (CheckBox)findViewById(R.id.checkbox);              
ch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        if(isChecked){
            framelayout.setVisibility(View.VISIBLE);//framelayout is your layout,initially set GONE.
        }
    }
  }); 

谢谢您的回答,但是我之前已经试过和上面那段代码一样的代码了,但并没有正常工作。 - Mark Vullen

0

尝试这种方式,它会帮助你的。

private int checkedBoxs = 0;
private final CheckBox.OnCheckedChangeListener listener = 
    new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                status.set(position, true);
                checkedBoxs++;
                if (layout.getVisibility() == View.GONE)
                    layout.setVisibility(View.VISIBLE);
            } else {
                status.set(position, false);
                checkedBoxs--;
                if (checkedBoxs == 0)
                    layout.setVisibility(View.GONE);
            }
        }
    };

首先检查您的状态,使用日志进行记录,然后设置您的可见性。 - Aditya Vyas-Lakhan
要检查复选框是否被选中,请参见此链接:https://dev59.com/7WMl5IYBdhLWcg3wgnSl - Aditya Vyas-Lakhan
抱歉再次问一个愚蠢的问题,您说的“使用日志”是什么意思? - Mark Vullen
使用Log.d(TAG, "复选框的状态为:" + 你的值); - Aditya Vyas-Lakhan
你也可以使用 System.out.println();。 - Aditya Vyas-Lakhan
显示剩余3条评论

0

可能是因为您的帧布局中的视图可见性也已经消失了。

请尝试以下代码

    checkBoxYes = (CheckBox)findViewById(R.id.checkBoxYes);
    ViewGroup frameLayout = (ViewGroup)findViewById(R.id.frameLayout)
    checkBoxYes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked){
        frameLayout.setVisibility(View.VISIBLE);
         int count = frameLayout.getChildCount();
    for (int i = 0; i < count; i++) {
        View view = frameLayout.getChildAt(i);
        view.setVisibility(View.VISIBLE);
    }
    }
}

});

确保你的 frameLayout 被转换为 ViewGroup。


已更改我的答案,请试一下。 - Ragesh Ramesh
我已经编辑了我的答案。我将frameLayout声明为ViewGroup,而不是声明为FrameLayout。 - Ragesh Ramesh
所以,在XML中我也应该将FrameLayout更改为ViewGroup吗? - Mark Vullen
为什么不展示你整个XML,这样我可能能够更好地帮助你。 - Ragesh Ramesh
谢谢大家帮助我,我已经解决了问题,非常感谢你们所有人。 - Mark Vullen
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接