我在 xml 布局文件中声明了许多按钮,并将此布局用于片段。
按钮 android:id="@+id/button1" android:onClick="onClickButton"/> 按钮 android:id="@+id/button2" android:onClick="onClickButton"/> …… 按钮 android:id="@+id/buttonN" android:onClick="onClickButton"/>
令人失望的是,“onClickButton”事件在主机活动中被捕获,而不是在我的片段中。当然,在我的片段中,我可以手动解决这个问题,如下所示:
@覆盖
public View onCreateView(LayoutInflater inflater, ViewGroup 容器,
捆绑保存的InstanceState){
查看视图 = inflater.inflate(R.layout.fragment_content, container, false);
按钮 button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener(...);
... ETC
按钮 buttonN = (Button) view.findViewById(R.id.buttonN);
buttonN.setOnClickListener(...);
是否有任何明确的方法来处理片段中的 onClickbutton 事件?