Android自定义alertDialog和对话框中的点击事件

需求说明:

在界面底部弹出一个对话框,其中有四个按钮,可分别点击不同的按钮。

界面如下:


布局文件:dialog_menu.xml

我这里其实并不是点击的按钮,点击的是盛放图片和下方文字的Layout,所以要先将RelativeLayout设置

android:clickable="true"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0.9"
    android:background="@color/gray"
    android:orientation="vertical">

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

        <LinearLayout
            android:id="@+id/ll_up"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/ten"
            android:orientation="horizontal">

            <RelativeLayout
                android:id="@+id/rl_explain"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:clickable="true">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true">

                    <ImageView
                        android:id="@+id/img_explain"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:background="@android:color/white"
                        android:src="@mipmap/notebook" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/img_explain"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="@dimen/five"
                        android:text="@string/explain"
                        android:textColor="@android:color/black" />
                </RelativeLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/rl_light_screen"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/gray"
                android:clickable="true">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true">

                    <ImageView
                        android:id="@+id/img_light"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:background="@android:color/white"
                        android:src="@mipmap/light" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/img_light"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="@dimen/five"
                        android:text="@string/light_screen"
                        android:textColor="@android:color/black" />
                </RelativeLayout>
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_down"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/ten"
            android:orientation="horizontal"
            android:layout_marginBottom="@dimen/ten">

            <RelativeLayout
                android:id="@+id/rl_setting"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:clickable="true">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true">

                    <ImageView
                        android:id="@+id/img_setting"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:background="@android:color/white"
                        android:src="@mipmap/setting" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/img_setting"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="@dimen/five"
                        android:text="@string/setting"
                        android:textColor="@android:color/black" />
                </RelativeLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/rl_exit"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/gray"
                android:clickable="true">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true">

                    <ImageView
                        android:id="@+id/img_exit"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:background="@android:color/white"
                        android:src="@mipmap/exit" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/img_exit"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="@dimen/five"
                        android:text="@string/exit"
                        android:textColor="@android:color/black" />
                </RelativeLayout>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

MenuDialog的style:res/values/styles.xml

<resources>
<style name="MenuDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item><!--边框-->
        <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
        <item name="android:windowIsTranslucent">true</item><!--半透明-->
        <item name="android:windowNoTitle">true</item><!--无标题-->
        <item name="android:windowBackground">@android:color/transparent</item><!--背景透明-->
        <item name="android:backgroundDimEnabled">false</item><!--模糊-->
        <item name="android:gravity">bottom</item>
        <item name="android:layout_gravity">bottom</item>
    </style>
</resources>


在MainActivity中弹出对话框

因为对话框是在activity上显示的,所以对话框上的按钮不能直接findViewById,这样是找不到的,需先获得dialog的window,再通过window去findViewById

private void showMenuDialog() {
        //dialog 不能用Application的Context
        AlertDialog menuDialog = new AlertDialog.Builder(MainActivity.this, R.style.MenuDialog).create();
        menuDialog.show();
        Window window = menuDialog.getWindow();
        window.setContentView(R.layout.dialog_menu);
        window.setGravity(Gravity.BOTTOM); //设置对话框在界面底部显示
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = config.getScreenWidth();  //设置对话框的宽度为屏幕宽 (此处得到的是我一开始获得并存放起来的屏幕宽)
        window.setAttributes(params);//此句代码一定要放在show()后面,否则不起作用
        menuDialog.setCanceledOnTouchOutside(true);
        rl_explain = (RelativeLayout) window.findViewById(R.id.rl_explain); 
        rl_exit = (RelativeLayout) window.findViewById(R.id.rl_exit);
        rl_light_screen = (RelativeLayout) window.findViewById(R.id.rl_light_screen);
        rl_setting = (RelativeLayout) window.findViewById(R.id.rl_setting);
        rl_explain.setOnClickListener(this);
        rl_light_screen.setOnClickListener(this);
        rl_setting.setOnClickListener(this);
        rl_exit.setOnClickListener(this);
    }
最后具体事件的处理就和activity的onClick事件一样


以上是通过自定义AlertDialog的方式实现的点击对话框内的按钮的。

还可以通过将Activity设置成对话框形式再处理点击事件

布局文件不变。

style也可以不变。

将之前showMenuDialog()的操作放到新的Activity中(MenuDialogActivity.java),就不用再使用AlertDialog了,直接把布局文件setContentView(R.layout.dialog_menu);这样就可以直接findViewById了,因为点击事件都是在一个新的Activity中了。

注意在AndroidManifest.xml中设置该Activity的样式:

 <activity android:name=".activity.MenuDialogActivity"
            android:theme="@style/MenuDialog"/>




  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值