简体   繁体   English

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

[英]Hide/Show TextView when touching button inside a Fragment

So I am here asking for your kind help. 所以我在这里寻求您的帮助。 I am trying to make a TextView visible and gone by touching a single button. 我试图通过触摸单个按钮使TextView可见并消失。 It did work inside a MainActivityJava but when doing the same inside a Fragment Activity, I am facing many troubles. 它确实在MainActivityJava内部工作,但是在Fragment Activity内部进行同样工作时,我面临许多麻烦。 It seems that the setOnClickListener does not match with fragment but I do not know how to deal with it... 似乎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>

Try this code 试试这个代码

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:切换按钮打开:显示片段关闭:隐藏片段 - Android: Toggle Button On: Show Fragment Off: Hide Fragment 当我尝试在片段中显示或隐藏浮动操作按钮时出错 - Error when I Tried to show or hide a Floating Action Button from a fragment 在片段内的onLocationChanged内更改TextView时,应用程序崩溃 - App crashing when TextView is changed inside onLocationChanged inside a fragment 我想从片段中隐藏片段容器视图(在 MainActivity 布局内),但是当我单击任务按钮然后重新打开应用程序时它不起作用 - i want to hide fragment container view (Inside MainActivity Layout) from fragment but its not working when i click task button and then reopen app CollapsingToolbarLayout 折叠时隐藏 TextView,展开时显示 - Hide TextView when CollapsingToolbarLayout is Collapsed, and show it when expanded 当用户不触摸 JScrollpane 时隐藏 JScrollPane 的 ScrollBar - Hide ScrollBar of the JScrollPane when user not touching JScrollpane 封装TextView隐藏/显示方法 - Encapsulate TextView Hide/Show Method Android:如何在片段中按返回按钮时隐藏底页? - Android: How to hide bottom sheet when back button is pressed in fragment? 设置textView时片段中的NPE - NPE in Fragment when setting textView 显示/隐藏特定片段的底部导航视图 - Show/Hide BottomNavigationView for specific fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM