android中dialog封装

在开发中经常用到对话框和加载中的进度条,做一个总结。
效果图:
android中dialog封装_第1张图片
android中dialog封装_第2张图片
android中dialog封装_第3张图片
android中dialog封装_第4张图片
在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();
    }
}

}

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

你可能感兴趣的:(android,对话框)