1. TextView
1) TextView의 기본 주요 속성
- text : 보여줄 문자열
- lines : 문자열의 라인수
- textColor : 표시되는 문자열 색상
- textSize : 표시되는 문자열 크기
- textAppearance : 표시되는 문자열 형식
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:lines="2"
android:text="helloword"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="#AC3838"
android:textSize="36sp" />
2) 코틀린에서 TextView 속성 통제하는법
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
`````````
//val textView = findViewById<TextView>(R.id.textView) // 이 과정이 kotlin에서는 필요가 없음.
textView.text = "안녕하세요"
`````````
}
3) 주로 사용하는 메서드
- setBackgroundColor : 배경 색상을 지정하는 메서드
- setTextColor : 글자 색상을 지정하는 메서드
- append : 문자열 추가 메서드
``````
textView.setBackgroundColor(Color.WHITE)
textView.setTextColor(Color.BLUE)
textView.append(" , 뱅커님.")
``````
2. Button
1) Button의 기본 주요 속성
- text : Button의 문자열을 설정
- srcCompat : ImageButton의 이미지 설정
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="텍스트 버튼" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher" />
2)Button 의 이벤트 구현 방법
- onClick 이벤트 구현
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
```````````
button.setOnClickListener(textBtnListener)
imageButton.setOnClickListener(imgBtnListener)
```````````
}
```````````````
val textBtnListener = object : View.OnClickListener {
override fun onClick(v: View?) {
textView.text = "text button click"
}
}
val imgBtnListener = object : View.OnClickListener {
override fun onClick(v: View?) {
textView.text = "image button click"
}
}
````````````````
3) 같은 리스너 내에서 when 절로 btn 동작 구분하기
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
``````````````````
button2.setOnClickListener(togetherBtnListener)
button3.setOnClickListener(togetherBtnListener)
`````````````````
}
`````````````````````
val togetherBtnListener = object : View.OnClickListener {
override fun onClick(v: View?) {
when(v?.id) {
R.id.button2 -> {
textView.text = "같은 리스너 쓰기 1";
}
R.id.button3 -> {
textView.text = "같은 리스너 쓰기 2";
}
}
}
}
``````````````````````
4) Lambda 이용해서 Listener 바로 구현하기
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
```````````````````````````
lambdaBtn.setOnClickListener{
textView.text = "람다 함수 써서 listener 바로 구현하기"
}
````````````````````````````
3.EditText
1) EditText 주요 속성
- text : EditText 에 표시할 문자열을 설정
- hint : 입력된 값이 없을때 안내할 문구 표시
- inputType : 입력 될 값의 타입을 결정
- imeOptions : 키보드의 enter 모양
2) 코틀린에서 EditText 사용 하는 법
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="ex) 홍길동"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
textView.text = editText.text
// 키보드 내려가게하고 Focus 도 사라지게
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(editText.windowToken,0)
editText.clearFocus()
}
}
3) 사용자 입력 문자열 감시하기
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
``````````````````
editText.addTextChangedListener(watcherListener)
`````````````````
}
val watcherListener = object : TextWatcher {
// 문자열 변경 전
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
textView1.text = " 입력 전 문자열 : $s"
}
// 문자열 변경 직후
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
textView2.text = "변경 직후 문자열 : $s"
}
// 문자열 변경 완료 ( 화면에 반영)
override fun afterTextChanged(s: Editable?) {
textView3.text = "화면 반영 문자열 : $s"
}
}
4) 엔터 입력시 처리 방법
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
```````````````````
editText.setOnEditorActionListener { v, actionId, event ->
textView1.text = "엔터 입력시 동작"
false // 처리 후 키보드 내려감 , true 면 안내려감
}
``````````````````
}
반응형
'Programming > Android' 카테고리의 다른 글
ConstaintLayout 내용 정리 (0) | 2021.07.19 |
---|---|
[Android] SharedPreferences 내 입맛대로 사용하기. (0) | 2019.10.20 |
[Android] Android Studio에서 aar 사용하기. (1) | 2019.10.16 |
댓글