博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android自定义Dialog
阅读量:5820 次
发布时间:2019-06-18

本文共 7081 字,大约阅读时间需要 23 分钟。

hot3.png

第一种:使用AlertDialog.Builder创建的Dialog,使用系统默认样式的Dialog,我们先看一下效果:131507_fVe0_2407682.jpg

下面是实现方式:

1:创建布局zuohao_dialog.xml

        
        
        

2:java代码:

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);                        dialog.create();                        dialog.setTitle("您好:");                        viewdialog=LayoutInflater.from(MainActivity.this).inflate(R.layout.zuohao_dialog, null);                                                dialog.setView(viewdialog);                                    dialog.setNegativeButton("确定", new DialogInterface.OnClickListener()            {                @Override                public void onClick(DialogInterface dialog, int which)                {                    final EditText etzuohao=(EditText) viewdialog.findViewById(R.id.et_setzuohao);                                        etzuohao.setOnClickListener(new OnClickListener() {                                                @Override                        public void onClick(View arg0) {                                                        etzuohao.setHint("");                        }                    });                                        et_zuohao=etzuohao.getText().toString();                                        tv_zuohao.setText(et_zuohao+"号桌");                                        //使用SharedPreferences将桌号保存                    SharedPreferences spzuohao=getSharedPreferences("zuohao", MODE_PRIVATE);                                        Editor editor=spzuohao.edit();                                        editor.putString("zuohao",et_zuohao );                                        editor.commit();                                        dialog.dismiss();                }            });            dialog.setPositiveButton("取消", new DialogInterface.OnClickListener()            {                @Override                public void onClick(DialogInterface dialog, int which)                {                    dialog.dismiss();                }            });            dialog.create().show();

第二种:使用自定义样式的Dialog

效果图:132127_uldJ_2407682.jpg

1:一样还是布局:zuohao_dialog1.xml

    
    
        
        
        
        
        
        
        
        
    

2:java代码:

final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.zuohao_dialog1, null);                TextView tv_message=(TextView) view.findViewById(R.id.dialog_message);                tv_message.setVisibility(View.VISIBLE);                tv_message.setText("\t\t\t\t\t\t"+"请先输入您的桌号,方便您下单,或者点击屏幕右上角的“获取桌号”!");                                Button btok=(Button) view.findViewById(R.id.dialog_btok);                Button btresult=(Button) view.findViewById(R.id.dialog_btresult);                final EditText et_zuohao1=(EditText) view.findViewById(R.id.et_setzuohao1);                                                                //R.style.dialog,这个样式需在values\styles.xml文件中自己定义                final Dialog dialog = new Dialog(MainActivity.this,R.style.dialog);//,R.style.dialog                Window dialogWindow = dialog.getWindow();                WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); // 获取对话框当前的参数值                dialogWindow.setAttributes(params);                dialog.setContentView(view);                                btok.setOnClickListener(new OnClickListener() {                                        @Override                    public void onClick(View arg0) {                                                                                                String zuohaostr1=et_zuohao1.getText().toString().trim();                                                Log.e("MainAct", "zuohao"+zuohaostr1);                                                tv_zuohao.setText(zuohaostr1+"号桌");                                                dialog.dismiss();                                            }                });                            btresult.setOnClickListener(new OnClickListener() {                                        @Override                    public void onClick(View arg0) {                        dialog.dismiss();                    }                });                dialog.show();

需要注意的是上面这段代码中的其中定义Dialog的样式,如下:

final Dialog dialog = new Dialog(MainActivity.this,R.style.dialog);

其中R.style.dialog这个需在values\styles.xml文件中自己定义:定义方式如下:

    
            
@null            
true            
true            
true            
@android:color/transparent            
@android:color/transparent            
true            
0.6   

效果图:134326_CHIZ_2407682.jpg

第四种:圆形进度条对话框:

1:先定义一个布局:login_dialog.xml

        
    

2:values\styles.xml里面定义其样式:

    
        
@null        
true        
@color/touming        
true        
@null    

3:在res文件夹下新建一个名叫anim的文件夹,定义它的动画效果:login_dialog_anim.xml

        

4:自定义Dialog关联布局,样式,动画:LoginDialog.java

package com.ywsym.tuan.dialog;import android.app.Dialog;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import com.ywsym.tuan.R;/** * 创建一个Dialog类,创建一个静态的方法,返回的Dialog实体,并传递两个参数 * context表示上下文对象,msg表示对话框提示信息 * */public class LoginDialog {    public static Dialog CreateDialog(Context context,String msg){                //1:先创建子布局选择器对象        LayoutInflater inflater=LayoutInflater.from(context);                //获取dialog布局        View dialogView=inflater.inflate(R.layout.login_dialog, null);                //获取dialog布局总布局LinearLayout        LinearLayout layout=(LinearLayout) dialogView.findViewById(R.id.dialog);                //获取布局上的控件对象        ImageView dialog_imag=(ImageView) dialogView.findViewById(R.id.login_dialog_imag);                TextView dialog_msgtv=(TextView) dialogView.findViewById(R.id.login_dialog_msg);                //给当前对话框加入动画        Animation anim=AnimationUtils.loadAnimation(context, R.anim.login_dialog_anim);                //将动画效果跟ImageView图片结合        dialog_imag.startAnimation(anim);                //将用户提示给TextView赋值        dialog_msgtv.setText(msg);                //给Dialog添加自定义的样式        Dialog dialog=new Dialog(context, R.style.loading_dialog);                //将dialog添加到当前布局上去,并设置当前的布局填充屏幕        dialog.setContentView(layout, new LinearLayout.LayoutParams(                LinearLayout.LayoutParams.FILL_PARENT,                 LinearLayout.LayoutParams.FILL_PARENT)        );                //最后返回dalog;        return dialog;    }}

5:在需要Dialog的地方掉用就OK

//正在登录//调用加载进度条对话框dialog=LoginDialog.CreateDialog(LoginActivity.this, "正在登录");dialog.show();

转载于:https://my.oschina.net/symahll/blog/485144

你可能感兴趣的文章
django 目录结构修改
查看>>
win8 关闭防火墙
查看>>
CSS——(2)与标准流盒模型
查看>>
MYSQL 基本SQL语句
查看>>
C#中的Marshal
查看>>
linux命令:ls
查看>>
Using RequireJS in AngularJS Applications
查看>>
hdu 2444(二分图最大匹配)
查看>>
【SAP HANA】关于SAP HANA中带层次结构的计算视图Cacultation View创建、激活状况下在系统中生成对象的研究...
查看>>
DevOps 前世今生 | mPaaS 线上直播 CodeHub #1 回顾
查看>>
iOS 解决UITabelView刷新闪动
查看>>
Web前端JQuery入门实战案例
查看>>
CentOS 7 装vim遇到的问题和解决方法
查看>>
JavaScript基础教程1-20160612
查看>>
iOS \U7ea2 乱码 转换
查看>>
FCN图像分割
查看>>
ios xmpp demo
查看>>
python matplotlib 中文显示参数设置
查看>>
【ros】Create a ROS package:package dependencies报错
查看>>
HDU1576 A/B【扩展欧几里得算法】
查看>>