Android 如何切换同一活动中不同文本视图的可见性
我有3张卡片显示在我的应用程序屏幕上。当用户单击卡片时,卡片应展开以显示文本。我想通过在点击的卡片中切换文本视图的可见性来实现这一点。页面上的第一张卡按预期工作,但其他卡都不能正常工作。我如何实现期望的行为 这是我的科特林课程:Android 如何切换同一活动中不同文本视图的可见性,android,android-studio,android-layout,kotlin,mobile,Android,Android Studio,Android Layout,Kotlin,Mobile,我有3张卡片显示在我的应用程序屏幕上。当用户单击卡片时,卡片应展开以显示文本。我想通过在点击的卡片中切换文本视图的可见性来实现这一点。页面上的第一张卡按预期工作,但其他卡都不能正常工作。我如何实现期望的行为 这是我的科特林课程: private lateinit var thruHist: TextView private lateinit var esHist: TextView private lateinit var bayHist: TextView override fun onCr
private lateinit var thruHist: TextView
private lateinit var esHist: TextView
private lateinit var bayHist: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// this tells the app which layout to associate with this class
setContentView(R.layout.activity_workhistory)
// now connect each view to a variable for manipulation
thruHist = findViewById(R.id.thruHistory)
esHist = findViewById(R.id.esHistory)
bayHist = findViewById(R.id.bayHistory)
}
fun openBay(view: View) {
if (bayHist.visibility === View.VISIBLE) {
bayHist.visibility = View.GONE
} else bayHist.visibility = View.VISIBLE
}
fun openEs(view: View) {
if (esHist.visibility === View.VISIBLE) {
esHist.visibility = View.GONE
} else esHist.visibility = View.VISIBLE
}
fun openThru(view: View) {
if (thruHist.visibility === View.VISIBLE) {
thruHist.visibility = View.GONE
} else thruHist.visibility = View.VISIBLE
}
以下是我的内容布局XML文件:
为了便于阅读,我对代码进行了压缩,同时也强调了我试图解决的问题
我是android新手,希望您能给予反馈。提前感谢。我使用了垂直方向的线性布局,并按预期工作,单击文本可见,我没有对您的活动进行任何更改,cardview的渐变依赖项是实现“androidx.cardview:cardview:1.0.0”
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openBay"
app:cardElevation="2dp"
app:cardCornerRadius="8dp"
>
<TextView
android:id="@+id/bayHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openEs"
app:cardElevation="2dp"
app:cardCornerRadius="8dp"
>
<TextView
android:id="@+id/esHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openThru"
app:cardElevation="2dp"
app:cardCornerRadius="8dp"
>
<TextView
android:id="@+id/thruHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah" />
</androidx.cardview.widget.CardView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openBay"
app:cardElevation="2dp"
app:cardCornerRadius="8dp">
<TextView
android:id="@+id/bayHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openEs"
app:cardElevation="2dp"
app:cardCornerRadius="8dp">
<TextView
android:id="@+id/esHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openThru"
app:cardElevation="2dp"
app:cardCornerRadius="8dp">
<TextView
android:id="@+id/thruHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah"/>
</androidx.cardview.widget.CardView>
</LinearLayout>