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

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

    Be careful, although android:onClick XML seems to be a convenient way to handle click, the setOnClickListener implementation do something additional than adding the onClickListener. Indeed, it put the view property clickable to true.

    While it's might not be a problem on most Android implementations, according to the phone constructor, button is always default to clickable = true but other constructors on some phone model might have a default clickable = false on non Button views.

    So setting the XML is not enough, you have to think all the time to add android:clickable="true" on non button, and if you have a device where the default is clickable = true and you forget even once to put this XML attribute, you won't notice the problem at runtime but will get the feedback on the market when it will be in the hands of your customers !

    In addition, we can never be sure about how proguard will obfuscate and rename XML attributes and class method, so not 100% safe that they will never have a bug one day.

    So if you never want to have trouble and never think about it, it's better to use setOnClickListener or libraries like ButterKnife with annotation @OnClick(R.id.button)

    0 讨论(0)
  • 2020-11-21 12:13
       Add Button in xml and give onclick attribute name that is the name of Method.
       <!--xml --!>
       <Button
      android:id="@+id/btn_register"
      android:layout_margin="1dp"
      android:onClick="addNumber"
      android:text="Add"
      />
    
    
        Button btnAdd = (Button) findViewById(R.id.mybutton); btnAdd.setOnClickListener(new View.OnClickListener() {
       @Override
        public void onClick(View v) {
          addNumber(v);
        }
        });
    
      Private void addNumber(View v){
      //Logic implement 
        switch (v.getId()) {
        case R.id.btnAdd :
            break;
         default:
            break;
        }}
    
    0 讨论(0)
  • 2020-11-21 12:14

    Another way to set your on click listeners would be to use XML. Just add android:onClick attribute to your tag.

    It is a good practice to use the xml attribute “onClick” over an anonymous Java class whenever possible.

    First of all, lets have a look at the difference in code:

    XML Attribute / onClick attribute

    XML portion

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1" 
        android:onClick="showToast"/>
    

    Java portion

    public void showToast(View v) {
        //Add some logic
    }
    

    Anonymous Java Class / setOnClickListener

    XML Portion

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    Java portion

    findViewById(R.id.button1).setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Add some logic
            }
    });
    

    Here are the benefits of using the XML attribute over an anonymous Java class:

    • With Anonymous Java class we always have to specify an id for our elements, but with XML attribute id can be omitted.
    • With Anonymous Java class we have to actively search for the element inside of the view (findViewById portion), but with the XML attribute Android does it for us.
    • Anonymous Java class requires at least 5 lines of code, as we can see, but with the XML attribute 3 lines of code is sufficient.
    • With Anonymous Java class we have to name of our method “onClick", but with the XML attribute we can add any name we want, which will dramatically help with the readability of our code.
    • Xml “onClick” attribute has been added by Google during the API level 4 release, which means that it is a bit more modern syntax and modern syntax is almost always better.

    Of course, it is not always possible to use the Xml attribute, here are the reasons why we wouldn’t chose it:

    • If we are working with fragments. onClick attribute can only be added to an activity, so if we have a fragment, we would have to use an anonymous class.
    • If we would like to move the onClick listener to a separate class (maybe if it is very complicated and/or we would like to re-use it in different parts of our application), then we wouldn’t want to use the xml attribute either.
    0 讨论(0)
  • 2020-11-21 12:15

    android:onClick is for API level 4 onwards, so if you're targeting < 1.6, then you can't use it.

    0 讨论(0)
  • 2020-11-21 12:16

    Check if you forgot to put the method public!

    0 讨论(0)
  • 2020-11-21 12:23

    To make your life easier and avoid the Anonymous Class in setOnClicklistener (), implement a View.OnClicklistener Interface as below:

    public class YourClass extends CommonActivity implements View.OnClickListener, ...

    this avoids:

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            yourMethod(v);
        }
    });
    

    and goes directly to:

    @Override
    public void onClick(View v) {
      switch (v.getId()) {
        case R.id.your_view:
          yourMethod();
          break;
      }
    }
    
    0 讨论(0)
提交回复
热议问题