Android自定义对话框的简单实现

本文实例为大家分享了Android自定义对话框的具体实现代码,供大家参考,具体内容如下

1、定义对话框的布局

<?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">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="16sp"
        android:layout_margin="4dp"
        android:text="标题"/>
    <TextView
        android:id="@+id/content1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="第一行文字"
        android:layout_margin="4dp"
        android:layout_below="@id/title"
        android:gravity="center"/>
    <TextView
        android:id="@+id/content2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="第一行文字"
        android:layout_margin="4dp"
        android:layout_below="@id/content1"
        android:gravity="center"/>

    <LinearLayout
        android:id="@+id/linear"
        android:layout_below="@id/content2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="6dp"
        android:paddingRight="20dp"
        android:paddingLeft="20dp"
        >
        <Button
            android:id="@+id/ok"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="14sp"
            android:text="确定"/>
        <Button
            android:id="@+id/cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="14sp"
            android:text="取消"/>
    </LinearLayout>

    <Button
        android:id="@+id/know"
        android:layout_below="@id/linear"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="知道了"/>

</RelativeLayout>

对话框样式(比较丑哈,就是大概这个意思,嘿嘿)

2、定义接口

利用接口回调的方式使对话框消失。

public interface DialogListener {
    void onClick(MyDialog dialog,View view);
}

3、写一个类继承Dialog,并重写构造方法

说明:第三个按钮的监听与其他两个不同,前两个使用的是button原声的监听事件,第三个为自定义的接口,目的是获取MyDialog,然后通过dismiss()方法使对话框不显示。(接口回调的方式)

public class MyDialog extends Dialog {
    private TextView mTipOneView;
    private TextView mTipTwoView;
    private TextView mTitleView;
    private Button mOkView;
    private Button mCancelView;
    private Button mKonwView;

    private View.OnClickListener mOkListener;
    private View.OnClickListener mCancelListener;
    private DialogListener mKnowListener;

    private String title;
    private String oneTip;
    private String twoTip;

    private void setOnDialogListener(DialogListener listener){
        this.mKnowListener = listener;
    }

    public MyDialog(Context context) {
        super(context);
    }
    public MyDialog(Context context,String title,String oneTip,String twoTip,View.OnClickListener ok,View.OnClickListener cancel,DialogListener know) {
        this(context);
        this.title = title;
        this.oneTip = oneTip;
        this.twoTip = twoTip;
        mOkListener = ok;
        mCancelListener = cancel;
        mKnowListener = know;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog);
        mCancelView = (Button) findViewById(R.id.cancel);
        mOkView = (Button) findViewById(R.id.ok);
        mKonwView = (Button) findViewById(R.id.know);
        mTipOneView = (TextView) findViewById(R.id.content1);
        mTipTwoView = (TextView) findViewById(R.id.content2);
        mTitleView = (TextView) findViewById(R.id.title);

        mTitleView.setText(title);
        mTipTwoView.setText(twoTip);
        mTipOneView.setText(oneTip);
        mCancelView.setOnClickListener(mCancelListener);
        mOkView.setOnClickListener(mOkListener);
        mKonwView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mKnowListener.onClick(MyDialog.this,view);
            }
        });
    }
}

通过setViewContent(R.layout.~)为对话框设置样式;使用构造方法传值。

4、显示对话框

public class CustomDialogActivity extends AppCompatActivity {

    private DialogListener listener;
    private MyDialog myDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_dialog);

        listener = new DialogListener() {
            @Override
            public void onClick(MyDialog dialog, View view) {
                myDialog.dismiss();
            }
        };
    }

    public void showDialog(View view){
         myDialog = new MyDialog(CustomDialogActivity.this, "不知道", "有问题么", "啥问题", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("----->", "ok");
                //点击按钮发生的事件
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("----->", "cancle");
                //点击按钮发生的事件
            }
        },listener);

        myDialog.show();

    }
}

注意:一定不要忘了show(),否则对话框不显示。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android仿QQ消息提示实现弹出式对话框

    本文在<7种形式的Android Dialog使用实例>在这篇文章的基础进行学习,具体内容如下 1.概述 android原生控件向来以丑著称(新推出的Material Design当另说),因此几乎所有的应用都会特殊定制自己的UI样式.而其中弹出式提示框的定制尤为常见,本篇我们将从模仿QQ退出提示框来看一下常见的几种自定义提示框的实现方式. 这里使用的几种弹出框实现方法概括为以下几种: 自定义Dialog 自定义PopupWindow 自定义Layout View Activity的Dialo

  • 详解Android 全局弹出对话框SYSTEM_ALERT_WINDOW权限

    项目中为了实现账号多设备登录的监听 一个账号在别的设备登录时在该设备上需要弹出对话框提示 故而用到全局对话框 方案一. 1.在开发中有时会用到全局弹出对话框但必须在manifest中申请权限: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 2.创建Dialog AlertDialog.Builder builder=new AlertDialog.Builder(this)

  • 实例详解Android自定义ProgressDialog进度条对话框的实现

    Android SDK已经提供有进度条组件ProgressDialog组件,但用的时候我们会发现可能风格与我们应用的整体风格不太搭配,而且ProgressDialog的可定制行也不太强,这时就需要我们自定义实现一个ProgressDialog. 通过看源码我们发现,ProgressDialog继承自Alertdialog,有一个ProgressBar和两个TextView组成的,通过对ProgressDialog的源码进行改进就可以实现一个自定义的ProgressDialog. 1.效果: 首先

  • Android自定义对话框Dialog的简单实现

    本文着重研究了自定义对话框,通过一下步骤即可清晰的理解原理,通过更改界面设置和style类型,可以应用在各种各样适合自己的App中. 首先来看一下效果图: 首先是activity的界面 点击了上述图片的按钮后,弹出对话框: 点击对话框的确定按钮: 点击对话框的取消按钮: 下面来说一下具体实现步骤: 第一步:设置Dialog的样式(一般项目都可以直接拿来用):style.xml中 <!--自定义Dialog背景全透明无边框theme--> <style name="MyDialo

  • Android实现退出界面弹出提示对话框

    根据之前学的Android对话框技术,来实现下面一个效果:界面有一个"退出"按钮,按下之后会弹出一个询问是否退出的提示对话框,单击"不"按钮,不退出游戏,单击"是的"按钮,将退出游戏. 接下来实现此实例: res/layout/main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="

  • Android中AlertDialog各种对话框的用法实例详解

    目标效果: 程序运行,显示图一的几个按钮,点击按钮分别显示图二到图六的对话框,点击对话框的某一项或者按钮,也会显示相应的吐司输出. 1.activity_main.xml页面存放五个按钮. activity_main.xml页面: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools&

  • Android实现点击AlertDialog上按钮时不关闭对话框的方法

    本文实例讲述了Android实现点击AlertDialog上按钮时不关闭对话框的方法.分享给大家供大家参考.具体如下: 开发过程中,有时候会有这样的需求: 点击某个按钮之后显示一个对话框,对话框上面有一个输入框,并且有"确认"和"取消"两个按钮.当用户点击确认按钮时,需要对输入框的内容进行判断.如果内容为空则不关闭对话框,并toast提示. 使用AlertDialog.Builder创建对话框时,可以使用builder.setNegativeButton和build

  • Android实现底部对话框BottomDialog弹出实例代码

    最近项目上需要实现一个底部对话框,要实现这样的功能其实很简单,先看代码: private void show1() { Dialog bottomDialog = new Dialog(this, R.style.BottomDialog); View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_normal, null); bottomDialog.setContentView(contentV

  • Android中自定义对话框(Dialog)的实例代码

    1.修改系统默认的Dialog样式(风格.主题) 2.自定义Dialog布局文件 3.可以自己封装一个类,继承自Dialog或者直接使用Dialog类来实现,为了方便以后重复使用,建议自己封装一个Dialog类 第一步: 我们知道Android定义个控件或View的样式都是通过定义其style来实现的,查看Android框架中的主题文件,在源码中的路径:/frameworks/base/core/res/res/values/themes.xml,我们可以看到,Android为Dialog定义了

  • Android 之BottomsheetDialogFragment仿抖音评论底部弹出对话框效果(实例代码)

    实现的效果图: 自定义Fragment继承BottomSheetDialogFragment 重写它的三个方法: onCreateDialog() onCreateView() onStart() 他们的执行顺序是从上到下 import android.app.Dialog; import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable;

随机推荐