猛禽洛的程式筆記庫

[Android] 自訂Dialog UI

XML布局檔dialog_project_info.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:id="@+id/main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".ui.activity.CreateProjectInfoActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:background="#03A9F4"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_project_info_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/edit_project_info"
        android:textColor="#FFFFFF"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="@+id/toolbar"
        app:layout_constraintEnd_toEndOf="@+id/toolbar"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:fillViewport="true"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        app:layout_constraintBottom_toTopOf="@+id/btn_project_info_save"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/imageView9"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:adjustViewBounds="true"
                    android:padding="12dp"
                    app:srcCompat="@drawable/icon_user" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView6"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:gravity="center_vertical"
                    android:text="@string/subject"
                    android:textSize="20sp" />

            </LinearLayout>

            <View
                android:id="@+id/divider"
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="?android:attr/listDivider" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <EditText
                    android:id="@+id/et_project_info_name"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:ems="10"
                    android:gravity="center"
                    android:hint="@string/anonymous_name"
                    android:inputType="text"
                    android:maxLength="30" />
          
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/btn_project_info_save"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="32dp"
        android:backgroundTint="#03A9F4"
        android:text="@string/save"
        android:textSize="20sp"
        app:cornerRadius="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

以上會有一個能修改名稱的欄位。

 

在來建立Dialog的Class  EditProjectInfoDialog.kt:

class EditProjectInfoDialog(context: Context): Dialog(context){

    private lateinit var binding: DialogProjectInfoBinding
    //這個是一個dataclass,裡面包入了姓名name等資訊
    private var jsonProjectInfo: JsonProjectInfo? = null
    //讓外部呼叫時回傳事件用
    private var saveListener: IOnSaveListener? = null
    //可以在初始話Dialog時,帶入畫面上要顯示的資料
    fun setInfo(jsonProjectInfo: JsonProjectInfo): EditProjectInfoDialog {
        this.jsonProjectInfo = jsonProjectInfo
        return this
    }
    //按下儲存按鈕後觸發的事件
    fun setSave(Listener: IOnSaveListener): EditProjectInfoDialog {
        this.saveListener = Listener
        return this
    }
    //建立畫面
    override fun onCreate(savedInstanceState: Bundle?) {
        binding = DialogProjectInfoBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        //如果有傳入初始化資料,則顯示
        jsonProjectInfo?.let {
            binding.etProjectInfoName.setText(it.name)
        }
        //把按鈕事件導到自訂的clickListener
        binding.btnProjectInfoSave.setOnClickListener(this::clickListener)
    }
    //按鈕事件監聽,並且連jsonProjectInfo的資訊一起帶出(修改後的資料)
    private fun clickListener(v: View){
        saveListener?.onSave(this, jsonProjectInfo!!)
    }
    //傳出介面
    interface IOnSaveListener {
        fun onSave(dialog: EditProjectInfoDialog?, newJsonProjectInfo: JsonProjectInfo)
    }
}

 

之後就可以當一般的Dialog呼叫使用:

val editProjectInfoDialog = EditProjectInfoDialog(this@ProjectListActivity)
//帶入要初始話的資料
editProjectInfoDialog.setInfo(projectInfoList[position])
//按鈕事件監聽介面
editProjectInfoDialog.setSave(object : EditProjectInfoDialog.IOnSaveListener{
  override fun onSave(dialog: EditProjectInfoDialog?, newJsonProjectInfo: JsonProjectInfo) {
            //這邊就可以接收到按下按鈕後的事件,跟回傳的資料
  }

})
//記得show
editProjectInfoDialog.show()

 

-END-

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *