简体   繁体   中英

Android child click events?

I am trying to make a toolbar with a few buttons on it in a custom linearlayout class called "ToolBarLayout".

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.

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().

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... :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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