触摸片段内的按钮时隐藏/显示TextView

时间:2018-06-20 11:35:50

标签: java android fragment visibility visible

所以我在这里寻求您的帮助。 我试图通过触摸单个按钮使TextView可见并消失。 它确实在MainActivityJava内部工作,但是在Fragment Activity内部进行同样工作时,我面临许多麻烦。 似乎setOnClickListener与片段不匹配,但我不知道如何处理...

package com.androidbegin.locumatix;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class Tab1Fragment extends Fragment {



 @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab1, container, false);
        return view;
    }

    Button button = getActivity().findViewById(R.id.buttonTaureauBrute);
    TextView textView = getActivity().findViewById(R.id.textView3);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {

            textView.setVisibility(View.GONE);
            textView.setVisibility(View.VISIBLE);
        }
    };

}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wallpaper_selection">

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

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

                <ImageButton
                    android:id="@+id/imageButtonTaureauSon"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:scaleType="fitXY"
                    app:srcCompat="@drawable/prononciation_icone" />

                <Button
                    android:id="@+id/buttonTaureauBrute"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="5"
                    android:text="Prendre le taureau par les cornes"
                    android:textAlignment="center"
                    android:onClick="onClick"/>

            </LinearLayout>

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="16dp"
                android:layout_weight="1"
                android:gravity="center"
                android:visibility="gone"
                android:text="Affronter une difficulté avec détermination"
                android:textAlignment="center"
                android:textColor="@color/colorAccent" />

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/taureau" />

            <Button
                android:id="@+id/button10"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Exemples :"
                android:textAlignment="center" />

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:text="NATURE :\nverbe"
                    android:textAlignment="center"
                    android:textColor="@color/colorAccent" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:text="REGISTRE :\nstandard"
                    android:textAlignment="center"
                    android:textColor="@color/colorAccent" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:text="NIVEAU :\nB1"
                    android:textAlignment="center"
                    android:textColor="@color/colorAccent" />


            </LinearLayout>

            <Button
                android:id="@+id/button14"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="EXPLICATIONS :" />

            <Button
                android:id="@+id/button11"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="USAGES :" />

            <Button
                android:id="@+id/button13"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Lexique :" />

            <Button
                android:id="@+id/button12"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Traductions :" />

        </LinearLayout>

    </ScrollView>

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

尝试此代码

public class Tab1Fragment extends Fragment {

    Button button;
    TextView textView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab1, container, false);

        button = getActivity().findViewById(R.id.buttonTaureauBrute);
        textView = getActivity().findViewById(R.id.textView3);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                if( view.getVisibility() == View.GONE ) {
                    textView.setVisibility(View.VISIBLE);
                } else {
                    textView.setVisibility(View.GONE);
                }
            }
        };

        return view;
    }

}