android中dialog封装

在开发中经常用到对话框和加载中的进度条,做一个总结。
效果图:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
在activity中的代码:
public class MainActivity extends Activity implements OnClickListener {
private Button btn_ok;
private Button btn_list;
private Button btn_myDialog;
private Button btn_progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn_ok = (Button) findViewById(R.id.btn_ok);
    btn_list = (Button) findViewById(R.id.btn_list);
    btn_myDialog=(Button) findViewById(R.id.btn_myDialog);
    btn_progress=(Button) findViewById(R.id.btn_progress);
    btn_ok.setOnClickListener(this);
    btn_list.setOnClickListener(this);
    btn_myDialog.setOnClickListener(this);
    btn_progress.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    //系统的对话框;
    case R.id.btn_ok:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
        builder.setMessage("提示的内容");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub

                    }
                });
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub

                    }
                });
        builder.create().show();
        break;
    //系统列表框;
    case R.id.btn_list:
        AlertDialog.Builder builder2 = new AlertDialog.Builder(
                MainActivity.this);
        builder2.setTitle("选择");
        builder2.setIcon(R.drawable.ic_launcher);
        String items[] = { "拍照", "图片库" };
        builder2.setItems(items, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int which) {
                // TODO Auto-generated method stub
                switch (which) {
                case 0:
                    Toast.makeText(MainActivity.this, "拍照", 0).show();
                    break;

                case 1:
                    Toast.makeText(MainActivity.this, "图片库 ", 0).show();
                    break;
                }
            }
        });
        builder2.show();
        break;
        //自定义封装对话框;
    case R.id.btn_myDialog:
        final Dialog_ask dialog = new Dialog_ask(MainActivity.this,
                "对话框的标题", "提示的内容");
        dialog.show();
        dialog.setCanel("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT)
                        .show();
                dialog.dismiss();
            }
        });
        dialog.setOk("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT)
                        .show();
                dialog.dismiss();
            }
        });
        break;
        //自定义正在加载中进度条;
    case R.id.btn_progress:
        LoadingDialog load_dialog=new LoadingDialog(this);
        load_dialog.showDialog();
        break;
    }

}

}


封装的Dialog_adk代码:

public class Dialog_ask {
    Context context;
    android.app.AlertDialog dialog;
    TextView tv_title;
    TextView tv_con;
    LinearLayout buttonLayout;
    Button btn_ok;
    Button btn_canel;

    /**
     * @param context
     * @param title   设置对话框的标题
     * @param con     设置对话框的内容
     */
    public Dialog_ask(final Context context, String title, String con) {
        // TODO Auto-generated constructor stub
        this.context = context;
        dialog = new android.app.AlertDialog.Builder(context).create();
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_ask, null);
        dialog.setView(view);
        //在这可以设置dialog的一些属性;
        btn_ok = (Button) view.findViewById(R.id.btn_ok);
        btn_canel = (Button) view.findViewById(R.id.btn_cancel);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_con = (TextView) view.findViewById(R.id.tv_con);
        tv_title.setText(title);
        tv_con.setText(con);

    }

    public void show() {
        dialog.show();
    }

    //确定
    public void setOk(String s, final View.OnClickListener listener) {
        btn_ok.setText(s);
        btn_ok.setOnClickListener(listener);
    }

    //取消
    public void setCanel(String s, final View.OnClickListener listener) {
        btn_canel.setText(s);
        btn_canel.setOnClickListener(listener);
        // dismiss();
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

封装的Progress加载中的代码:
public class LoadingDialog {
private Context context;
private ImageView imageView;
private Dialog dialog;

public LoadingDialog(Context context) {
    this.context = context;
    dialog = new Dialog(context, R.style.dialog_style);
    View view = LayoutInflater.from(context).inflate(R.layout.loading_dialog, null);
    imageView= (ImageView) view.findViewById(R.id.img);
    Animation animation= AnimationUtils.loadAnimation(
            context, R.anim.load_animation);
    // 使用ImageView显示动画
    imageView.startAnimation(animation);
    dialog.setCancelable(false);
    dialog.setContentView(view);

}

public void showDialog() {
    dialog.show();
}

public void dismissDialog() {
    if (dialog != null && dialog.isShowing()) {
        dialog.dismiss();
    }
}

}

这里写图片描述
代码如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        style="@style/TextAppearance.Info2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#efefef"
        android:padding="10dp"
        android:text="选择关闭类型" />

    <View
        android:layout_below="@id/title"
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#50000000" />

    <Button
        android:id="@+id/tv_normal"
        style="@style/TextAppearance.Info2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/view1"
        android:background="@drawable/bg_quality_click"
        android:gravity="left|center_vertical"
        android:paddingLeft="10dp"
        android:text="普通关闭" />

    <View
        android:layout_below="@id/tv_normal"
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#10000000" />

    <Button
        android:id="@+id/tv_noStandart"
        style="@style/TextAppearance.Info2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/view2"
        android:background="@drawable/bg_quality_click"
        android:gravity="left|center_vertical"
        android:paddingLeft="10dp"
        android:text="无标准" />
    <View
        android:layout_below="@id/tv_noStandart"
        android:id="@+id/view3"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#10000000" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/button_quality_close"
        android:gravity="center"
        android:text="取消" />
</RelativeLayout>
   myDialog = new AlertDialog.Builder(context).create();
                myDialog.show();
                WindowManager windowManager = getWindowManager();
                Display display = windowManager.getDefaultDisplay();
                WindowManager.LayoutParams lp = myDialog.getWindow().getAttributes();
                lp.width = display.getWidth()-100; //设置宽度
                lp.height=display.getHeight()-200;
                myDialog.getWindow().setAttributes(lp);

                myDialog.getWindow().setContentView(R.layout.dialog_close);
                myDialog.getWindow()
                        .findViewById(R.id.tv_normal)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                myDialog.dismiss();
                            }
                        });

代码下载地址:http://download.csdn.net/detail/androidxiaogang/9235401

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值