欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义弹出框dialog效果

程序员文章站 2022-07-11 23:16:10
项目要用到弹出框,还要和苹果的样式一样(android真是没地位),所以就自己定义了一个,不是很像(主要是没图),但是也还可以。 废话不多说了,直接上代码 1、先看布局...

项目要用到弹出框,还要和苹果的样式一样(android真是没地位),所以就自己定义了一个,不是很像(主要是没图),但是也还可以。

废话不多说了,直接上代码

1、先看布局文件

<?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="match_parent"
  android:padding="20dp"
  android:orientation="vertical">

  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:background="@drawable/custom_dialog_background"
    android:orientation="vertical">

    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center_horizontal"
      android:orientation="vertical">

      <textview
        android:id="@+id/tv_title_custom_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margintop="20dp"
        android:text="提醒"
        android:textcolor="#000"
        android:textsize="18dp" />

      <textview
        android:id="@+id/tv_message_custom_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margintop="10dp"
        android:text="您确定要取消订单吗" />
    </linearlayout>

    <view
      android:layout_width="match_parent"
      android:layout_height="0.5dp"
      android:layout_margintop="20dp"
      android:background="#dfdfdf" />

    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">

      <button
        android:id="@+id/btn_negative_custom_dialog"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:text="取消"
        android:textcolor="@android:color/holo_blue_dark" />

      <view
        android:layout_width="0.5dp"
        android:layout_height="match_parent"
        android:background="#dfdfdf" />

      <button
        android:id="@+id/btn_positive_custom_dialog"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:text="确定"
        android:textcolor="@android:color/holo_blue_dark" />
    </linearlayout>
  </linearlayout>
</linearlayout>

2、集成dialog重写了一下

package newair.com.storelibrary.ui.custom.widget;

import android.app.dialog;
import android.content.context;
import android.content.dialoginterface;
import android.graphics.color;
import android.graphics.drawable.colordrawable;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.view.window;
import android.view.windowmanager;
import android.widget.button;
import android.widget.framelayout;
import android.widget.linearlayout;
import android.widget.textview;

import newair.com.storelibrary.r;

/**
 * created by ouhimehime on 16/4/22.
 * ---------自定义提示框-----------
 */
public class customdialog extends dialog {


  public customdialog(context context) {
    super(context);
  }

  public customdialog(context context, int themeresid) {
    super(context, themeresid);
  }

  protected customdialog(context context, boolean cancelable, oncancellistener cancellistener) {
    super(context, cancelable, cancellistener);
  }

  public static class builder {
    private context context;
    private string title; //标题
    private string message;//提示消息
    private string negative_text;//消极的
    private string positive_text;//积极的
    private dialoginterface.onclicklistener negativelistener;//消极的监听
    private dialoginterface.onclicklistener positivelistener;//积极的监听

    public builder(context context) {
      this.context = context;
    }

    public builder settitle(string title) {
      if (title == null) {
        this.title = "提醒";
      }
      this.title = title;
      return this;
    }

    public builder setmessage(string message) {
      if (message == null) {
        this.message = "您没有填写提示信息哦";
      }
      this.message = message;
      return this;
    }

    public builder setnegativebutton(string negative_text, dialoginterface.onclicklistener negativelistener) {
      if (negative_text == null) {
        this.negative_text = "取消";
      }
      this.negative_text = negative_text;
      this.negativelistener = negativelistener;

      return this;
    }

    public builder setpositionbutton(string positive_text, dialoginterface.onclicklistener positivelistener) {
      if (positive_text == null) {
        this.positive_text = "确定";
      }
      this.positive_text = positive_text;
      this.positivelistener = positivelistener;

      return this;
    }

    private textview tv_title_custom_dialog; //标题
    private textview tv_message_custom_dialog;//提示信息
    private button btn_negative_custom_dialog;//消极
    private button btn_positive_custom_dialog;//积极


    public customdialog create() {
      final customdialog dialog = new customdialog(context);
      view view = layoutinflater.from(context).inflate(r.layout.dialog_custom_style_layout, null);
      dialog.requestwindowfeature(window.feature_no_title);//加上这一句,取消原来的标题栏,没加这句之前,发现在三星的手机上会有一条蓝色的线
//      dialog.addcontentview(view, new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent));
      dialog.setcontentview(view, new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent));
      tv_title_custom_dialog = (textview) view.findviewbyid(r.id.tv_title_custom_dialog);
      tv_message_custom_dialog = (textview) view.findviewbyid(r.id.tv_message_custom_dialog);
      btn_negative_custom_dialog = (button) view.findviewbyid(r.id.btn_negative_custom_dialog);
      btn_positive_custom_dialog = (button) view.findviewbyid(r.id.btn_positive_custom_dialog);
      tv_title_custom_dialog.settext(title);
      tv_message_custom_dialog.settext(message);
      btn_negative_custom_dialog.settext(negative_text);
      btn_positive_custom_dialog.settext(positive_text);
      dialog.getwindow().setbackgrounddrawable(new colordrawable(color.transparent));
      btn_negative_custom_dialog.setonclicklistener(new view.onclicklistener() {
        @override
        public void onclick(view v) {
          negativelistener.onclick(dialog, dialog.button_negative);
        }
      });
      btn_positive_custom_dialog.setonclicklistener(new view.onclicklistener() {
        @override
        public void onclick(view v) {
          positivelistener.onclick(dialog, dialog.button_positive);
        }
      });
      return dialog;
    }
  }
}

3、使用起来和系统的用法一样

customdialog.builder builder = new customdialog.builder(this);
        builder.settitle("购物提醒")
            .setmessage("我是提示信息,大家好好")
            .setnegativebutton("再看看", new dialoginterface.onclicklistener() {
              @override
              public void onclick(dialoginterface dialog, int which) {
                dialog.dismiss();
                toast.maketext(goodslistactivity.this, "点击了取消按钮", toast.length_short).show();
              }
            })
            .setpositionbutton("确定", new dialoginterface.onclicklistener() {
              @override
              public void onclick(dialoginterface dialog, int which) {
                dialog.dismiss();
                toast.maketext(goodslistactivity.this, "点击了确定按钮", toast.length_short).show();
              }
            })
            .create()
            .show();

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