【问题标题】:how to change a GONE visibility to VISIBLE visibility in android studio如何在 android studio 中将 GONE 可见性更改为 VISIBLE 可见性
【发布时间】:2016-06-24 19:03:29
【问题描述】:

在我的应用程序中,我将 Frame Layout 可见性设置为 GONE,并且我想在单击复选框时显示 Frame 布局。可以设置为可见吗?

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

标签: java android xml


【解决方案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.
        }
    }
  }); 

【讨论】:

  • 感谢您的回答,但我之前的代码与上面的代码相同,但无法正常工作。
【解决方案2】:

试试这个方法对你有帮助

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);
            }
        }
    };

【讨论】:

  • 对不起,我对这段代码有点困惑,我应该什么时候声明状态和位置?对不起,这是我第一次,所以请引导我。谢谢
  • 职位和地位是什么意思?
  • status.set(位置,真)。我们需要先声明它吗?
  • 首先检查您的状态..为此使用日志..然后设置您的可见性
  • 勾选复选框或看不到这个stackoverflow.com/questions/18336151/…
【解决方案3】:

可能是因为框架布局中的视图可见性也消失了。

试试下面的代码

    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。

【讨论】:

  • 我的框架布局不为空。它包含线性布局和三个编辑文本。
  • 已更改我的答案。试试看。
  • 你在哪里声明了 mViewGroup ?
  • 我已经编辑了我的答案。我将 frameLayout 声明为 ViewGroup。而不是将其声明为 FrameLayout。
  • 所以它将是 ViewGroup frameLayout 而不是 FrameLayout frameLayout
猜你喜欢
  • 2011-05-01
  • 2014-12-14
  • 1970-01-01
  • 2015-05-26
  • 2023-03-23
  • 2013-04-16
  • 1970-01-01
  • 2016-09-17
  • 2017-10-31
相关资源
最近更新 更多