Android自定义Dialog及其点击事件

在项目开发中,经常要用到dialog。但是系统的dialog太丑,所有我们要自定义dialog。下面的先介绍自定义dialog的方法,然后对自定义dialog里的控件的点击事件进行响应。

自定义dialog java代码:

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder
                .setView(R.layout.login_out_dialog) //自定义的布局文件
                .create();
        dialog.show();

自定义dialog的布局文件 login_out_dialog.xml

<?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="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:background="@drawable/login_out_dialog_bg"
    android:gravity="center"
    android:orientation="vertical">

    <!--提示退出的对话框-->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hint"
        android:textColor="@color/black"
        android:textSize="14sp"
        android:layout_marginTop="16dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="@string/confirm_login_out"
        android:textColor="@color/black"
        android:textSize="12sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="16dp"
        android:background="@color/travel_around_divider" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center">
        <!--取消-->
        <TextView
            android:id="@+id/cancel_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/cancel"
            android:textColor="#56aff7"
            android:textSize="16sp"
            android:gravity="center"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:layout_marginStart="16dp"/>

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/travel_around_divider" />

        <!--退出-->
        <TextView
            android:id="@+id/login_out_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/login_out"
            android:textColor="#56aff7"
            android:textSize="16sp"
            android:gravity="center"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:layout_marginStart="16dp"/>
    </LinearLayout>

</LinearLayout>

效果图

退出登录.PNG

有没有变得漂亮一些,呵呵

最后就是,点击事件
直接findviewbyid是没有用的。下面是点击事件的方法:

 dialog.getWindow().findViewById(R.id.cancel_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss(); //取消对话框
            }
        });
        dialog.getWindow().findViewById(R.id.login_out_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出登录
                mPresenter.exitLogin(UserInfoActivity.this);
                dialog.dismiss(); //取消对话框
                finish();
            }
        });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,569评论 0 17
  • Day1: 在代码中通过R.string.hello_world可以获得该字符串的引用; 在XML中通过@stri...
    冰凝雪国阅读 1,457评论 0 5
  • 都说处女座很强迫症,事情规划的仅仅有条,规矩立的条条框框,甚至有极端者让人无法忍受。而我刚好走了相反的极端,电脑桌...
    小小_dijiu阅读 179评论 0 2
  • 中国有句俗话,叫做“酒香不怕巷子深”,大概就是“是金子总会发光”这种意思,不过在如今浩瀚如烟的开源项目里面,即使真...
    siddontang阅读 8,563评论 8 17