우리는 지금까지 xml에서 만든 여러가지 뷰,버튼들을 제어하기 위해 xml에 id를 이용하여
Activity에서
TextView tv;
tv=findViewByid(R.id.textview);
tv.setText("click");
이런 식으로 제어했다. 하지만 ButterKnife 라이브러리를 이용하면 좀더 간결하고 직관적이게 사용이 가능하다.
위 라이브러리의 버전 및 사용을 알수 있는 주소이다.
https://github.com/JakeWharton/butterknife
JakeWharton/butterknife
Bind Android views and callbacks to fields and methods. - JakeWharton/butterknife
github.com
예제는 Button을 누를 시 TextView의 글씨를 바꾸는 간단한 예제로 설명을 하겠다.
<실행 화면> findViewByid와 clickListener또는xml에서 Onclick을 사용하지 않고
butterKnife Library사용하여 쉽게 제어했습니다.
새로운 프로젝트를 만들어 주자.
임의의 이름으로 프로젝트를 만들었다.
dependencies{ }안에 추가 할 것.
1
|
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.1'
|
android{ }안에 추가 할 것.
1
2
3
4
|
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
|
1
2
|
implementation 'com.jakewharton:butterknife:10.2.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
|
라이브러리를 적용시켰다면 우선 코딩작업에 앞서 실행을 한번 하자.
( 그래야 문제가 발생했을 때 적용이 잘 안됐는지, 코드의 문제인지 찾기가 수월하다. )
activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
android:layout_marginBottom="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
tools:ignore="MissingConstraints" />
|
이제 MainActivity.java에서 작업하자.
(ButterKnife를 사용하려면 onCreate에 ButterKnife.bind(this); 추가 )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package com.example.ex_butterknife;
import android.os.Bundle;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tv) TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.btn) void clcik_btn(){
textview.setText("click!!");
}
}
|
<실행 화면> findViewByid와 clickListener또는xml에서 Onclick을 사용하지 않고
butterKnife Library사용하여 쉽게 제어했습니다.
'안드로이드 > 개발자 일상' 카테고리의 다른 글
안드로이드 다른 모양 버튼(custom button) 만들기 (0) | 2020.03.15 |
---|---|
안드로이드 SharedPreferences 값 저장하고, 불러오기. (3) | 2020.03.12 |
안드로이드 xmlns:tools , tools: (0) | 2020.03.12 |
코드 작성 시 다양한 표기 방법 (카멜, 파스칼, 헝가리안, 스네이크) (1) | 2020.03.11 |
라이브러리 최신 버전 찾기 팁 (0) | 2020.03.09 |
댓글