[코틀린을 활용한 안드로이드 프로그래밍] 7장 직접 풀어보기 7-2

2022. 4. 15. 03:28·안드로이드 프로그래밍/코틀린
반응형

 

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

 

<MainActivity>

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.cookandroid.project7_2
 
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ContextMenu
import android.view.MenuItem
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "배경색 바꾸기(컨텍스트 메뉴)"
 
        registerForContextMenu(button1)
        registerForContextMenu(button2)
 
    }
 
    override fun onCreateContextMenu(
        menu: ContextMenu?,
        v: View?,
        menuInfo: ContextMenu.ContextMenuInfo?
    ) {
        super.onCreateContextMenu(menu, v, menuInfo)
 
        if(v === button1){
            menu!!.setHeaderTitle("배경석 변경")
            menu!!.add(0, 1, 0, "배경색(빨강)")
            menu!!.add(0, 2, 0, "배경색(초록)")
            menu!!.add(0, 3, 0, "배경색(파랑)")
        }
        if(v === button2){
            menu!!.add(0, 4, 0, "버튼 45도 회전")
            menu!!.add(0, 5, 0, "버튼 2개 확대")
        }
    }
 
    override fun onContextItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            1 -> {
                baseLayout.setBackgroundColor(Color.RED)
            }
 
            2 -> {
                baseLayout.setBackgroundColor(Color.GREEN)
            }
 
            3 -> {
                baseLayout.setBackgroundColor(Color.BLUE)
            }
            4 ->{
                button2.rotation += 45f
            }
            5 ->{
                button2.scaleX += 2f
                button2.scaleY += 2f
            }
        }
        return super.onContextItemSelected(item)
    }
 
 
}
Colored by Color Scripter
cs

 

<activity_main.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ㅌ<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/baseLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="배경색 변경"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="버튼 변경"/>
 
</LinearLayout>
Colored by Color Scripter
cs

 

반응형
저작자표시 (새창열림)

'안드로이드 프로그래밍 > 코틀린' 카테고리의 다른 글

[코틀린을 활용한 안드로이드 프로그래밍] 10장 연습문제 5번  (0) 2022.04.15
[코틀린을 활용한 안드로이드 프로그래밍] 10장 직접 풀어보기 10-3  (0) 2022.04.15
[코틀린을 활용한 안드로이드 프로그래밍] 4장 직접 풀어보기 4-3  (0) 2022.04.15
[코틀린을 활용한 안드로이드 프로그래밍] 4장 직접 풀어보기 4-4  (0) 2022.04.15
[코틀린을 활용한 안드로이드 프로그래밍] 7장 직접 풀어보기 7-1  (0) 2022.04.15
'안드로이드 프로그래밍/코틀린' 카테고리의 다른 글
  • [코틀린을 활용한 안드로이드 프로그래밍] 10장 직접 풀어보기 10-3
  • [코틀린을 활용한 안드로이드 프로그래밍] 4장 직접 풀어보기 4-3
  • [코틀린을 활용한 안드로이드 프로그래밍] 4장 직접 풀어보기 4-4
  • [코틀린을 활용한 안드로이드 프로그래밍] 7장 직접 풀어보기 7-1
슥지니
슥지니
개발 블로그
  • 슥지니
    슥지니의 코딩노트
    슥지니
  • 전체
    오늘
    어제
    • 분류 전체보기 (199)
      • 알고리즘 문제풀이 (158)
        • 백준 (158)
      • 알고리즘 (6)
      • Node.js (2)
        • MongoDB (1)
        • 기타 (1)
      • spring (0)
      • 가상화폐 (1)
        • 바이낸스(Binance) (1)
      • C++ 테트리스 게임 (1)
      • C++ (10)
      • 안드로이드 프로그래밍 (21)
        • 코틀린 (21)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    dp
    C++
    다이나믹 프로그래밍
    Kotlin
    백트랙킹
    알고리즘
    백준
    자료구조
    구현
    콘솔
    dfs
    코틀린을 활용한 안드로이드 프로그래밍
    그리디
    C
    코틀린
    시뮬레이션
    그래프
    콘솔 테트리스 게임
    우선순위 큐
    BFS
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
슥지니
[코틀린을 활용한 안드로이드 프로그래밍] 7장 직접 풀어보기 7-2
상단으로

티스토리툴바