我只想点击图片,会出现一个对话框,在该对话框中有几个图像,我只是选择该图像,我的图像应该被该图像替换,但我的图像视图变为空白而不是取代图像
我的代码在
之下 imgJuice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Resources resources = getResources();
final Dialog dialog = new Dialog(getActivity(),
android.R.style.Theme_Dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.choose_juice_image_dialog);
ImageView imgJuiceOrange = (ImageView) dialog
.findViewById(R.id.imgJuiceOrange);
imgJuiceOrange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgJuice.setImageDrawable(resources
.getDrawable(R.id.imgJuiceOrange));
// also i have tried with setImageResource but nothing will be happen
dialog.dismiss();
}
});
}
答案 0 :(得分:3)
使用以下代码:
@Override
public void onClick(View v) {
imgJuice.setImageDrawable(resources
.getDrawable(R.drawable.imgJuiceOrange));
dialog.dismiss();
}
imgJuiceOrange
是drawable
而不是id
。
答案 1 :(得分:-1)
此外,您忘记在此处添加dialog.show()
: -
imgJuice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Resources resources = getResources();
final Dialog dialog = new Dialog(getActivity(),
android.R.style.Theme_Dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.choose_juice_image_dialog);
ImageView imgJuiceOrange = (ImageView) dialog
.findViewById(R.id.imgJuiceOrange);
imgJuiceOrange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgJuice.setImageDrawable(resources
.getDrawable(R.id.imgJuiceOrange));
// also i have tried with setImageResource but nothing will be happen
dialog.dismiss();
}
});
//Dialog Show code missing
dialog.show();
}