zoukankan      html  css  js  c++  java
  • 安卓自定义控件之设计自己的提示dialog

    当需要设计自己的对话框dialog,并且按照自己要求定制,比如最常见的,操作提示,操作成功显示成功的信息,失败时也显示对应的提示

    先看看效果:

    步骤:

    1.继承Dialog类,定义几个变量

    private boolean flag;//显示什么图片
        private String msg,title;//标题和内容
        private TextView tvmsg;//布局文件中的显示消息框
        private ImageView icon;//布局文件中的显示图片按钮
        private Button close;//布局文件中的关闭按钮

    2.实现父类构造器

    public MyDialog(Context context, boolean flag, String title,String msg)
        {
            super(context);
            this.flag = flag;
            this.msg = msg;
            this.title=title;
        }

    3.重写父类的onCreate方法

    View Code
    protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog);
            tvmsg = (TextView) findViewById(R.id.msg);
            icon = (ImageView) findViewById(R.id.icon);
            close = (Button) findViewById(R.id.ok);    
            if(flag)
                icon.setImageResource(R.drawable.ok);
            else icon.setImageResource(R.drawable.error);
            setTitle(title);
            tvmsg.setText(msg);    
            setOnClickListener();
        }

    4.给按钮添加监听

     private void setOnClickListener(){
            close.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    dismiss();
                }
            });
        }

    调用方法:

      MyDialog dialog=new MyDialog(this, false,"信息提示","您操作有误");     
           dialog.show();

    布局文件,我直接上传我的提供大家下载:/Files/Jaylong/dialog.xml

  • 相关阅读:
    互联网与局域网(四)
    Socket介绍(五)
    HttpClient(七)
    TCP协议与HTTP协议区别
    TCP连接的三次握手
    context-param和init-param区别
    【HPU】[1736]老王修马路(二)
    【HPU】[1735]老王修马路(一)
    【HPU】[1734]老王修公园
    【HPU】[1733]神奇的数字9
  • 原文地址:https://www.cnblogs.com/Jaylong/p/dialog.html
Copyright © 2011-2022 走看看