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

Android Dialog对话框

程序员文章站 2024-02-09 22:38:04
Dialog的基本方法 系统样式 1.下拉列表 2.单选框列表 3.多选框列表 4.等待对话框 5.进度条对话框 自定义Dialog 1.继承Dialog 2.自定义Style 3.自定义布局 4.ipad隐藏底部虚拟按键 5.使用自定义Dialog ......

dialog的基本方法

//创建dialog
alertdialog.builder builder = new alertdialog.builder(mainactivity.this);

//设置标题图标
builder.seticon(r.drawable.ic_launcher);

//设置标题
builder.settitle("这是一个对话框");

//设置信息
builder.setmessage("是否要跳转?");

//确定按钮
setpositivebutton(charsequence text, dialoginterface.onclicklistener listener)

//取消按钮
setnegativebutton (charsequence text, dialoginterface.onclicklistener listener)

//忽略
setneutralbutton (charsequence text, dialoginterface.onclicklistener listener)

//显示对话框
show();

系统样式

1.下拉列表

alertdialog.builder builder = new alertdialog.builder(mainactivity.this);
         builder.seticon(r.drawable.ic_launcher);
         builder.settitle("选择一个城市");
         //下拉列表的数据源
         final string[] cities = {"北京", "上海", "广州", "深圳", "杭州"};
         builder.setitems(cities, new dialoginterface.onclicklistener(){
             @override
             public void onclick(dialoginterface dialog, int which){
                 toast.maketext(mainactivity.this, "你选择的城市为:" + cities[which], toast.length_short).show();
             }
 });
 builder.show();

2.单选框列表

alertdialog.builder builder = new alertdialog.builder(mainactivity.this);
        builder.seticon(r.drawable.ic_launcher);
        builder.settitle("请选择性别");
        final string[] sex = {"男", "女"};
        //第二个参数指定默认哪个单选框被勾中
        builder.setsinglechoiceitems(sex, 1, new dialoginterface.onclicklistener(){
            @override
            public void onclick(dialoginterface dialog, int which){
                   toast.maketext(mainactivity.this, "性别为:" + sex[which], toast.length_short).show();
            }
        });
        builder.setpositivebutton("确定", new dialoginterface.onclicklistener(){
            @override
            public void onclick(dialoginterface dialog, int which){
                        
            }
        });
        builder.setnegativebutton("取消", new dialoginterface.onclicklistener(){
            @override
            public void onclick(dialoginterface dialog, int which){
                        
            }
       });
builder.show();

3.多选框列表

string str;
alertdialog.builder builder = new alertdialog.builder(mainactivity.this); builder.seticon(r.drawable.ic_launcher); builder.settitle("选择你想看的电视剧"); final string[] hobbies = {"廷禧攻略", "扶摇", "香蜜沉沉烬如霜", "如懿传"}; //第二个参数代表哪几个选项被选择,需要传递一个boolean[]数组进去,其长度要和第一个参数的长度相同,如果null表示都不选 builder.setmultichoiceitems(hobbies, null, new dialoginterface.onmultichoiceclicklistener(){   @override   public void onclick(dialoginterface dialog, int which, boolean ischecked){   if(ischecked){     str.append(hobbies[which] + ", ");   }   toast.maketext(mainactivity.this, "选择的是:" + str.tostring(), toast.length_short).show(); } }); builder.setpositivebutton("确定", new dialoginterface.onclicklistener(){   @override   public void onclick(dialoginterface dialog, int which){ } }); builder.setnegativebutton("取消", new dialoginterface.onclicklistener(){   @override   public void onclick(dialoginterface dialog, int which){ } }); builder.show();

 4.等待对话框

progressdialog waitingdialog= new progressdialog(mainactivity.this);
waitingdialog.settitle("等待加载中,请稍后...");
waitingdialog.setmessage("等待中...");
waitingdialog.setindeterminate(true);//采用不确定式的进度条
waitingdialog.setcancelable(false);//点击外部不取消对话框
waitingdialog.show();

5.进度条对话框

int maxpd = 100;
progressdialog progressdialog =  new progressdialog(mainactivity.this);
progressdialog.setprogress(0);//设置默认值
progressdialog.settitle("正在下载");
progressdialog.setprogressstyle(progressdialog.style_horizontal);//进度条样式
progressdialog.setmax(maxpd);//设置最大值
progressdialog.show();

自定义dialog

1.继承dialog

public class customdialog extends dialog {
//标题
protected textview hinttv;

//左边按钮
protected button doubleleftbtn;

//右边按钮
protected button doublerightbtn;

//输入框
public edittext edittext;

public customdialog(context context) {
    super(context, r.style.customdialogstyle);
}

@override
protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    this.setcancelable(false);  // 是否可以撤销
    setcontentview(r.layout.dialog_custom);

    hinttv = findviewbyid(r.id.tv_common_dialog_hint);
    doubleleftbtn =  findviewbyid(r.id.btn_common_dialog_double_left);
    doublerightbtn = findviewbyid(r.id.btn_common_dialog_double_right);
    edittext = findviewbyid(r.id.et_common_dialog);
}

//置右键文字和点击事件
public void setrightbutton(string rightstr, view.onclicklistener clicklistener) {
    doublerightbtn.setonclicklistener(clicklistener);
    doublerightbtn.settext(rightstr);
}

//设置左键文字和点击事件
public void setleftbutton(string leftstr, view.onclicklistener clicklistener) {
    doubleleftbtn.setonclicklistener(clicklistener);
    doubleleftbtn.settext(leftstr);
}

//设置提示内容
public void sethinttext(string str) {
    hinttv.settext(str);
    hinttv.setvisibility(view.visible);
}

//给两个按钮 设置文字
public void setbtntext(string leftstr, string rightstr) {
    doubleleftbtn.settext(leftstr);
    doublerightbtn.settext(rightstr);
}

}

2.自定义style

<style name="customdialogstyle" parent="@android:style/theme.dialog">
        <!-- 边框 -->
        <item name="android:windowframe">@null</item>

        <!-- 背景透明 -->
        <item name="android:windowbackground">@color/transparent</item>

        <!-- 无标题 -->
        <item name="android:windownotitle">true</item>

        <!-- 是否浮现在activity之上 -->
        <item name="android:windowisfloating">true</item>

        <!-- 半透明 -->
        <item name="android:windowistranslucent">false</item>

        <!-- 背景模糊 -->
        <item name="android:windowcontentoverlay">@null</item>

        <!-- 允许对话框的背景变暗 -->
        <item name="android:backgrounddimenabled">true</item>

        <!-- 字体颜色 -->
        <item name="android:textcolor">@color/white</item>
        <item name="android:edittextcolor">@color/white</item>
 </style>

3.自定义布局

<linearlayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_common_dialog_layout"
    android:layout_width="500dp"
    android:layout_height="250dp"
    android:layout_margin="5dp"
    android:background="@drawable/background_info"
    android:orientation="vertical"
    android:gravity="center">

    <!--提示信息-->
    <textview
        android:id="@+id/tv_common_dialog_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textcolor="@color/white"
        android:textsize="27sp"/>
    <edittext
        android:id="@+id/et_common_dialog"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:textcolor="@color/back"
        android:inputtype="text|textnosuggestions"
        tools:ignore="labelfor"
        android:hint="请输入密码"/>
    
    <!--底部按钮-->
    <linearlayout
        android:id="@+id/ll_common_dialog_double"
        android:layout_width="360dp"
        android:layout_height="60dp"
        android:layout_margin="20dp"
        android:orientation="horizontal">

        <button
            android:id="@+id/btn_common_dialog_double_left"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/btnselector"
            android:gravity="center"
            android:textcolor="@color/white"
            android:textsize="27dp"/>

        <button
            android:id="@+id/btn_common_dialog_double_right"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/btnselector"
            android:gravity="center"
            android:textcolor="@color/white"
            android:textsize="27dp"/>
    </linearlayout>
</linearlayout>

4.ipad隐藏底部虚拟按键

//弹出dialog时隐藏底部虚拟按键
dialog.getwindow().getdecorview().setsystemuivisibility(view.system_ui_flag_hide_navigation);
dialog.getwindow().getdecorview().setonsystemuivisibilitychangelistener(new view.onsystemuivisibilitychangelistener() {
    @override
    public void onsystemuivisibilitychange(int visibility) {
        int uioptions =view.system_ui_flag_hide_navigation;
        if (build.version.sdk_int >= 19) {
            uioptions |= 0x00001000;
        } else {
            uioptions |= view.system_ui_flag_low_profile;
        }
        dialog.getwindow().getdecorview().setsystemuivisibility(uioptions);
    }
});

5.使用自定义dialog

customdialog dialog = new customdialog(this);
dialog.sethinttext("请输入密码");
dialog.setleftbutton("取消", new view.onclicklistener() {
       @override
       public void onclick(view v) {
           dialog.dismiss();
       }
});
dialog.setrightbutton("确定", new view.onclicklistener() {
        @override
        public void onclick(view v) {
               
        }
});
dialog.show();