android 在onClick上切换布局的可见性

tyky79it  于 2023-01-11  发布在  Android
关注(0)|答案(4)|浏览(113)

我需要设置这个布局可见,当我点击一个按钮,我的java是这样的:

Layout propLayout = (Layout) findViewById(R.id.properLayout);

    public void propsBtn(View view) {
propLayout.setVisiblity(View.Visible);
}

我知道我的布局线完全错了!如果有人能告诉我如何设置正确,我将非常感激:)
这是我的XML:

<FrameLayout 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:background="@color/mainBackGround"
tools:context="com.myapplication2.app.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp">

...contents...

    </RelativeLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"
    android:id="@+id/properLayout">

    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_gravity="bottom"
        android:padding="8dp">

...contents...           

    </RelativeLayout>

</LinearLayout>
lrpiutwd

lrpiutwd1#

DerGolem给出了正确的答案,但他随后删除了它,所以我现在在这里报告它:

//First we set visibility value of interested layout either to gone, visible or invisible
android:visibility="gone"

然后在onCreate下面写:

//specify the button that has to handle visibility  
ImageButton properties = (ImageButton) findViewById(R.id.propBtn);

//And the layout we want to change is visibility
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.properLayout);

properties.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if (propLayout.getVisibility() == View.VISIBLE){
            propLayout.setVisibility(View.INVISIBLE);
        } else {
            propLayout.setVisibility(View.VISIBLE);
        }
    }
});
wgmfuz8q

wgmfuz8q2#

另一种切换方式,如果有人喜欢,可以使用更少的行

// button to click
ImageButton properties = (ImageButton) findViewById(R.id.propBtn);

// and LinearLayout to toggle
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.properLayout);

properties.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        propLayout.setVisibility((propLayout.getVisibility() == View.VISIBLE) 
          ? View.INVISIBLE 
          : View.VISIBLE);
    }
});
0sgqnhkj

0sgqnhkj3#

您需要在要切换的布局上设置OnClickListener()。然后在侦听器中检查布局是否可见。如果可见,则将其可见性设置为View.INVISIBLE。否则,将其设置为View.VISIBLE

7uzetpgm

7uzetpgm4#

试试这个:

int counter = 0;
    TextVeiw tv = (TextView) findViewById(R.id.textView);
Button button = (Button ) findViewById(R.id.but);
button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
    // TODO Auto-generated method stub
    counter++;
    if(counter%2==0){
        tv.setVisibility(View.Visible);//show
    } else {
        tv.setVisibility(View.Gone);//hide
    }
}
});

相关问题