Android自定义dialog之多选列表框的实现(四)

    多选列表框的实现原理极为简单,基本上是用系统原生的Dialog,主要看一下内容的实现。

先上效果图

Button监听事件:

Button mulChoiseDialog = (Button) findViewById(R.id.button4);
        mulChoiseDialog.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                //选项数组
                String[] choices = {"新浪微博", "拍照", "从相册中选取"};
                //包含多个选项的对话框
                AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
//                        .setIcon(android.R.drawable.btn_star)
//                        .setTitle("分享")
                        .setItems(choices, onselect).create();
                dialog.show();
            }
        });

对话框列表中的每个事件的监听处理:

/** * 选项的事件监听器 */
    DialogInterface.OnClickListener onselect = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case 0:
                    Toast.makeText(MainActivity.this, "您选择了新浪微博.", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    // 还有权限设置
                    Toast.makeText(MainActivity.this, "您选择了拍照", Toast.LENGTH_SHORT).show();
                    Intent intent1 = new Intent("android.media.action.IMAGE_CAPTURE");
                    intent1.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(new File(Environment
                                    .getExternalStorageDirectory(), "camera.jpg")));
                    intent1.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
                    startActivityForResult(intent1, 10);
                    break;
                case 2:
                    Toast.makeText(MainActivity.this, "您选择了从相册中选取", Toast.LENGTH_SHORT).show();
                    Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT);
                    intent2.addCategory(Intent.CATEGORY_OPENABLE);
                    intent2.setType("image/*");
                    intent2.putExtra("crop", "true");
                    intent2.putExtra("aspectX", 1);
                    intent2.putExtra("aspectY", 1);
                    intent2.putExtra("outputX", 80);
                    intent2.putExtra("outputY", 80);
                    intent2.putExtra("return-data", true);
                    startActivityForResult(intent2, 11);
                    break;
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
            this.mImageView.setVisibility(View.VISIBLE);
            this.mImageView.setImageDrawable(Drawable.createFromPath(new File(
                    Environment.getExternalStorageDirectory(), "camera.jpg")
                    .getAbsolutePath()));
            System.out.println("data-->" + data);
        } else if (requestCode == 11 && resultCode == Activity.RESULT_OK) {
            System.out.println("data2-->" + data);
            this.mImageView.setVisibility(View.VISIBLE);

            Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
            this.mImageView.setImageBitmap(cameraBitmap);
        }
    }

onActivityResult()函数

此函数主要完成了,对调用系统摄像头拍摄照片和,从相册中选取照片对照片显示处理。
并且需要加上相应的权限:

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

照片显示的效果如下:

那么Demo的基本样式都实现完成了!

你可能感兴趣的:(android,自定义,多选列表框,系统拍照,相册选取)