반응형

[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()
        }
    }
}
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>
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>
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>
cs
 
반응형

+ Recent posts