Android-底部弹窗式选择框Dialog

本文以一个选择图库和照片为例:

需要三个元素:1弹窗布局,2style主题,设置Dialog类


先看弹窗布局:

xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"

    >

            android:id="@+id/ll_icon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/my_dialog_item"
        android:orientation="vertical">

                    android:id="@+id/btn_take_pic"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:clickable="true"
            android:gravity="center"
            android:text="拍照"
            android:textColor="@color/color_black_4e4e4e"
            android:textSize="@dimen/s_16sp" />

                    android:id="@+id/view_01"
            android:layout_width="match_parent"
            android:layout_height="@dimen/s_0d5dp"
            android:background="@drawable/line_night_color" />

                    android:id="@+id/btn_get_album_pic"
            android:layout_width="match_parent"
            android:layout_height="@dimen/s_50dp"
            android:layout_centerVertical="true"
            android:clickable="true"
            android:gravity="center"
            android:text="从相册选择"
            android:textColor="@color/color_black_4e4e4e"
            android:textSize="@dimen/s_16sp" />
    

            android:id="@+id/btn_cancel"
        android:layout_width="match_parent"
        android:layout_height="@dimen/s_50dp"
        android:layout_centerVertical="true"
        android:layout_marginTop="@dimen/s_10dp"
        android:background="@drawable/my_dialog_item"
        android:clickable="true"
        android:gravity="center"
        android:text="取消"
        android:textColor="@color/color_black_4e4e4e"
        android:textSize="@dimen/s_16sp"

        />



再看在values中建一个styles:



    
    
    
    

    

    

    

Dialog类:

public class SetPhoneDialog {
    private Context mContext;
    Dialog dialog;

    public TextView btnTakePic,btnGetAlbumPic,btnCancel;
    public SetPhoneDialog(Context mContext) {
        this.mContext = mContext;
    }
    public void dialogSet(final ArticleMenuOnClickListener listener){
        dialog = new Dialog(mContext, R.style.PictureDialog);
        dialog.setContentView(R.layout.menu_dialog_headimg);

        btnTakePic = (TextView) dialog.findViewById(R.id.btn_take_pic);
        btnGetAlbumPic = (TextView)dialog.findViewById(R.id.btn_get_album_pic);
        btnCancel = (TextView)dialog.findViewById(R.id.btn_cancel);

        //获取到dialog窗口
        Window dialogWindow = dialog.getWindow();
        //设置窗口在activity的最底下
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.PictureDialog); //设置窗口弹出动画

        WindowManager m = ((Activity) mContext).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 1); // 宽度设置为屏幕的百分之百
        dialogWindow.setAttributes(p);
        dialog.show();

        //设置取消按钮点击事件
        btnCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramView) {
                dialog.dismiss();
            }
        });
        //设置拍照按钮点击事件
        btnTakePic.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramView) {
                dialog.dismiss();
                //take a picture
                listener.btn_PaiZhaoListener();
            }
        });
        //设置从相册选择按钮点击事件
        btnGetAlbumPic.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramView) {
                dialog.dismiss();
                listener.btn_XiangJiListener();
            }
        });



    }
    //点击事件接口
    public interface ArticleMenuOnClickListener{
        public void btn_PaiZhaoListener();
        public void btn_XiangJiListener();
    }
}

在activity中实现:

//头像点击事件
case R.id.head_image:
    SetPhoneDialog diaolog = new SetPhoneDialog(getActivity());
    diaolog.dialogSet(new SetPhoneDialog.ArticleMenuOnClickListener() {
        @Override
        public void btn_PaiZhaoListener() {
            //方法回调,拍照
            try {
                Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //下面这句指定调用相机拍照后的照片存储的路径
                takeIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME)));
                startActivityForResult(takeIntent, REQUEST_CODE_TAKE);
            }catch (Exception e){
                Toast.makeText(getActivity(),"请打开手机相机权限!",Toast.LENGTH_SHORT).show();
            }
        }

效果图:



你可能感兴趣的:(Android笔记,android,android开发,as)