1. 程式人生 > >各種dialog的封裝實現

各種dialog的封裝實現

這裡封裝了常用的Toast,Loading, 提示框,對話方塊,單選對話方塊,列表對話方塊,底部彈出對話方塊等,這裡做個記錄,方便以後複用,使用起來也非常簡單和方便。

呼叫方式如下:

package com.e6gps.dialogsetdemo;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.e6gps.dialogsetdemo.dialog.CommonDialog;
import com.e6gps.dialogsetdemo.dialog.ListSelectDialog;
import com.e6gps.dialogsetdemo.dialog.LoadingDialogUtil;
import com.e6gps.dialogsetdemo.dialog.PhotoDialogBuilder;
import com.e6gps.dialogsetdemo.dialog.SingleSelectDialog;

/**
 * Created by jason on 2016/8/18.
 * des:${des}
 */
public class MainActivity extends AppCompatActivity implements OnClickListener {

    private Button mBtn_loading;

    private Dialog mDialog;
    private Button mBtn_toast;
    private Button mBtn_hint;
    private Button mBtn_dialog;
    private Button mBtn_selelct_list_dialog;
    private Button mBtn_single_list_dialog;
    private Button mBtn_takephoto_dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtn_loading = (Button) findViewById(R.id.btn_loading);
        mBtn_toast = (Button) findViewById(R.id.btn_toast);
        mBtn_hint = (Button) findViewById(R.id.btn_hint);
        mBtn_dialog = (Button) findViewById(R.id.btn_dialog);
        mBtn_selelct_list_dialog = (Button) findViewById(R.id.btn_selelct_list_dialog);
        mBtn_single_list_dialog = (Button) findViewById(R.id.btn_single_list_dialog);
        mBtn_takephoto_dialog = (Button) findViewById(R.id.btn_takephoto_dialog);

        mBtn_loading.setOnClickListener(this);
        mBtn_toast.setOnClickListener(this);
        mBtn_hint.setOnClickListener(this);
        mBtn_dialog.setOnClickListener(this);
        mBtn_selelct_list_dialog.setOnClickListener(this);
        mBtn_single_list_dialog.setOnClickListener(this);
        mBtn_takephoto_dialog.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_loading:

                mDialog = LoadingDialogUtil.createLoadingDialog(MainActivity.this, "正在載入資料請稍候", true);
                mDialog.show();

                break;
            case R.id.btn_toast:
                ToastUtils.show(MainActivity.this, "我是吐司");
                break;

            case R.id.btn_hint:
                {
                    CommonDialog.Builder builder = new CommonDialog.Builder(MainActivity.this);
                    builder.setTitle("提示");
                    builder.setMessage("已經是最新版本");
                    builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    builder.create().show();
                }

                break;
            case R.id.btn_dialog:

                CommonDialog.Builder builder = new CommonDialog.Builder(MainActivity.this);
                builder.setTitle("撥打電話");
                builder.setMessage("15112403565");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtils.show(MainActivity.this, "取消");

                    }
                });
                builder.setPositiveButton("撥打", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtils.show(MainActivity.this, "確定");
                    }
                });
                builder.create().show();

                break;

            case R.id.btn_selelct_list_dialog:
                {
                    //設定標籤
                    final String[] values = new String[]{"今天", "明天","後天"};
                    final ListSelectDialog dialog = new ListSelectDialog(this, "標記為", values);
                    dialog.setOnSubmitClickListener(new ListSelectDialog.OnSubmitClickListener() {
                        @Override
                        public void onSubmitClick(int pos) {
                            ToastUtils.show(MainActivity.this,values[pos]);
                            dialog.hidden();
                        }
                    });
                    dialog.show();
                }
                break;

            case R.id.btn_single_list_dialog:
                {
                    // 取消釋出
                    final String[] values = new String[]{"太醜了", "沒有錢"};
                    SingleSelectDialog dialog = new SingleSelectDialog(this, "選擇原因", values);
                    dialog.setSelect(0);
                    dialog.setOnSubmitClickListener(new SingleSelectDialog.OnSubmitClickListener() {
                        @Override
                        public void onSubmitClick(int pos) {

                            ToastUtils.show(MainActivity.this,values[pos]);
                        }
                    });
                    dialog.show();
                }
                break;

            case R.id.btn_takephoto_dialog:
                {
                    final PhotoDialogBuilder photoDialogBuilder = new PhotoDialogBuilder(this);
                    photoDialogBuilder.setOnPhotoAlbum(new PhotoDialogBuilder.OnPhotoAlbumClickListner() {
                        @Override
                        public void onPhotoAlbum() {
                            ToastUtils.show(MainActivity.this,"相簿上傳");
                        }
                    });
                    photoDialogBuilder.setOnPhotoCamera(new PhotoDialogBuilder.OnPhotoCameraClickListner() {
                        @Override
                        public void onPhotoCamera() {
                            ToastUtils.show(MainActivity.this,"拍照上傳");
                        }
                    });
                    photoDialogBuilder.setOnBtncancle(new PhotoDialogBuilder.OnBtnCancleClickListner() {
                        @Override
                        public void onBtnCancle() {
                            photoDialogBuilder.hidden();
                        }
                    });
                    photoDialogBuilder.show();
                }
                break;

            default:
                break;
        }
    }
}

下面是效果圖:


需要原始碼,點選這裡下載。。。