gpt4 book ai didi

java - 触摸 fragment 内的按钮时隐藏/显示 TextView

转载 作者:太空宇宙 更新时间:2023-11-04 10:22:32 25 4
gpt4 key购买 nike

所以我在这里寻求您的帮助。我试图通过触摸一个按钮来使 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>

最佳答案

试试这个代码

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

}

关于java - 触摸 fragment 内的按钮时隐藏/显示 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50947289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com