How exactly does the android:onClick XML attribute differ from setOnClickListener?

后端 未结 17 2114
野趣味
野趣味 2020-11-21 11:50

From that I\'ve read you can assign a onClick handler to a button in two ways.

Using the android:onClick XML attribute where you just use t

17条回答
  •  温柔的废话
    2020-11-21 12:24

    By using the XML attribute you just need to define a method instead of a class so I was wondering if the same can be done via code and not in the XML layout.

    Yes, You can make your fragment or activity implement View.OnClickListener

    and when you initialize your new view objects in code you can simply do mView.setOnClickListener(this);

    and this automatically sets all view objects in code to use the onClick(View v) method that your fragment or activity etc has.

    to distinguish which view has called the onClick method, you can use a switch statement on the v.getId() method.

    This answer is different from the one that says "No that is not possible via code"

提交回复
热议问题