1

我以编程方式创建了图像按钮并设置了图像

  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());
         }
        }
    });

我在这里做错了吗?

4

2 回答 2

5

您没有设置图像onClick的任何图像。您只是在推动对话框。如果您想在之后更改图像onClick,请在您的方法ImageView 中添加此行onClick

imageView.setImageResource(resId);

于 2013-10-28T11:45:23.017 回答
1
ImageView imageView=(ImageView) findViewById(R.id.parking_map);
                imageView.setImageResource(R.drawable.ic_launcher);
于 2015-04-15T11:32:21.273 回答