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

2022. 4. 22. 01:43·안드로이드 프로그래밍/코틀린
반응형

[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
package com.cookandroid.project7_3
 
import android.content.res.Resources
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.dialog1.view.*
import kotlinx.android.synthetic.main.toast1.*
import kotlinx.android.synthetic.main.toast1.view.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        title = "사용자 정보 입력"
 
        button1.setOnClickListener {
            var dialogView = View.inflate(this@MainActivity, R.layout.dialog1, null)
            var dlg = AlertDialog.Builder(this@MainActivity)
            dialogView.dlgEdt1.text = tvName.text
            dialogView.dlgEdt2.text = tvEmail.text
            dlg.setTitle("사용자 정보 입력")
            dlg.setIcon(R.drawable.ic_menu_allfriends)
            dlg.setView(dialogView)
            dlg.setPositiveButton("확인"){dialog, which->
                tvName.text = dialogView.dlgEdt1.text
                tvEmail.text = dialogView.dlgEdt2.text
            }
            dlg.setNegativeButton("취소"){dialog, which->
                var toast = Toast(this@MainActivity)
                var toastView = View.inflate(this@MainActivity, R.layout.toast1, null)
                toastView.toastText1.text = "취소했습니다"
                toast.view = toastView
                var xOffset = (Math.random() * Resources.getSystem().displayMetrics.widthPixels).toInt()
                var yOffset = (Math.random() * Resources.getSystem().displayMetrics.heightPixels).toInt()
                toast.setGravity(Gravity.TOP or Gravity.LEFT,xOffset, yOffset)
                toast.show()
 
            }
            dlg.show()
        }
    }
}
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
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
 
    <EditText
        android:id="@+id/tvName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="사용자 이름"/>
    <EditText
        android:id="@+id/tvEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="이메일"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:hint="여기를 클릭"/>
 
 
 
 
 
</LinearLayout>
Colored by Color Scripter
cs

 

<dialog1.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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="사용자 이름"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/dlgEdt1"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="이메일"/>
 
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/dlgEdt2"/>
 
</LinearLayout>
Colored by Color Scripter
cs

 

 

<toast1.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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#ff0000"
    android:gravity="center">
 
 
 
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_star_big_on"/>
 
   <TextView
       android:id="@+id/toastText1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="TextView"
       android:textSize="20dp"/>
 
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_star_big_on"/>
 
 
</LinearLayout>
Colored by Color Scripter
cs
 
반응형
저작자표시 (새창열림)

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

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

    • 홈
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바