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

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

多选列表框


具体实现

主要是AlertDialog和DialogInterface.OnClickListener的使用,具体API接口实现中又完成了调用系统相机拍照, 系统相册图片的选取功能。

Button监听事件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
}
});

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* 选项的事件监听器
*/
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()函数

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

1
2
3
4
5
6
<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的基本样式都实现完成了!

Enjoy it ? Donate me ! 求鼓励,求支持!
0%