【问题标题】:Hide/Show TextView when touching button inside a Fragment触摸片段内的按钮时隐藏/显示 TextView
【发布时间】:2018-11-29 12:46:22
【问题描述】:

所以我在这里寻求您的帮助。 我试图通过触摸一个按钮使 TextView 可见并消失。 它确实在 MainActivityJava 中工作,但是在 Fragment Activity 中做同样的事情时,我遇到了很多麻烦。 好像setOnClickListener和fragment不匹配但是不知道怎么处理...

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>

【问题讨论】:

  • 把getActivity().findViewById改成view.findViewById,放到onCreateView里面
  • 非常感谢。效果很好^~^

标签: java android fragment visibility visible


【解决方案1】:

试试这个代码

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

}

【讨论】:

  • 亲爱的 Anik,您的代码解决了大部分问题,但 Button 按钮的 findViewById 以黄色突出显示,表示它可能会产生 Java.lang.NullPointerException。
  • 感谢您和@debanjan,他们建议“将 getActivity().findViewById 更改为 view.findViewById”,一切都得到了解决。非常感谢~
  • 我已经编辑了代码。请试一试。顺便说一句,对于视图绑定和设置事件侦听器,您可以使用黄油刀,生活会轻松很多。请查看以下链接以获取黄油刀。 jakewharton.github.io/butterknife
  • 非常感谢您的编辑。我在我的收藏夹列表中添加了“黄油刀”。非常有用。
  • 顺便说一句,亲爱的 Anik,我怎样才能通过触摸同一个按钮(id.buttonTaureauBrute)来隐藏(消失)/显示(可见)Textview(id.textView3)?第一次单击确实隐藏了文本视图,但在第二次单击时它不会重新出现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-21
  • 2019-08-24
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多