我已经以编程方式创建了图像按钮并设置了图像
img = new ImageView(this);
img.setBackgroundResource(R.drawable.starred);
img.setLayoutParams(new TableRow.LayoutParams(28,28));
我想在用户点击图片时更改图片
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try
{
AlertDialog.Builder builder = new AlertDialog.Builder(Job_Description.this);
builder.setTitle("Save this Job ?");
// builder.setMessage("whould You like to save job");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
img.setImageResource(R.drawable.starapplied);
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
dialog.cancel();
}
});
builder.show();
}catch(Exception e)
{
Log.d("Image clicked", e.toString());
}
}
});
我在这里做错了吗?
答案 0 :(得分:5)
您没有设置图像的任何图像onClick
。你只是推动对话。如果您想在onClick
的{{1}}之后更改图片
然后在ImageView
方法
onClick
答案 1 :(得分:1)
ImageView imageView=(ImageView) findViewById(R.id.parking_map);
imageView.setImageResource(R.drawable.ic_launcher);