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

后端 未结 17 2503
野趣味
野趣味 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:07

    Note that if you want to use the onClick XML feature, the corresponding method should have one parameter, whose type should match the XML object.

    For example, a button will be linked to your method through its name string : android:onClick="MyFancyMethod" but the method declaration should show: ...MyFancyMethod(View v) {...

    If you are trying to add this feature to a menu item, it will have the exact same syntax in the XML file but your method will be declared as: ...MyFancyMethod(MenuItem mi) {...

提交回复
热议问题