大佬教程收集整理的这篇文章主要介绍了如何让click事件传递给android中的容器?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我怎样才能做到这一点?
这是我的一些布局,我总共有5行(这里只显示1行).
如上所述,每行有5个FrameLayouts,每个包含一个TextView.我无法将OnClickListener放在TextView上,因为这可能会在运行时更改为ImageView.因此我想在FrameLayout上使用OnClickListener.
似乎FrameLayout的内容阻止它检测到click事件.
<TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/gateContainer" android:stretchcolumns="*"> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center"> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/door1" android:clickable="true"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="New Text" android:id="@+id/textView" android:layout_gravity="center" /> </FrameLayout> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/door2" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="New Text" android:id="@+id/textView5" android:layout_gravity="center" /> </FrameLayout> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/door3" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="New Text" android:id="@+id/textView4" android:layout_gravity="center"/> </FrameLayout> </TableLayout>
View.onClickListener clickListener = new View.onClickListener() { @Override public void onClick(View v){ // do something } }; TableLayout tableLayout = (TableLayout) findViewById(R.id.gateContainer); // Go through all TableRows for (int i = 0; i < tableLayout.getChildCount(); i++){ TableRow tableRow = (TableRow) tableLayout.getChildAt(i); // Set listener for each FrameView for (int j = 0; j < tableRow.getChildCount(); j++){ FrameLayout frame = (FrameLayout) tableRow.getChildAt(j); frame.setOnClickListener(clickListener); } }
以上是大佬教程为你收集整理的如何让click事件传递给android中的容器?全部内容,希望文章能够帮你解决如何让click事件传递给android中的容器?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。