我会保持这么简单。我想在ImageView
中将AlertDialog
设置为Drawables
数组中的图像。我可以通过访问ImageView
来检索我要设置为dismiss
的图像mImages [位置]。
以下是简要说明(以下完整说明):
简而言之 - 我需要一种方法将图像从我的主要活动传递到对话框然后在取消finish();
对话框上并确认设置系统壁纸(到主活动传递的图像)然后{{1}活动。
以下是完整的解释:
用户会在Gallery
上方显示ImageView
和Gallery
,以显示焦点在Gallery
的图片的较大预览。
Gallery
中显示的图像使用以下设置:
// setup wallpaper array
private void findWallpapers() {
mThumbs = new ArrayList<Integer>(24);
mImages = new ArrayList<Integer>(24);
final Resources resources = getResources();
final String packageName = getApplication().getPackageName();
addWallpapers(resources, packageName, R.array.wallpapers);
}
// setup array defining all wallpapers & define thumbnails
private void addWallpapers(Resources resources, String packageName, int list) {
final String[] extras = resources.getStringArray(list);
for (String extra : extras) {
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0) {
final int thumbRes = resources.getIdentifier(extra + "_small",
"drawable", packageName);
if (thumbRes != 0) {
mThumbs.add(thumbRes);
mImages.add(res);
}
}
}
}
按下“设置壁纸”Button
后,应打开AlertDialog
,其中包含焦点在Gallery
的图像的另一个预览。 AlertDialog
将包含TextView
的说明,我们建议设置为壁纸的图片预览,“正常”Button
和“取消”Button
。按“好”Button
会将ImageView
预览中的图片设置为InputStream
的系统壁纸。
再次感谢!
private void selectWallpaper(int position) {
Toast.makeText(getBaseContext(), "Select Wallpaper", Toast.LENGTH_SHORT)
.show();
if (mIsWallpaperSet) {
return;
}
mIsWallpaperSet = true;
Context context = this;
// CharSequence text = "Wallpaper Set!";
// int duration = Toast.LENGTH_LONG;
InputStream stream = getResources().openRawResource(
mImages.get(position));
final Dialog accept = new Dialog(context);
accept.setContentView(R.layout.confirm);
accept.setTitle("Please Confirm");
TextView instructions = (TextView) accept.findViewById(R.id.textView1);
instructions.setText("Would you like to set this as your wallpaper?");
ImageView wallpreview = (ImageView) accept
.findViewById(R.id.imageView1);
wallpreview.createFromStream(stream, "test");
// SETUP cancel (no btn) listener
Button cancelbtn = (Button) accept.findViewById(R.id.button2);
cancelbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
accept.dismiss();
}
});
// SETUP Yes Btn listener
Button okbtn = (Button) accept.findViewById(R.id.button1);
okbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// dismiss being used as placeholder, actually setting wallpaper
// will be added
accept.dismiss();
}
});
accept.show();
}
{
;
}
答案 0 :(得分:0)
您可以使用布局充气程序来扩充警报对话框布局,并将其分配给主活动中的警报对话框视图。您可以使用findViewById()为这些按钮设置onClickListeners侦听器。这将确保您的onClickListener可以看到mImages。
final ArrayList<Integer> mImages=new ArrayList<Integer>();
final ImageView v=findViewById(R.id.imageView);
AlertDialog d=new AlertDialog.Builder(Main.this).create();
d.setView(getLayoutInflater().inflate(R.layout.dialogView,findViewById(R.id.ROOT_LAYOUT_IN_XML_ID),false));
((ImageView)d.findViewById(R.id.YOUR_VIEW_ID)).setImageResource(mImages.get(?));
((Button)d.findViewById(R.id.cancelButton)).setOnClickListener(new OnClickListener()
{
d.dismiss();
});
((Button)d.findViewById(R.id.acceptButton)).setOnClickListener(new OnClickListener()
{
v.setImageResource(mImages.get(?));
d.dismiss();
});