Android 주석 ButterKnife 의 기본 사용
3564 단어 butterknife쓰다
우선,
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 의 기본 적 인 사용 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 면 메 시 지 를 남 겨 주세요.편집장 은 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
butterknife 오류 메 서 드"value"를 찾 을 수 없습니다 심 볼 을 해결 할 수 없습니다프로젝트 에서 butterknife 주 해 를 사용 할 때 이러한 오류 가 발생 하 는 것 은 일반적으로 가이드 백 이 없 기 때 문 입 니 다. 이때 우 리 는 alt 키 와 enter 키 를 누 르 고 있 었 다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.