0

我会保持这个简单。我想将一个ImageViewin设置AlertDialog为数组中的一个图像我想设置的Drawables 图像ImageView可以通过访问 mImages[position] 来检索。

这是一个简短的解释(下面的完整解释):简而言之 - 我需要一种方法将图像从我的主要活动传递到对话框,然后在取消dismiss对话框并确认设置系统壁纸(从主要活动传递的图像) 然后finish();是活动。

这是一个完整的解释:

用户会看到一个GalleryImageView上面的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。按“确定”ButtonImageView通过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();
}

{
    ;
}
4

1 回答 1

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();
});
于 2012-04-08T02:45:56.993 回答