安卓自定义控件之设计自己的提示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

 

posted @ 2012-05-04 22:36  流星剑  阅读(3140)  评论(0编辑  收藏  举报