在onClick()中获取R.drawable int

时间:2014-11-08 23:58:23

标签: java android onclick

我有一些ImageView,都使用相同的onClick()。如何从特定的ImageView获取ID或R.drawable int以根据我点击的图像打开另一个图像?

5 个答案:

答案 0 :(得分:1)

我想你的xml布局文件中有一些图像视图:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_content" >

    <ImageView
        android:id="@+id/image_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image_drawable_1"
        android:onClick="doSomething" />

    <ImageView
        android:id="@+id/image_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image_drawable_2"
        android:onClick="doSomething" />

    <ImageView
        android:id="@+id/image_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image_drawable_3"
        android:onClick="doSomething" />

</LinearLayout>

您必须将此方法添加到您的活动中:

public void doSomething(View v) {
    switch (v.getId()) {
        case R.id.image_1:
            // You clicked on image 1
            // If you need to do something to the ImageView with this particular Id then uncomment the next line.
            // ImageView imageView = (ImageView) v;
            break;
        case R.id.image_2:
            // You clicked on image 2
            break;
        case R.id.image_3:
            // You clicked on image 3
            break;
    }

}

答案 1 :(得分:1)

使用getResources().getDrawable

示例:

ImageView myImg = (ImageView) findViewById(R.id.myImg); 
myImg.setImageResource = getResources().getDrawable( R.drawable.icon );

答案 2 :(得分:0)

我不认为您可以获取用于为ImageView生成Drawable的资源标识符,但是当您以编程方式设置图像时,可以将其保存为ImageView的标记。

imageView.setImageResource(R.drawable.image_resource);
imageView.setTag(R.drawable.image_resource);

您可以像以后一样检索它:

int imageResource = (Integer) imageView.getTag();

答案 3 :(得分:0)

我存储了通过单击int数组中的onClick()显示的图像的ID,因此我创建了类似于解决该问题的解决方法。

我的onClick()现在看起来像这样:

public void showPictureZoomDialog(View view)
{
    int singlePicValue400 = 0;

        switch (view.getId())
        {
            case R.id.picture_imgvw_pic1:
                singlePicValue400 = currentPicSite.getPicValues400()[0];
                break;
            case R.id.picture_imgvw_pic2:
                singlePicValue400 = currentPicSite.getPicValues400()[1];
                break;
            case R.id.picture_imgvw_pic3:
                singlePicValue400 = currentPicSite.getPicValues400()[2];
                break;
            case R.id.picture_imgvw_pic4:
                singlePicValue400 = currentPicSite.getPicValues400()[3];
                break;
            case R.id.picture_imgvw_pic5:
                singlePicValue400 = currentPicSite.getPicValues400()[4];
                break;
            case R.id.picture_imgvw_pic6:
                singlePicValue400 = currentPicSite.getPicValues400()[5];
                break;
            case R.id.picture_imgvw_pic7:
                singlePicValue400 = currentPicSite.getPicValues400()[6];
                break;
            case R.id.picture_imgvw_pic8:
                singlePicValue400 = currentPicSite.getPicValues400()[7];
                break;
            case R.id.picture_imgvw_pic9:
                singlePicValue400 = currentPicSite.getPicValues400()[8];
                break;
            case R.id.picture_imgvw_pic10:
                singlePicValue400 = currentPicSite.getPicValues400()[9];
                break;
        }

    Intent intent = new Intent(this, PictureZoomDialog.class);
    intent.putExtra("PICID", singlePicValue400);
    startActivity(intent);
}

我通过Intent获取要显示的图像的ID:

public class PictureZoomDialog extends Activity {

private ImageView mDialog;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_picture_zoom_dialog);

    mDialog = (ImageView)findViewById(R.id.pictureactivity_imgvw_pic1);
    mDialog.setClickable(true);
    mDialog.setImageResource(getIntent().getIntExtra("PICID",-1));

    mDialog.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}
}

很多人让我朝着正确的方向前进!

答案 4 :(得分:-1)

您可以使用以下方法从ImageView获取当前的Drawable。

Drawable myDrawable = YourImageView.getDrawable();

然后你可以继续使用myDrawable