Android自定义Dialog框样式

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

首先定义dialog的布局文件,buy_goods_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:background="#fff"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:text="购买数量"
            android:textColor="#000" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true">

            <Button
                android:id="@+id/button_reduce"
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:text="—" />

            <Button
                android:id="@+id/button_number"
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:text="1" />

            <Button
                android:id="@+id/button_plus"
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:text="+" />
        </LinearLayout>
    </RelativeLayout>

    <Button
        android:id="@+id/button_buyGoodsDialog_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/relativeLayout"
        android:text="确定" />
</LinearLayout>

接着是创建一个类继承Dialog写代码,BuyGoodsDialog.java如下:

package com.example.administrator.myapplication;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class BuyGoodsDialog extends Dialog {
    private Activity context;// 上下文对象

    private Button reduceButton;// “-”按钮
    private Button numberButton;// “1”按钮
    private Button plusButton;// “+”按钮
    private Button okButton;// “确定”按钮

    private View.OnClickListener mClickListener;// 确定按钮的事件监听器

    public BuyGoodsDialog(Activity context) {
        super(context);
        this.context = context;
    }

    public BuyGoodsDialog(Activity context, int theme, View.OnClickListener clickListener) {
        super(context, theme);
        this.context = context;
        this.mClickListener = clickListener;
    }

    public BuyGoodsDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected BuyGoodsDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 指定布局
        this.setContentView(R.layout.buy_goods_dialog);
        // 获取buy_goods_dialog布局中的控件
        reduceButton = (Button) findViewById(R.id.button_reduce);// 减号(-)按钮
        numberButton = (Button) findViewById(R.id.button_number);// 数字(1)按钮
        plusButton = (Button) findViewById(R.id.button_plus);// 加号(+)按钮
        okButton = (Button) findViewById(R.id.button_buyGoodsDialog_ok);// 确定按钮

        numberButton.setText("1");// 设置数字按钮初始值为1

        // 获取窗口对象
        Window dialogWindow = this.getWindow();
        // 窗口管理器
        WindowManager m = context.getWindowManager();
        // 获取屏幕宽、高用
        Display d = m.getDefaultDisplay();
        // 获取对话框当前的参数值
        WindowManager.LayoutParams p = dialogWindow.getAttributes();
        // 这里设置的宽高优先级高于XML中的布局设置
//        // 高度设置为屏幕的0.6
//        p.height = (int) (d.getHeight() * 0.6);
//        // 宽度设置为屏幕的0.8
//        p.width = (int) (d.getWidth() * 0.8);
        // 设置到属性配置中
        dialogWindow.setAttributes(p);

        // “+”号按钮的事件监听器,使数字按钮的值加1
        plusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numberButton.setText(String.valueOf(Integer.parseInt(numberButton.getText().toString()) + 1));
            }
        });
        // “-”号按钮的事件监听器,使数字按钮的值减1
        reduceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.parseInt(numberButton.getText().toString()) - 1;
                if (num <= 0) {
                    numberButton.setText("1");
                } else {
                    numberButton.setText(String.valueOf(num));
                }
            }
        });

        // 为确定按钮绑定点击事件监听器
        okButton.setOnClickListener(mClickListener);// 使用外部的
//        okButton.setOnClickListener(onClickListener);// 使用内部自定义的

        this.setCancelable(true);// 设置是否点击周围空白处可以取消该Dialog,true表示可以,false表示不可以
    }

    /**
     * 获取数字按钮的数字
     *
     * @return 返回数字
     */
    private String getCount() {
        return numberButton.getText().toString();
    }

    public View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(), "库存:" + getCount(), Toast.LENGTH_SHORT).show();
        }
    };

}

最后就是调用了

BuyGoodsDialog dialog=new BuyGoodsDialog(MainActivity.this, R.style.Theme_AppCompat_Dialog, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"点击了确定按钮!",Toast.LENGTH_SHORT).show();
            }
});
dialog.show();

运行,测试如下:

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

(0)

相关推荐

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

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

  • 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自定义ProgressDialog进度条对话框的实现

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

  • android基于dialog实现等待加载框示例

    最近想找一些新颖的等待框,但一直找不到理想的效果,没有办法,只好自己动手丰衣足食了. 先给大家看个效果图! 首先就是新建一个dialog的XML文件了 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="w

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

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

  • Android使用Dialog风格弹出框的Activity

    在Android中经常会遇到需要使用Dialog风格弹出框的activity,首先我们可能会首先想到的是在XML布局文件中设置android:layout_height="wrap_content"属性,让activity的高度自适应,显然这还不行,我们还需要为其DialogActivity设置自定义一个样式 <style name="dialogstyle"> <!--设置dialog的背景--> <item name="a

  • 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自定义Dialog实现加载对话框效果

    前言 最近开发中用到许多对话框,之前都是在外面的代码中创建AlertDialog并设置自定义布局实现常见的对话框,诸如更新提示等含有取消和删除两个按钮的对话框我们可以通过代码创建一个AlertDialog并通过它暴露的一系列方法设置我们自定义的布局和style,但有时候系统的AlertDialog并不能实现更好的定制,这时,我们就想到了自定义Dialog.通过查看AlertDialog的类结构发现它也是继承于Dialog,于是我们也可以通过继承Dialog实现我们自定义的Dialog.这篇文章将

  • 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的白色边框的简单方法

    在value目录下,创建styles.xml文件 复制代码 代码如下: <?xml version="1.0" encoding="UTF-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android"> <style        name="dialog"        parent="@androi

随机推荐