Android Application

Kotlin 대화상자(Dialog) : 사용자 정보 입력 앱 만들기

작성자 임베디드코리아 작성일23-10-30 23:03 조회1,601회 댓글0건

첨부파일

Kotlin 대화상자(Dialog) : 사용자 정보 입력 앱 만들기
-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/tvName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="사용자 이름"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/tvEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="이메일"
        android:textSize="20dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="여기를 클릭" />
</LinearLayout>

-------------------------------------------------------------------------------------
*****      dialog1.xml **
--- res/layout 에서  ---> New  --> XML --> Layout XML File
--- Layout File : dialog1  ,  Root Tag : LinearLayout
-------------------------------------------------------------------------------------
<?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="사용자 이름"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/dlgEdt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이메일"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/dlgEdt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

-------------------------------------------------------------------------------------
*****      toast1.xml **
--- res/layout 에서  ---> New  --> XML --> Layout XML File
--- Layout File : toast1  ,  Root Tag : LinearLayout
-------------------------------------------------------------------------------------
<?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:background="#ff0000"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView1"
        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:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_star_big_on" />
</LinearLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    lateinit var tvName : TextView
    lateinit var tvEmail : TextView
    lateinit var button1 : Button
    lateinit var dlgEdtName : EditText
    lateinit var dlgEdtEmail : EditText
    lateinit var toastText : TextView
    lateinit var dialogView : View
    lateinit var toastView : View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        title = "사용자 정보"

        supportActionBar!!.setDisplayShowHomeEnabled(true)
        supportActionBar!!.setIcon(R.drawable.ic_launcher)

        tvName = findViewById<View>(R.id.tvName) as TextView
        tvEmail = findViewById<View>(R.id.tvEmail) as TextView
        button1 = findViewById<View>(R.id.button1) as Button

        tvName.setOnClickListener  {
            tvName.setText(null)
        }

        tvEmail.setOnClickListener  {
            tvEmail.setText(null)
        }

        button1.setOnClickListener {
            dialogView = View.inflate(this@MainActivity, R.layout.dialog1, null) as View

            val dlg = AlertDialog.Builder(this@MainActivity)
            dlg.setTitle("사용자 정보(입력)")
            dlg.setIcon(R.drawable.ic_menu_allfriends)
            dlg.setView(dialogView)
            dlgEdtName = dialogView.findViewById<View>(R.id.dlgEdt1) as EditText
            dlgEdtEmail = dialogView.findViewById<View>(R.id.dlgEdt2) as EditText
            dlgEdtName.setText(tvName.text.toString())
            dlgEdtEmail.setText(tvEmail.text.toString())

            dlg.setPositiveButton("확인") { dialog, which ->
                tvName.text = dlgEdtName.text.toString()
                tvEmail.text = dlgEdtEmail.text.toString()
            }

            dlg.setNegativeButton("취소") { dialog, which ->
                val toast = Toast(this@MainActivity)

                val display = (getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
                val xOffset = (Math.random() * display.width).toInt()
                val yOffset = (Math.random() * display.height).toInt()

                toast.setGravity(Gravity.TOP or Gravity.LEFT, xOffset, yOffset)

                toastView = View.inflate(this@MainActivity, R.layout.toast1, null) as View
                toastText = toastView.findViewById<View>(R.id.toastText1) as TextView
                toastText.text = "취소 했습니다."
                toast.view = toastView
                toast.show()
            }
            dlg.show()
        }

    }

}


------------------------------------------------------------------------------------
    themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.ToastAndDialog" parent="Theme.AppCompat.Light">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
    </style>

    <style name="Theme.ToastAndDialog" parent="Base.Theme.ToastAndDialog" />
</resources>