[英]How to click view in LinearLayout?
There are multiple selectable TextView
in the Dialog
. Dialog
中有多个可选择的TextView
。
I'm looking for a way to get text values
by selecting multiple views
in a LinearLayout
over and over.我正在寻找一种通过在
LinearLayout
中一遍又一遍地选择multiple views
来获取text values
的方法。
As the first method, I thought of how to set the click event after setting the variables of each view.作为第一种方法,我想到了在设置好每个视图的变量之后如何设置点击事件。 But this makes the code too long and messy.
但这使得代码太长太乱。
As a second method, I thought of using the RecyclerView
to place the view and set the click event.作为第二种方法,我想到了使用
RecyclerView
来放置视图并设置点击事件。 It sounds like a good method, but it seems inefficient because the number of items is so small that it makes no sense to use RecyclerView
.这听起来是个好方法,但似乎效率低下,因为项目数量太少,使用
RecyclerView
毫无意义。
How can I set up a click event that gets the values of the views in the LinearLayout
?如何设置单击事件以获取
LinearLayout
中的视图值?
XML XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_body_part"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/back"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text=" BACK "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="@+id/selection_state1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_dumbbell"
android:backgroundTint="@android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/chest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" CHEST "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="@+id/selection_state2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_dumbbell"
android:backgroundTint="@android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/leg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" LEG "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="@+id/selection_state3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_dumbbell"
android:backgroundTint="@android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/shoulder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" SHOULDER "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="@+id/selection_state4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_dumbbell"
android:backgroundTint="@android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/bieceps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" BICEPS "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="@+id/selection_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_dumbbell"
android:backgroundTint="@android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/triceps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" TRICEPS "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="@+id/selection_state6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_dumbbell"
android:backgroundTint="@android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/abs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" ABS "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="@+id/selection_state7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_dumbbell"
android:backgroundTint="@android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<Button
android:id="@+id/check"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="START"
android:textSize="27dp"
android:backgroundTint="@color/purple_200"/>
</LinearLayout>
This is the code I tried.这是我尝试过的代码。
class BodyPartDialogFragment : DialogFragment(), View.OnClickListener{
private lateinit var ll: LinearLayout
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_body_part_dialog, container, false)
ll = view.findViewById(R.id.ll_body_part)
ll.setOnClickListener {
onClick(it)
}
return view
}
override fun onClick(view: View?) {
when (view?.id) {
R.id.back -> Toast.makeText(context, "back", Toast.LENGTH_LONG)
R.id.chest -> Toast.makeText(context, "chest", Toast.LENGTH_SHORT)
R.id.leg -> Toast.makeText(context, "leg", Toast.LENGTH_SHORT)
R.id.shoulder -> Toast.makeText(context, "shoulder", Toast.LENGTH_SHORT)
R.id.bieceps -> Toast.makeText(context, "biceps", Toast.LENGTH_SHORT)
R.id.triceps -> Toast.makeText(context, "triceps", Toast.LENGTH_SHORT)
R.id.abs -> Toast.makeText(context, "abs", Toast.LENGTH_SHORT)
}
}
}
It seems that the entire LinearLayout
is selected似乎选择了整个
LinearLayout
Please tell me what to do请告诉我该怎么办
Should I just use Recycler View?我应该只使用 Recycler View 吗?
You are not clicking the views, you are clicking the linear layout您不是在单击视图,而是在单击线性布局
ll.setOnClickListener {
onClick(it) //it here is view, which in this case is the linear layout
}
So if you want to only select the TextView
and is always going to be a TextView
the view you need, you can iterate through them:因此,如果您只想 select 和
TextView
并且始终是TextView
您需要的视图,您可以遍历它们:
fun addClickListeners(viewGroup: ViewGroup) {
for (i in 0 until viewGroup.childCount) {
val candidate = viewGroup.childAt(i) as? TextView
if (candidate != null) {
candidate.setOnClickListener {
it.text.toString() //do something
}
} else {
(viewGroup.childAt(i) as? ViewGroup)?.let {
addClickListeners(it) //recursive for inner childs
}
}
}
}
Then call it like this然后像这样调用它
addClickListeners(ll)
Regarding if you should use or not a RecyclerView
that is up to you.关于您是否应该使用由您决定的
RecyclerView
。 If your evaluation is the requirement is low enough for trusting that is always gonna be a TextView
then do that.如果您的评估要求足够低,可以信任始终是
TextView
,那么就这样做。 If you think it can increase the scope then use a RecyclerView
.如果你认为它可以增加 scope 然后使用
RecyclerView
。 If you think there is gonna be a middle point where things grow but not that much, create a custom view.如果您认为会有一个中间点,事情会增长但不会增长那么多,请创建一个自定义视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.