Android-butterknife simple and practical

Source: Internet
Author: User

Android-butterknife simple and practical

Butterknife is a dependency injection framework, which saves us the findviewbyid () operation. We have been using the injection function in xutils all the time. This time, we have experienced this and I feel that there are still some differences.

Instructions for use:

@ Viewinject (r. id. xxx)

Textview xxx;

The textview cannot be private or static. Otherwise, an error is returned. This restriction is not found in xutils.

 

@ OnLongClick (R. id. xxx)

Boolean method ()

The method here cannot be private, and the return type must be boolean.

 

 

Test code:

1. Add dependency:

 

compile 'com.jakewharton:butterknife:6.1.0'

2. layout code:

 

 

     
  
   
  
 

3. activity Code:

 

 

public class MainActivity extends ActionBarActivity {    @InjectView(R.id.tv_hello)    TextView tv_hello;    @InjectView(R.id.bt_changetext)    Button bt_changetext;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.inject(this);//        bt_changetext.setOnClickListener(new View.OnClickListener() {//            @Override//            public void onClick(View v) {////            }//        });    }//    @OnClick(R.id.bt_changetext)//    void changeText() {//        tv_hello.setText("hello my dear graypn!");//    }    @OnLongClick(R.id.bt_changetext)    boolean changeTextByLongClick() {        tv_hello.setText("hello my dear graypn!");        return true;    }}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.