单击浮动操作按钮时,会出现一个警告对话框,对话框中有一个图像视图和一个图像按钮。通过单击图像按钮,屏幕将移动到图库,单击其中一个图像后,图库将关闭,图像视图应使用所选图像进行设置。
这是我的意图,但目前的代码似乎有一个缺陷。我设法去画廊,拿起一张图片然后就可以了。所选图像未在对话框的图像视图中设置。
这里是定义单击浮动操作按钮的操作的代码。
fabAddACategory = (FloatingActionButton) findViewById(R.id.fabAddNewCategory);
fabAddACategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
layout = inflater.inflate(R.layout.dialog_add_a_new_category, null);
ivCategoryImage = (ImageView) layout.findViewById(R.id.ivCategoryImage);
final EditText etCategoryTitle = (EditText) layout.findViewById(R.id.etCategoryTitle);
alertDialog = new AlertDialog.Builder(ManageCategoriesActivity.this)
.setView(layout)
.setPositiveButton(getString(R.string.create), null)
.setNegativeButton(getString(R.string.cancel), null)
.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button btnCreate = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
btnCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String categoryTitle = etCategoryTitle.getText().toString();
}
});
ImageButton ibAdd = (ImageButton) layout.findViewById(R.id.ibAdd);
ibAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent chooseImageFromGallery = new Intent();
chooseImageFromGallery.setType("image/*");
chooseImageFromGallery.setAction(Intent.ACTION_GET_CONTENT);
try {
chooseImageFromGallery.putExtra("return-data", true);
startActivityForResult(chooseImageFromGallery, PICK_IMAGE);
} catch (ActivityNotFoundException e) {
}
}
});
}
});
alertDialog.show();
}
});
下面这个重写的onActivityResult()
方法定义了选择图像后的操作。根据此部分,应在图像视图上设置新选择的图像。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK && null != data) {
Bundle extra = data.getExtras();
if(extra != null) {
Bitmap picture = extra.getParcelable("data");
ivCategoryImage.setImageBitmap(picture);
}
}
}