Android 주석 ButterKnife 의 기본 사용

3564 단어 butterknife쓰다
ButterKnife 의 최신 버 전 은 8.4.0 이다.
우선,ButterKnife의 jar 가방 을 가 져 와 야 합 니 다.
AndroidStudio 에서File->Project Structure->Dependencies->Library dependency butterknife 를 검색 하면 됩 니 다.첫 번 째 는...
다른 하 나 는 build:grade(app)dependencies 에 직접 추가 하 는 것 입 니 다.

compile 'com.jakewharton:butterknife:8.4.0' 
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' 
ok,지금부터 본 격 적 으로 사용 하 세 요.용법 도 간단 합 니 다.
Activity 하위 클래스onCreate()방법 에서 사용ButterKnife.bind(this);하면 됩 니 다.

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ButterKnife.bind(this); 
    tv1.setText("hi!sy") 
메모:반드시 set ContentView 뒤에 써 야 합 니 다.
그리고 R.layot.activity 에 커서 를 놓 으 세 요.main 에서 마우스 우 클릭 으로 Generate...(Alt+Insert)를 선택 하면 다음 과 같이 표 시 됩 니 다.

그리고 이렇게.

TextView 클릭 이벤트 와 findView ById 주석 이 선택 되 어 있 습 니 다.Confirm 을 누 르 면 성공 합 니 다!
뭐?없다 고?서 두 르 지 마.작은 플러그 인 을 설치 해 야 해.AndroidStudio->File->Settings->Plugins->Zelezny 다운로드 추 가 를 검색 하면 해당 구성 요소 의 인 스 턴 스 대상 을 빠르게 생 성 할 수 있 습 니 다.수 동 으로 쓰 지 않 아 도 됩 니 다.
사용 시 주 해 를 가 져 올 Activity 나 Fragment 또는 ViewHolder 의 layot 자원 코드 에서 오른쪽 단 추 를 누 르 십시오.>Generate―Generate ButterKnife Injections.
소스 코드

public class MainActivity extends Activity { 
   @BindView(R.id.tv_time) 
  TextView tvTime; 
  @BindView(R.id.activity_main) 
  RelativeLayout activityMain; 
  @BindView(R.id.tv_cal) 
  TextView tvCal; 
  @BindView(R.id.tv_date) 
  TextView tvDate; 
  Time time; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ButterKnife.bind(this); 
    tvTime.setText("Time "); 
    tvCal.setText("Calender "); 
    tvDate.setText("Date "); 
    initTime(); 
  } 
  private void initTime() { 
    time = new Time(); 
    time.setToNow(); 
  } 
  @OnClick({R.id.tv_cal, R.id.tv_date,R.id.tv_time}) 
  public void onClick(View view) { 
    switch (view.getId()) { 
      case R.id.tv_time://      
        String times = time.year + " " + time.month + " " + time.monthDay 
            + " " + time.hour + " " + time.minute + " " + time.second + " " 
            + ":        " + time.yearDay + " "; 
        Toast.makeText(this, Time.getCurrentTimezone() + times, Toast.LENGTH_SHORT).show(); 
        tvTime.setText(times); 
        break; 
      case R.id.tv_cal: 
        break; 
      case R.id.tv_date: 
        break; 
    } 
  } 
  @Override 
  protected void onDestroy() { 
    super.onDestroy(); 
//    Unbinder unbinder=ButterKnife.bind(this); 
//    unbinder.unbind(); 
    ButterKnife.bind(this).unbind(); 
  } 
} 
위 에서 말 한 것 은 편집장 이 소개 한 안 드 로 이 드 주해 ButterKnife 의 기본 적 인 사용 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 면 메 시 지 를 남 겨 주세요.편집장 은 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기