I have a layout which contains some views. I want to set some actions when a user clicks anywhere in the layout, so I have set an OnClickListener for the layout which works clear. But how can I know which view was clicked? I want to assign different actions depends on the view which was clicked; or maybe no view was clicked and it was only the layout itself. So the problem is if I set OnClickListener for one of the views, both OnCLickListeners related to the layout and the view will get activated, but I want only the view action.Thanks in advance.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Assign the id to each of your views by android:id="@+id/v1" then in
check for the view like
Other answers are quite abstract and some are incorrect. You can't Override onClick method for an Activity as it doesn't have one (unless of course you implement an OnClickListener).
Furthermore, if you set the listener for the layout the
View
argument received by the onClick method will always be the layout.You can either create your own custom layout that intercepts the clicks and check if you clicked on a view, or you can set listeners for all the views.
A somewhat cleaner solution is using a single listener and checking if it's a view that was clicked.
If you want different actions for different views, just create a listener for each of them.
Ok, it's easy.
You have to add an OnClickListener to each view (button, textView and so on), and to the whole layout as well.
In this case, the onClickListeners that will be launched if you click are the view's onClickListener and the layout's onClickListener.
So no need for a whole bunch of classes.
One will be enough if you try this (do actions depending on the id and don't forget the layout ;) ):
this onClickListener is for all of your views ;)
Remember that the method OnClick have one parameter that indicates which View is clicked!
for anyone interested you can also log the currently clicked view: