简体   繁体   English

Android 儿童点击事件?

[英]Android child click events?

I am trying to make a toolbar with a few buttons on it in a custom linearlayout class called "ToolBarLayout".我正在尝试在名为“ToolBarLayout”的自定义线性布局 class 中制作一个带有几个按钮的工具栏。

When i instance this i want to be able to add a custom listener to it so i can switch which button index was pressed in the main activity.当我实例化这个时,我希望能够向它添加一个自定义侦听器,这样我就可以切换在主要活动中按下了哪个按钮索引。 Unfortunately i do not know how to pass the button's click events to the parent.不幸的是,我不知道如何将按钮的点击事件传递给父级。

I have tried adding onclick to the toolbar in the hope that the view returned will be the actual view ID clicked, but it is always the parent.我尝试将 onclick 添加到工具栏,希望返回的视图将是单击的实际视图 ID,但它始终是父视图。

Or perhaps there is another way?或者也许还有另一种方式? Anyone know of any examples like this?有人知道这样的例子吗?

Inflate the layout.After that, using findViewById(..) you can instantiate each button in separete view and add onClickListener().膨胀布局。之后,使用 findViewById(..) 您可以在单独的视图中实例化每个按钮并添加 onClickListener()。

PS附言


LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View parentView = mInflater.inflate(R.layout.your_layout, parent, false);

Button btn = (Button)parentView.findViewById(R.id.button);
btn.setOnClickListener(...);

Something like that... :)类似的东西... :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM