安卓自定义Dialog

来源:互联网 发布:相机双重曝光软件 编辑:程序博客网 时间:2024/05/17 20:25

以前Dialog用的并不多,所以也没有想过把系统自带的Dialog美化一下什么的,今天做仿微博选择性别的时候需要自定义一个Dialog,网上查了查,有不少的例子,但是很多都不是能直接用的,这也就是传说中的程序都是调试出来的吧,花了一下午做了一个自定义的Dialog(用的时间太长了,不得不说,效率实在是太低了,都学了一年多的安卓了,现在还是一只连菜鸟都算不上的小白大哭

废话不多说,感慨也还是留着晚上睡觉的时候吧,现在开始自定义Dialog!!!

新建一个叫做CustomDialog的Java文件,并且继承Dialog,这时候系统会提示创建构造函数的,最好是把3个构造函数都创建上,虽然很多时候并用不上,但是我记得又一次自定义控件的时候只创建了只有参数Context的构造函数,但是他报错了,改正方法就是把所有的构造函数都创建上。

需要说一下的就是AlertDialog并没有自己的构造函数,平时我们用AlertDialog的时候都是通过Builder这个类的构造函数来创建弹出框的,所以自定义弹出框,最重要的当然也就是Builder的重写,这是我重写的Builder,因为我只是要把弹出框的布局自定义为圆角的,所以这里我只是重写了得到布局的方法,其他的比如标题,图片什么的也都可以这样重写,这不过对于这些来说重写的时候一个控件好像是要只有参数不同的两个相同的方法(大家可以自己试一试,我没有做,毕竟他不符合我的要求吐舌头

public static class Bulider implements View.OnClickListener{    private Context context;    private View contentView;    private TextView man,woman;    private CustomDialog customDialog;    private CustomDialogPassValue customDialogPassValue;    public Bulider(Context context,CustomDialogPassValue customDialogPassValue){        this.context = context;        this.customDialogPassValue = customDialogPassValue;    }    public Bulider setContentView(View view){        this.contentView = view;        return this;    }    public CustomDialog create(){        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        customDialog = new CustomDialog(context, R.style.dialog);        View layout = layoutInflater.inflate(R.layout.changesex,null);        //addContentView(View view,ViewGroup.LayoutParams params):在屏幕中添加一个额外的视图,添加之后屏幕中原来的视图不会被删除        //View view:新添加的视图        //ViewGroup.LayoutParams params:视图的布局参数        customDialog.addContentView(layout,new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));        customDialog.setContentView(layout);        man = (TextView)layout.findViewById(R.id.man);        woman = (TextView)layout.findViewById(R.id.woman);        man.setOnClickListener(this);        woman.setOnClickListener(this);        return customDialog;    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.man:                customDialogPassValue.passValue("男");                customDialog.dismiss();                break;            case R.id.woman:                customDialogPassValue.passValue("女");                customDialog.dismiss();                break;        }    }}
需要提一下的就是
R.style.dialog

就是自定义弹出框的布局形式,直接上代码

<style name="dialog" parent="android:style/Theme.Dialog">    <item name="android:background">#00000000</item>    <!--透明色(#00000000)(@android:color/transparent)-->    <item name="android:windowBackground">@android:color/transparent</item>    <!--标题栏被隐藏-->    <item name="android:windowNoTitle">true</item>    <!--悬浮类型-->    <item name="android:windowIsFloating">true</item></style>
这样自定义的弹出框就完成了。效果图:


虽然弹出框是完成了,但是对于项目来说还有一个问题就是在选择了性别之后需要在原来的布局文件上显示出来,现在问题就来了,我现在知道的传值方式有两种,Intent、Bundle,大同小异,很明显的,这两种方法对于这个项目都不合适,最终的解决办法是接口(之前只知道接口能传值,但是一直不太会用,正好学了学)

public interface CustomDialogPassValue{    void passValue(String id);}
这是我在CustomDialog中定义的接口,向外传值的方法,在第一段的代码中都有,现在就剩一个问题了,怎么把值取出来。也不难,在调用自定义弹出框构造函数的时候嗲用接口就行了,下面就是代码,so简单。。

final CustomDialog.Bulider bulider = new CustomDialog.Bulider(this,new CustomDialog.CustomDialogPassValue(){    @Override    public void passValue(String id) {        newSex.setText(id);    }});bulider.create().show();
原创粉丝点击