티스토리 뷰

SWDesk/App

Kotlin + Dialog

bizmaker 2019. 12. 11. 21:59

코틀린 대화상자 테스트

 

 

<MainActivity.kt>

더보기
// Reference : https://blog.yena.io/studynote/2017/12/17/Android-Kotlin-Dialog.html
class MainActivity : AppCompatActivity() {

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

        mainBtn.setOnClickListener {
            val builder = AlertDialog.Builder(this)
            val dialogView = layoutInflater.inflate(R.layout.custom_dialog, null)
            val dialogText = dialogView.findViewById<EditText>(R.id.dialogEt)
            val dialogRatingBar = dialogView.findViewById<RatingBar>(R.id.dialogRb)

            builder.setView(dialogView)
                .setPositiveButton("확인") { dialogInterface, i ->
                    mainTv.text = dialogText.text.toString()
                    mainRb.rating = dialogRatingBar.rating
                    mainBtn.text = "Thank you!"
                    /* 확인일 때 main의 View의 값에 dialog View에 있는 값을 적용 */

                }
                .setNegativeButton("취소") { dialogInterface, i ->
                    /* 취소일 때 아무 액션이 없으므로 빈칸 */
                }
                .show()
        }
    }
}

 

<res/layout/custom_dialogue.xml>

더보기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dialogTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="24dp"
        android:text="다이얼로그 예제"
        android:textAlignment="center"
        android:textSize="18sp"
        android:textStyle="bold" />

    <RatingBar
        android:id="@+id/dialogRb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:numStars="5" />

    <EditText
        android:id="@+id/dialogEt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:ems="10"
        android:hint="텍스트를 입력해주세요."
        android:inputType="textPersonName" />
</LinearLayout>

 

 

<res/layout.activity_main.xml>

더보기
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id = "@+id/greeting"
        app:layout_constraintBottom_toTopOf="@id/mainLl"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/mainLl"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/greeting">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id = "@+id/mainTv" />
        <RatingBar
            android:id="@+id/mainRb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:numStars="5" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="What's your score?"
            android:id = "@+id/mainBtn" />

    </LinearLayout>



</androidx.constraintlayout.widget.ConstraintLayout>

 

반응형

'SWDesk > App' 카테고리의 다른 글

Kotlin + RecyclerView 테스트 예제  (2) 2019.12.17
[BPM] BPM User Tool, Ver.1.0  (0) 2019.12.14
Dynamic Table Creation Test02  (0) 2019.12.11
Dynamic Table Creation Test02 - Wrong Code  (0) 2019.12.09
Dynamic Table Creation Test01  (0) 2019.12.09