创建dialog并添加点击事件

这篇博客展示了如何在Android中创建一个自定义对话框dialog_custom_image_selection.xml,包含相机和图库选项。通过DialogCustomImageSelectionBinding绑定视图,并设置点击事件,点击相机和图库按钮会显示Toast提示。点击按钮后,dialog会自动关闭。提供的代码示例详细解释了布局设计、dialog实例化、事件监听及dialog的显示和隐藏操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建dialog的布局dialog_custom_image_selection.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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/_10sdp"
    app:chainUseRtl="true">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_select_image_action"
        android:textColor="@color/grey_900"
        android:textSize="@dimen/_16sdp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@id/tv_camera"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:id="@+id/tv_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_30sdp"
        android:foreground="?attr/selectableItemBackgroundBorderless"
        android:gravity="center"
        android:text="@string/lbl_camera"
        app:drawableTopCompat="@drawable/ic_vector_photo_camera"
        android:textColor="@color/blue_grey_700"
        android:textSize="@dimen/_15sdp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/tv_gallery"
        app:layout_constraintTop_toBottomOf="@id/tv_title"
        />

    <TextView
        android:id="@+id/tv_gallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lbl_gallery"
        android:layout_margin="@dimen/_30sdp"
        android:foreground="?attr/selectableItemBackgroundBorderless"
        android:gravity="center"
        app:drawableTopCompat="@drawable/ic_vector_gallery"
        android:textColor="@color/blue_grey_700"
        android:textSize="@dimen/_15sdp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintLeft_toRightOf="@id/tv_camera"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_title"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

2、创建diaolog并显示

 val dialog = Dialog(this);
        val binding : DialogCustomImageSelectionBinding = DialogCustomImageSelectionBinding.inflate(layoutInflater)
        dialog.setContentView(binding.root)
        dialog.show()

3、运行结果:

4、设置view的点击事件

        binding.tvCamera.setOnClickListener {
            Toast.makeText(this,"Camera Clicked",Toast.LENGTH_SHORT).show()
        }

        binding.tvGallery.setOnClickListener {
            Toast.makeText(this,"Gallery Clicked",Toast.LENGTH_SHORT).show()
        }

5、 运行结果:

 

 

 

6、取消显示dialog

 dialog.dismiss()

注:

显示dialog的代码

  private fun customImageSelectionDialog(){
        val dialog = Dialog(this);
        val binding : DialogCustomImageSelectionBinding = DialogCustomImageSelectionBinding.inflate(layoutInflater)
        dialog.setContentView(binding.root)

        binding.tvCamera.setOnClickListener {
            Toast.makeText(this,"Camera Clicked",Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }

        binding.tvGallery.setOnClickListener {
            Toast.makeText(this,"Gallery Clicked",Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }

        dialog.show()
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值