自定义Dialog

来源:互联网 发布:下载漫画的软件 编辑:程序博客网 时间:2024/05/23 10:25

  之前一直没有写东西的习惯,而后也是听取了同事们的建议,简简单单的写点东西,只希望自己再次用到的时候能够减少点时间:

一直在想将view封装到dialog里面好,还是将view分离出来合理
首先Dialog的样式
<style name="MyDialog" parent="android:style/Theme.Dialog">    <!--背景颜色和透明度-->    <item name="android:windowBackground">@android:color/transparent</item>    <!--是否去除标题 -->    <item name="android:windowNoTitle">true</item>    <!--是否去除边框-->    <item name="android:windowFrame">@null</item>    <!--是否浮现在activity之上-->    <item name="android:windowIsFloating">true</item>    <!--是否模糊-->    <item name="android:backgroundDimEnabled">false</item></style>
Dialog的布局只是简单的写了一下,根据自己的需求写所需要的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/no"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <Button            android:id="@+id/yes"            android:layout_width="wrap_content"            android:layout_height="wrap_content"/>    </LinearLayout></LinearLayout>
Dialog
public class MyDialog extends Dialog {    private View mLayoutView;    private onNoClickListener noClickListener;//取消按钮被点击了的监听器    private onYesClickListener yesClickListener;//确定按钮被点击了的监听器    private Button yes,no;//确定与取消按钮    public void setNoClickListener(onNoClickListener noClickListener) {        this.noClickListener = noClickListener;    }    public void setYesClickListener(onYesClickListener yesClickListener) {        this.yesClickListener = yesClickListener;    }    public MyDialog(Context context ,int styleId,View layoutView) {        super(context,styleId);        mLayoutView=layoutView;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(mLayoutView);        setCanceledOnTouchOutside(false);        initView();    }    /**     *初始化控件     */    private void initView() {        yes= (Button) mLayoutView.findViewById(R.id.yes);        no= (Button) mLayoutView.findViewById(R.id.no);        //点击事件        yes.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                yesClickListener.onYesClick();            }        });        no.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                noClickListener.onNoClick();            }        });    }    /**     * 取消按钮接口     */    public interface onNoClickListener{        public void onNoClick();    }    /**     * 确定按钮接口     */    public interface onYesClickListener{        public void onYesClick();    }}
activity中的内容
private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    textView= (TextView) findViewById(R.id.tv);    textView.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog,null);            MyDialog myDialog=new MyDialog(MainActivity.this,R.style.MyDialog,view);            myDialog.setYesClickListener(new MyDialog.onYesClickListener() {                @Override                public void onYesClick() {                    Toast.makeText(MainActivity.this,"click yes",Toast.LENGTH_SHORT).show();                }            });            myDialog.show();        }    });}